From c96f224e8e949d61a3be6dbde4eef7393a6f0118 Mon Sep 17 00:00:00 2001 From: Jim Bugwadia Date: Sun, 18 Aug 2024 02:40:27 -0700 Subject: [PATCH] reduce recursions for nested var substutution (#10877) Signed-off-by: Jim Bugwadia --- pkg/engine/variables/vars.go | 11 +++++------ pkg/engine/variables/vars_test.go | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/pkg/engine/variables/vars.go b/pkg/engine/variables/vars.go index 6f2fa11f19..93c6a0b964 100644 --- a/pkg/engine/variables/vars.go +++ b/pkg/engine/variables/vars.go @@ -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, "$()") diff --git a/pkg/engine/variables/vars_test.go b/pkg/engine/variables/vars_test.go index cc441f96de..1b16f2dbc0 100644 --- a/pkg/engine/variables/vars_test.go +++ b/pkg/engine/variables/vars_test.go @@ -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)