From dbfe39ad90b98477e793d8eb3a2ad28fa53b8641 Mon Sep 17 00:00:00 2001 From: "gcp-cherry-pick-bot[bot]" <98988430+gcp-cherry-pick-bot[bot]@users.noreply.github.com> Date: Wed, 22 Feb 2023 09:01:47 +0000 Subject: [PATCH] oldObject translation solved in autogen (#6305) (#6372) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * OldObject translation solved in autogen * CronJob fixed in autogen * tests added --------- Signed-off-by: Abhishek Kumar Co-authored-by: Abhishek Kumar <76171953+octonawish-akcodes@users.noreply.github.com> Co-authored-by: shuting Co-authored-by: Charles-Edouard Brétéché --- pkg/autogen/autogen_test.go | 42 +++++++++++++++++++++++++++++++++++++ pkg/autogen/rule.go | 2 ++ 2 files changed, 44 insertions(+) diff --git a/pkg/autogen/autogen_test.go b/pkg/autogen/autogen_test.go index 62e1e08912..95d1ac60a9 100644 --- a/pkg/autogen/autogen_test.go +++ b/pkg/autogen/autogen_test.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path/filepath" + "reflect" "strings" "testing" @@ -543,6 +544,47 @@ func Test_CronJobAndDeployment(t *testing.T) { assert.DeepEqual(t, rulePatches, expectedPatches) } +func TestUpdateGenRuleByte(t *testing.T) { + tests := []struct { + pbyte []byte + kind string + want []byte + wantErr bool + }{ + { + pbyte: []byte("request.object.spec"), + kind: "Pod", + want: []byte("request.object.spec.template.spec"), + }, + { + pbyte: []byte("request.oldObject.spec"), + kind: "Pod", + want: []byte("request.oldObject.spec.template.spec"), + }, + { + pbyte: []byte("request.object.spec"), + kind: "Cronjob", + want: []byte("request.object.spec.jobTemplate.spec.template.spec"), + }, + { + pbyte: []byte("request.oldObject.spec"), + kind: "Cronjob", + want: []byte("request.oldObject.spec.jobTemplate.spec.template.spec"), + }, + { + pbyte: []byte("request.object.metadata"), + kind: "Pod", + want: []byte("request.object.spec.template.metadata"), + }, + } + for _, tt := range tests { + got := updateGenRuleByte(tt.pbyte, tt.kind) + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("updateGenRuleByte() = %v, want %v", string(got), string(tt.want)) + } + } +} + func Test_UpdateVariablePath(t *testing.T) { dir, err := os.Getwd() baseDir := filepath.Dir(filepath.Dir(dir)) diff --git a/pkg/autogen/rule.go b/pkg/autogen/rule.go index 1378d5161b..b9d331bc39 100644 --- a/pkg/autogen/rule.go +++ b/pkg/autogen/rule.go @@ -299,9 +299,11 @@ func generateCronJobRule(rule *kyvernov1.Rule, controllers string) *kyvernov1.Ru func updateGenRuleByte(pbyte []byte, kind string) (obj []byte) { if kind == "Pod" { obj = []byte(strings.ReplaceAll(string(pbyte), "request.object.spec", "request.object.spec.template.spec")) + obj = []byte(strings.ReplaceAll(string(obj), "request.oldObject.spec", "request.oldObject.spec.template.spec")) } if kind == "Cronjob" { obj = []byte(strings.ReplaceAll(string(pbyte), "request.object.spec", "request.object.spec.jobTemplate.spec.template.spec")) + obj = []byte(strings.ReplaceAll(string(obj), "request.oldObject.spec", "request.oldObject.spec.jobTemplate.spec.template.spec")) } obj = []byte(strings.ReplaceAll(string(obj), "request.object.metadata", "request.object.spec.template.metadata")) return obj