Skip to content

Commit ff39d39

Browse files
committed
thanos-ruler: set default retention from UWM Prometheus if present
If no thanos ruler retention is set, but UWM Prometheus retention is set, use the Prometheus retention as the thanos ruler retention. Setting thanos ruler retention explicitly overrides this. Signed-off-by: Jan Fajerski <[email protected]> generated-with-ai: cursor
1 parent d0d31fd commit ff39d39

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

pkg/manifests/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,13 @@ func (u *UserWorkloadConfiguration) applyDefaults() {
664664
if u.ThanosRuler == nil {
665665
u.ThanosRuler = &ThanosRulerConfig{}
666666
}
667+
// If the user configured a retention for user-workload Prometheus but did not
668+
// explicitly set a retention for Thanos Ruler, default Thanos Ruler retention
669+
// to the same value as Prometheus. This keeps the effective retention aligned
670+
// unless the user overrides it for Thanos Ruler.
671+
if u.ThanosRuler.Retention == "" && u.Prometheus != nil && u.Prometheus.Retention != "" {
672+
u.ThanosRuler.Retention = u.Prometheus.Retention
673+
}
667674
if u.Alertmanager == nil {
668675
u.Alertmanager = &AlertmanagerUserWorkloadConfig{}
669676
}

pkg/manifests/config_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,34 @@ thanosRuler:
261261
return ``
262262
},
263263
},
264+
{
265+
name: "thanosRuler.retention defaults to prometheus.retention when unset",
266+
configString: func() string {
267+
return `
268+
prometheus:
269+
retention: 15d
270+
`
271+
},
272+
configCheck: func(uwmc *UserWorkloadConfiguration) {
273+
require.Equal(t, "15d", uwmc.Prometheus.Retention)
274+
require.Equal(t, "15d", uwmc.ThanosRuler.Retention)
275+
},
276+
},
277+
{
278+
name: "explicit thanosRuler.retention overrides prometheus.retention",
279+
configString: func() string {
280+
return `
281+
prometheus:
282+
retention: 15d
283+
thanosRuler:
284+
retention: 7d
285+
`
286+
},
287+
configCheck: func(uwmc *UserWorkloadConfiguration) {
288+
require.Equal(t, "15d", uwmc.Prometheus.Retention)
289+
require.Equal(t, "7d", uwmc.ThanosRuler.Retention)
290+
},
291+
},
264292
}
265293

266294
for _, tc := range tcs {

pkg/manifests/manifests_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4305,6 +4305,36 @@ func TestThanosRulerRetentionConfig(t *testing.T) {
43054305
}
43064306
}
43074307

4308+
func TestThanosRulerRetentionDefaultsToPrometheusRetention(t *testing.T) {
4309+
// Construct a user-workload configuration the same way the operator does in
4310+
// production, i.e. via NewUserConfigFromString, so that UserWorkloadConfiguration
4311+
// defaulting (including retention alignment) is applied.
4312+
uwc, err := NewUserConfigFromString(`
4313+
prometheus:
4314+
retention: 45d
4315+
`)
4316+
require.NoError(t, err)
4317+
4318+
c := NewDefaultConfig()
4319+
c.UserWorkloadConfiguration = uwc
4320+
4321+
f := NewFactory("openshift-monitoring", "openshift-user-workload-monitoring", c, defaultInfrastructureReader(), &fakeProxyReader{}, NewAssets(assetsPath), &APIServerConfig{}, &configv1.Console{})
4322+
4323+
tr, err := f.ThanosRulerCustomResource(
4324+
&v1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "foo"}},
4325+
nil,
4326+
)
4327+
4328+
if err != nil {
4329+
t.Fatalf("Unexpected error occured %v", err)
4330+
return
4331+
}
4332+
4333+
if tr.Spec.Retention != "45d" {
4334+
t.Fatalf("Retention is not configured correctly, expected %s, got %s", "45d", tr.Spec.Retention)
4335+
}
4336+
}
4337+
43084338
func TestPrometheusGoGCRelatedConfig(t *testing.T) {
43094339
for _, tc := range []struct {
43104340
ir InfrastructureReader

0 commit comments

Comments
 (0)