From f8934e015570d5543df9565246c7caced9f2374f Mon Sep 17 00:00:00 2001 From: Erik Sommer Date: Thu, 20 Jun 2024 01:36:43 +0200 Subject: [PATCH] wip add flag --label-excluded-rule-groups-regex --- pkg/mimirtool/commands/rules.go | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/pkg/mimirtool/commands/rules.go b/pkg/mimirtool/commands/rules.go index 886fb48523..dda990c1ba 100644 --- a/pkg/mimirtool/commands/rules.go +++ b/pkg/mimirtool/commands/rules.go @@ -93,10 +93,12 @@ type RuleCommand struct { SyncConcurrency int // Prepare Rules Config - InPlaceEdit bool - AggregationLabel string - AggregationLabelExcludedRuleGroups string - aggregationLabelExcludedRuleGroupsList map[string]struct{} + InPlaceEdit bool + AggregationLabel string + AggregationLabelExcludedRuleGroups string + AggregationLabelExcludedRuleGroupsRegex string + aggregationLabelExcludedRuleGroupsList map[string]struct{} + aggregationLabelExcludedRuleGroupsRegex *regexp.Regexp // Lint Rules Config LintDryRun bool @@ -265,6 +267,7 @@ func (r *RuleCommand) Register(app *kingpin.Application, envVars EnvVarNames, re ).Short('i').BoolVar(&r.InPlaceEdit) prepareCmd.Flag("label", "label to include as part of the aggregations.").Default(defaultPrepareAggregationLabel).Short('l').StringVar(&r.AggregationLabel) prepareCmd.Flag("label-excluded-rule-groups", "Comma separated list of rule group names to exclude when including the configured label to aggregations.").StringVar(&r.AggregationLabelExcludedRuleGroups) + prepareCmd.Flag("label-excluded-rule-groups-regex", "Comma separated list of regexes for rule group names to exclude when including the configured label to aggregations.").StringVar(&r.AggregationLabelExcludedRuleGroupsRegex) // Lint Command lintCmd.Arg("rule-files", "The rule files to check.").ExistingFilesVar(&r.RuleFilesList) @@ -349,6 +352,13 @@ func (r *RuleCommand) setupArgs() error { } } + aggregationLabelExcludedRuleGroupsRegex, err := regexp.Compile(fmt.Sprintf("(%s)", strings.ReplaceAll(r.AggregationLabelExcludedRuleGroupsRegex, ",", "|"))) + if err != nil { + return errors.New("invalid regex for aggregation label excluded rule groups, provided by the flag --label-excluded-rule-groups-regex") + } + r.aggregationLabelExcludedRuleGroupsRegex = aggregationLabelExcludedRuleGroupsRegex + + // TODO: Remove statement in Mimir 2.14. if r.RuleFiles != "" { log.Warn("flag --rule-files is deprecated, use the argument instead") @@ -741,7 +751,13 @@ func (r *RuleCommand) prepare(_ *kingpin.ParseContext) error { // Do not apply the aggregation label to excluded rule groups. applyTo := func(group rwrulefmt.RuleGroup, _ rulefmt.RuleNode) bool { _, excluded := r.aggregationLabelExcludedRuleGroupsList[group.Name] - return !excluded + if excluded { + return false + } + if r.aggregationLabelExcludedRuleGroupsRegex.MatchString(group.Name) { + return false + } + return true } var count, mod int