Skip to content

Commit 93484f7

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 93484f7

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

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

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -83,32 +83,23 @@ 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+
// First we find the number of occurences of the rule in the ruleSet and
91+
// get an array containing the indexes of all the occurences.
92+
// Then, based on the current index of the rule in the ruleSet we are able
93+
// to deduce the index of the rule in the list of rules with the same name.
94+
local ruleIndex = std.find(index, std.find(ruleSet[index], ruleSet))[0];
95+
local updatedRule = std.mergePatch(ruleSet[index], { index: ruleIndex });
96+
[updatedRule] + indexRules(ruleSet, index + 1);
10497

10598
local patchOrExcludeRuleGroup(group, groupSet, operation) =
10699
if std.length(groupSet) == 0 then
107100
[group.rules]
108101
else if (group.name == groupSet[0].name) then
109-
local indexedRules = indexRules(null, std.sort(
110-
group.rules, keyF=ruleName
111-
));
102+
local indexedRules = indexRules(group.rules, 0);
112103
[patchOrExcludeRule(rule, groupSet[0].rules, operation) for rule in indexedRules]
113104
else
114105
[] + patchOrExcludeRuleGroup(group, groupSet[1:], operation);

0 commit comments

Comments
 (0)