Skip to content

Commit e420722

Browse files
authored
fix: standardize cel env for change mapping (#1477)
1 parent 57f5f68 commit e420722

File tree

5 files changed

+44
-8
lines changed

5 files changed

+44
-8
lines changed

api/v1/zz_generated.deepcopy.go

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fixtures/kubernetes.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ spec:
5757
changes:
5858
mapping:
5959
- filter: >
60-
change.change_type == 'diff' && change.summary == "status.containerStatuses" &&
60+
change_type == 'diff' && summary == "status.containerStatuses" &&
6161
patch != null && has(patch.status) && has(patch.status.containerStatuses) &&
6262
patch.status.containerStatuses.size() > 0 &&
6363
has(patch.status.containerStatuses[0].restartCount)
6464
type: PodCrashLooping
6565
- filter: >
66-
change.change_type == 'diff' &&
66+
change_type == 'diff' &&
6767
jq('.status.conditions[]? | select(.type == "Healthy").message', patch).contains('Health check passed')
6868
type: HealthCheckPassed
6969
exclude:

fixtures/plugin-change-mapping.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ spec:
77
changes:
88
mapping:
99
- filter: >
10-
change.change_type == 'diff' && change.summary == "status.containerStatuses" &&
10+
change_type == 'diff' && summary == "status.containerStatuses" &&
1111
patch != null && has(patch.status) && has(patch.status.containerStatuses) &&
1212
patch.status.containerStatuses.size() > 0 &&
1313
has(patch.status.containerStatuses[0].restartCount)
1414
type: PodCrashLooping
1515
- filter: >
16-
change.change_type == 'diff' && change.summary == "status.images" && config.kind == "Node"
16+
change_type == 'diff' && summary == "status.images" && config.kind == "Node"
1717
type: ImageUpdated

scrapers/changes/rules.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ func (t *changeRule) match(result *v1.ScrapeResult) (bool, error) {
4040
}
4141

4242
func (t *changeRule) process(ctx api.ScrapeContext, change *v1.ChangeResult) error {
43-
env := map[string]any{
44-
"change": change.AsMap(),
45-
"patch": change.PatchesMap(),
46-
}
43+
env := change.AsMap()
44+
45+
// Deprecated: For backwards compatibility
46+
change.FlushMap() // To prevent recursion
47+
env["change"] = change.AsMap()
48+
env["patch"] = change.PatchesMap()
4749

4850
ok, err := gomplate.RunTemplateBool(env, gomplate.Template{Expression: t.Rule})
4951
if err != nil {

scrapers/changes/rules_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ var _ = Describe("TestProcessRules", Ordered, func() {
1515
rules []v1.ChangeMapping
1616
err bool
1717
}{
18+
{
19+
name: "config_type in celenv",
20+
input: v1.ScrapeResult{
21+
Changes: []v1.ChangeResult{{ChangeType: "diff", Patches: "", ConfigType: "HelmRelease"}},
22+
},
23+
expect: []v1.ChangeResult{{ChangeType: "diff", Patches: "", ConfigType: "HelmRelease"}},
24+
rules: []v1.ChangeMapping{{
25+
Type: "diff",
26+
Filter: `config_type == 'HelmRelease'`,
27+
}},
28+
},
1829
{
1930
name: "health mapping - fail",
2031
input: v1.ScrapeResult{
@@ -102,6 +113,10 @@ var _ = Describe("TestProcessRules", Ordered, func() {
102113
}
103114

104115
for _, tt := range tests {
116+
// if tt.name != "config_type in celenv" {
117+
// continue
118+
// }
119+
105120
It(tt.name, func() {
106121
err := ProcessRules(api.NewScrapeContext(DefaultContext), &tt.input, tt.rules...)
107122
if tt.err {

0 commit comments

Comments
 (0)