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

reduce recursions for nested var substutution (#10877)

Signed-off-by: Jim Bugwadia <jim@nirmata.com>
This commit is contained in:
Jim Bugwadia 2024-08-18 02:40:27 -07:00 committed by GitHub
parent a5915a310b
commit c96f224e8e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 6 additions and 7 deletions

View file

@ -205,10 +205,10 @@ func substituteReferences(log logr.Logger, rule interface{}) (interface{}, error
}
func ValidateElementInForEach(log logr.Logger, rule interface{}) (interface{}, error) {
return jsonUtils.NewTraversal(rule, validateElementInForEach(log)).TraverseJSON()
return jsonUtils.NewTraversal(rule, validateElementInForEach()).TraverseJSON()
}
func validateElementInForEach(log logr.Logger) jsonUtils.Action {
func validateElementInForEach() jsonUtils.Action {
return jsonUtils.OnlyForLeafsAndKeys(func(data *jsonUtils.ActionData) (interface{}, error) {
value, ok := data.Element.(string)
if !ok {
@ -257,7 +257,7 @@ func substituteReferencesIfAny(log logr.Logger) jsonUtils.Action {
v = v[1:]
}
resolvedReference, err := resolveReference(log, data.Document, v, data.Path)
resolvedReference, err := resolveReference(data.Document, v, data.Path)
if err != nil {
switch err.(type) {
case context.InvalidVariableError:
@ -364,12 +364,11 @@ func substituteVariablesIfAny(log logr.Logger, ctx context.EvalInterface, vr Var
}
prefix := ""
if !initial {
prefix = string(old[0])
}
if value, err = substituteVarInPattern(prefix, originalPattern, v, substitutedVar); err != nil {
if value, err = substituteVarInPattern(prefix, value, v, substitutedVar); err != nil {
return nil, fmt.Errorf("failed to resolve %v at path %s: %s", variable, data.Path, err.Error())
}
@ -426,7 +425,7 @@ func replaceBracesAndTrimSpaces(v string) string {
return variable
}
func resolveReference(log logr.Logger, fullDocument interface{}, reference, absolutePath string) (interface{}, error) {
func resolveReference(fullDocument interface{}, reference, absolutePath string) (interface{}, error) {
var foundValue interface{}
path := strings.Trim(reference, "$()")

View file

@ -1077,7 +1077,7 @@ func TestActualizePattern_GivenRelativePathThatExists(t *testing.T) {
// pattern, err := actualizePattern(log.Log, pattern, referencePath, absolutePath)
pattern, err := resolveReference(logr.Discard(), pattern, referencePath, absolutePath)
pattern, err := resolveReference(pattern, referencePath, absolutePath)
assert.NilError(t, err)
assert.DeepEqual(t, resolvedReference, pattern)