Skip to content

Commit ea3b4dc

Browse files
committed
rule-sanitizer: avoid sorting rules
This change improves the rule-sanitizer library to avoid sorting all the rules. This was originally done to simplify indexing rules with the same names, but it turns out that integrating this lib can be complex to review when all the rules of the manifests are reordered. Thus, it is easier to just leave the file as it was initially sorted. Signed-off-by: Damien Grisonnet <dgrisonn@redhat.com>
1 parent bfb332e commit ea3b4dc

File tree

1 file changed

+6
-19
lines changed

1 file changed

+6
-19
lines changed

jsonnet/kube-prometheus/lib/rule-sanitizer.libsonnet

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -83,32 +83,19 @@ local sameRuleName(rule1, rule2) =
8383
else
8484
false;
8585

86-
local indexRules(lastRule, ruleSet) =
87-
if std.length(ruleSet) == 0 then
86+
local indexRules(ruleSet, index) =
87+
if std.length(ruleSet) == index then
8888
[]
89-
else if (lastRule != null) && sameRuleName(lastRule, ruleSet[0]) then
90-
local updatedRule = std.mergePatch(ruleSet[0], { index: lastRule.index + 1 });
91-
[updatedRule] + indexRules(updatedRule, ruleSet[1:])
92-
else
93-
local updatedRule = std.mergePatch(ruleSet[0], { index: 0 });
94-
[updatedRule] + indexRules(updatedRule, ruleSet[1:]);
95-
96-
local ruleName(rule) =
97-
if ('alert' in rule) then
98-
rule.alert
99-
else if ('record' in rule) then
100-
rule.record
10189
else
102-
assert false : 'rule should have either "alert" or "record" field' + std.toString(rule);
103-
'';
90+
local ruleIndex = std.find(index, std.find(ruleSet[index], ruleSet))[0];
91+
local updatedRule = std.mergePatch(ruleSet[index], { index: ruleIndex });
92+
[updatedRule] + indexRules(ruleSet, index + 1);
10493

10594
local patchOrExcludeRuleGroup(group, groupSet, operation) =
10695
if std.length(groupSet) == 0 then
10796
[group.rules]
10897
else if (group.name == groupSet[0].name) then
109-
local indexedRules = indexRules(null, std.sort(
110-
group.rules, keyF=ruleName
111-
));
98+
local indexedRules = indexRules(group.rules, 0);
11299
[patchOrExcludeRule(rule, groupSet[0].rules, operation) for rule in indexedRules]
113100
else
114101
[] + patchOrExcludeRuleGroup(group, groupSet[1:], operation);

0 commit comments

Comments
 (0)