1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00

oldObject translation solved in autogen (#6305)

* OldObject translation solved in autogen

Signed-off-by: Abhishek Kumar <abhishek22512@gmail.com>

* CronJob fixed in autogen

Signed-off-by: Abhishek Kumar <abhishek22512@gmail.com>

* tests added

Signed-off-by: Abhishek Kumar <abhishek22512@gmail.com>

---------

Signed-off-by: Abhishek Kumar <abhishek22512@gmail.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:
Abhishek Kumar 2023-02-21 14:36:29 +05:30 committed by GitHub
parent f2eb87b344
commit 7d4ea1dcdc
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"
"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))

View file

@ -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