1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-15 17:51:20 +00:00

oldObject translation solved in autogen (#6305) (#6372)

* OldObject translation solved in autogen



* CronJob fixed in autogen



* tests added



---------

Signed-off-by: Abhishek Kumar <abhishek22512@gmail.com>
Co-authored-by: Abhishek Kumar <76171953+octonawish-akcodes@users.noreply.github.com>
Co-authored-by: shuting <shuting@nirmata.com>
Co-authored-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>
This commit is contained in:
gcp-cherry-pick-bot[bot] 2023-02-22 09:01:47 +00:00 committed by GitHub
parent f79b282140
commit dbfe39ad90
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 0 deletions

View file

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"reflect"
"strings" "strings"
"testing" "testing"
@ -543,6 +544,47 @@ func Test_CronJobAndDeployment(t *testing.T) {
assert.DeepEqual(t, rulePatches, expectedPatches) 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) { func Test_UpdateVariablePath(t *testing.T) {
dir, err := os.Getwd() dir, err := os.Getwd()
baseDir := filepath.Dir(filepath.Dir(dir)) baseDir := filepath.Dir(filepath.Dir(dir))

View file

@ -299,9 +299,11 @@ func generateCronJobRule(rule *kyvernov1.Rule, controllers string) *kyvernov1.Ru
func updateGenRuleByte(pbyte []byte, kind string) (obj []byte) { func updateGenRuleByte(pbyte []byte, kind string) (obj []byte) {
if kind == "Pod" { if kind == "Pod" {
obj = []byte(strings.ReplaceAll(string(pbyte), "request.object.spec", "request.object.spec.template.spec")) 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" { if kind == "Cronjob" {
obj = []byte(strings.ReplaceAll(string(pbyte), "request.object.spec", "request.object.spec.jobTemplate.spec.template.spec")) 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")) obj = []byte(strings.ReplaceAll(string(obj), "request.object.metadata", "request.object.spec.template.metadata"))
return obj return obj