Skip to content

Commit 38ed2f5

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 38ed2f5

File tree

1 file changed

+24
-29
lines changed

1 file changed

+24
-29
lines changed

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

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -74,41 +74,36 @@ local patchOrExcludeRule(rule, ruleSet, operation) =
7474
else
7575
[] + patchOrExcludeRule(rule, ruleSet[1:], operation);
7676

77+
local findRuleName(rule, ruleSet) =
78+
local _findSameRuleName(rule, ruleSet, index) =
79+
if std.length(ruleSet) == index then
80+
[]
81+
else if (('alert' in rule && 'alert' in ruleSet[index] && rule.alert == ruleSet[index].alert) ||
82+
('record' in rule && 'record' in ruleSet[index] && rule.record == ruleSet[index].record)) then
83+
[index] + _findSameRuleName(rule, ruleSet, index + 1)
84+
else
85+
[] + _findSameRuleName(rule, ruleSet, index + 1);
86+
_findSameRuleName(rule, ruleSet, 0);
7787

78-
local sameRuleName(rule1, rule2) =
79-
if ('alert' in rule1 && 'alert' in rule2) then
80-
rule1.alert == rule2.alert
81-
else if ('record' in rule1 && 'record' in rule2) then
82-
rule1.record == rule2.record
83-
else
84-
false;
85-
86-
local indexRules(lastRule, ruleSet) =
87-
if std.length(ruleSet) == 0 then
88-
[]
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
101-
else
102-
assert false : 'rule should have either "alert" or "record" field' + std.toString(rule);
103-
'';
88+
local indexRules(ruleSet) =
89+
local _indexRules(ruleSet, index) =
90+
if std.length(ruleSet) == index then
91+
[]
92+
else
93+
// First we find the number of occurences of the rule in the ruleSet and
94+
// get an array containing the indexes of all the occurences.
95+
// Then, based on the current index of the rule in the ruleSet we are able
96+
// to deduce the index of the rule in the list of rules with the same name.
97+
local ruleIndex = std.find(index, findRuleName(ruleSet[index], ruleSet))[0];
98+
local updatedRule = std.mergePatch(ruleSet[index], { index: ruleIndex });
99+
[updatedRule] + _indexRules(ruleSet, index + 1);
100+
_indexRules(ruleSet, 0);
104101

105102
local patchOrExcludeRuleGroup(group, groupSet, operation) =
106103
if std.length(groupSet) == 0 then
107104
[group.rules]
108105
else if (group.name == groupSet[0].name) then
109-
local indexedRules = indexRules(null, std.sort(
110-
group.rules, keyF=ruleName
111-
));
106+
local indexedRules = indexRules(group.rules);
112107
[patchOrExcludeRule(rule, groupSet[0].rules, operation) for rule in indexedRules]
113108
else
114109
[] + patchOrExcludeRuleGroup(group, groupSet[1:], operation);

0 commit comments

Comments
 (0)