1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-04-17 17:56:33 +00:00

Fix foreach jmespath issue ()

This commit is contained in:
Vyankatesh Kudtarkar 2021-12-21 18:25:27 +05:30 committed by GitHub
parent a371dfbaa6
commit 6a942683b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 1 deletions
pkg/engine/variables

View file

@ -328,7 +328,6 @@ func substituteVariablesIfAny(log logr.Logger, ctx context.EvalInterface, vr Var
vars := RegexVariables.FindAllString(value, -1)
for len(vars) > 0 {
originalPattern := value
for _, v := range vars {
initial := len(regexVariableInit.FindAllString(v, -1)) > 0
old := v
@ -415,6 +414,9 @@ var regexPathDigit = regexp.MustCompile(`\.?([\d])\.?`)
// getJMESPath converts path to JMESPath format
func getJMESPath(rawPath string) string {
tokens := strings.Split(rawPath, "/")[3:] // skip "/" + 2 elements (e.g. mutate.overlay | validate.pattern)
if strings.Contains(rawPath, "foreach") {
tokens = strings.Split(rawPath, "/")[5:] // skip "/" + 4 elements (e.g. mutate.foreach/list/overlay | validate.mutate.foreach/list/pattern)
}
path := strings.Join(tokens, ".")
b := regexPathDigit.ReplaceAll([]byte(path), []byte("[$1]."))
result := strings.Trim(string(b), ".")

View file

@ -1157,3 +1157,9 @@ func Test_getJMESPath(t *testing.T) {
assert.Equal(t, "spec.containers[0].volumes[1]", getJMESPath("/validate/pattern/spec/containers/0/volumes/1"))
assert.Equal(t, "[0]", getJMESPath("/mutate/overlay/0"))
}
func Test_getJMESPathForForeach(t *testing.T) {
assert.Equal(t, "spec.containers[0]", getJMESPath("/validate/foreach/0/pattern/spec/containers/0"))
assert.Equal(t, "spec.containers[0].volumes[1]", getJMESPath("/validate/foreach/0/pattern/spec/containers/0/volumes/1"))
assert.Equal(t, "[0]", getJMESPath("/mutate/foreach/0/overlay/0"))
}