Skip to content

Commit

Permalink
reduce recursions for nested var substutution (kyverno#10877)
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Bugwadia <[email protected]>
  • Loading branch information
JimBugwadia authored Aug 18, 2024
1 parent a5915a3 commit c96f224
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
11 changes: 5 additions & 6 deletions pkg/engine/variables/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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())
}

Expand Down Expand Up @@ -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, "$()")
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/variables/vars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c96f224

Please sign in to comment.