Skip to content

Commit

Permalink
feat: add version check for thanos. keep_firing_for now available (#6283
Browse files Browse the repository at this point in the history
)
  • Loading branch information
DeamonMV committed Feb 8, 2024
1 parent ca78f91 commit c5098f5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
16 changes: 4 additions & 12 deletions pkg/operator/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func (prs *PrometheusRuleSelector) sanitizePrometheusRulesSpec(promRuleSpec moni
component := "Prometheus"

if prs.ruleFormat == ThanosFormat {
minVersionKeepFiringFor = semver.MustParse("0.34.0")
minVersionLimits = semver.MustParse("0.24.0")
component = "Thanos"
}
Expand All @@ -123,18 +124,9 @@ func (prs *PrometheusRuleSelector) sanitizePrometheusRulesSpec(promRuleSpec moni
}

for j := range promRuleSpec.Groups[i].Rules {
switch prs.ruleFormat {
case PrometheusFormat:
if promRuleSpec.Groups[i].Rules[j].KeepFiringFor != nil && prs.version.LT(minVersionKeepFiringFor) {
promRuleSpec.Groups[i].Rules[j].KeepFiringFor = nil
level.Warn(logger).Log("msg", fmt.Sprintf("ignoring 'keep_firing_for' not supported by %s", component), "minimum_version", minVersionKeepFiringFor)
}
case ThanosFormat:
// keep_firing_for is not yet supported in thanos https://github.com/thanos-io/thanos/issues/6165
if promRuleSpec.Groups[i].Rules[j].KeepFiringFor != nil {
promRuleSpec.Groups[i].Rules[j].KeepFiringFor = nil
level.Warn(logger).Log("msg", "ignoring `keep_firing_for` as it is not yet supported in thanos, see https://github.com/thanos-io/thanos/issues/6165")
}
if promRuleSpec.Groups[i].Rules[j].KeepFiringFor != nil && prs.version.LT(minVersionKeepFiringFor) {
promRuleSpec.Groups[i].Rules[j].KeepFiringFor = nil
level.Warn(logger).Log("msg", fmt.Sprintf("ignoring 'keep_firing_for' not supported by %s", component), "minimum_version", minVersionKeepFiringFor)
}
}
}
Expand Down
30 changes: 28 additions & 2 deletions pkg/operator/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ func TestMakeRulesConfigMaps(t *testing.T) {
t.Run("shouldDropLimitFieldForUnsupportedPrometheusVersion", shouldDropLimitFieldForUnsupportedPrometheusVersion)
t.Run("shouldDropLimitFieldForUnsupportedThanosVersion", shouldDropLimitFieldForUnsupportedThanosVersion)
t.Run("shouldAcceptRuleWithKeepFiringForPrometheus", shouldAcceptRuleWithKeepFiringForPrometheus)
t.Run("shouldDropKeepFiringForThanos", shouldDropKeepFiringForThanos)
t.Run("shouldDropRuleFiringForThanos", shouldDropRuleFiringForThanos)
t.Run("shouldAcceptRuleFiringForThanos", shouldAcceptRuleFiringForThanos)
t.Run("shouldDropKeepFiringForFieldForUnsupportedPrometheusVersion", shouldDropKeepFiringForFieldForUnsupportedPrometheusVersion)
}

Expand Down Expand Up @@ -227,7 +228,7 @@ func shouldAcceptRuleWithKeepFiringForPrometheus(t *testing.T) {
}
}

func shouldDropKeepFiringForThanos(t *testing.T) {
func shouldDropRuleFiringForThanos(t *testing.T) {
duration := monitoringv1.NonEmptyDuration("5m")
rules := &monitoringv1.PrometheusRule{
Spec: monitoringv1.PrometheusRuleSpec{Groups: []monitoringv1.RuleGroup{
Expand All @@ -252,6 +253,31 @@ func shouldDropKeepFiringForThanos(t *testing.T) {
}
}

func shouldAcceptRuleFiringForThanos(t *testing.T) {
duration := monitoringv1.NonEmptyDuration("5m")
rules := &monitoringv1.PrometheusRule{
Spec: monitoringv1.PrometheusRuleSpec{Groups: []monitoringv1.RuleGroup{
{
Name: "group",
Rules: []monitoringv1.Rule{
{
Alert: "alert",
Expr: intstr.FromString("vector(1)"),
KeepFiringFor: &duration,
},
},
},
}},
}

thanosVersion, _ := semver.ParseTolerant("v0.34.0")
pr := newRuleSelectorForConfigGeneration(ThanosFormat, thanosVersion)
content, _ := pr.generateRulesConfiguration(rules)
if !strings.Contains(content, "keep_firing_for") {
t.Fatalf("expected `keep_firing_for` to be present in PrometheusRule")
}
}

func shouldDropKeepFiringForFieldForUnsupportedPrometheusVersion(t *testing.T) {
duration := monitoringv1.NonEmptyDuration("5m")
rules := &monitoringv1.PrometheusRule{
Expand Down

0 comments on commit c5098f5

Please sign in to comment.