From 4524d12648534382208e11fb4976c6e1c9990a77 Mon Sep 17 00:00:00 2001 From: Ids van der Molen Date: Sat, 17 Oct 2020 12:06:48 +0200 Subject: [PATCH] feat(controller): Allow whitespace in variable substitution. Fixes #4286 --- docs/variables.md | 6 +++--- workflow/common/util.go | 2 +- workflow/common/util_test.go | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/docs/variables.md b/docs/variables.md index 3145542d6271..5a0c1db95789 100644 --- a/docs/variables.md +++ b/docs/variables.md @@ -1,9 +1,9 @@ # Workflow Variables -Some fields in a workflow specification allow for variable references which are automatically substituted by Argo. +Some fields in a workflow specification allow for variable references which are automatically substituted by Argo. ??? note "How to use variables" - Variables are enclosed in curly braces and **must not** include whitespace. + Variables are enclosed in curly braces and **may** include whitespace between the brackets and variable. ``` yaml apiVersion: argoproj.io/v1alpha1 @@ -24,7 +24,7 @@ Some fields in a workflow specification allow for variable references which are container: image: docker/whalesay command: [cowsay] - # args: ["{{ inputs.parameters.message }}"] <- bad + # args: ["{{ inputs.parameters.message }}"] <- good args: ["{{inputs.parameters.message}}"] # good ``` diff --git a/workflow/common/util.go b/workflow/common/util.go index 0e6e36cc92f5..ffca81d8a67e 100644 --- a/workflow/common/util.go +++ b/workflow/common/util.go @@ -373,7 +373,7 @@ func SubstituteParams(tmpl *wfv1.Template, globalParams, localParams Parameters) func Replace(fstTmpl *fasttemplate.Template, replaceMap map[string]string, allowUnresolved bool) (string, error) { var unresolvedErr error replacedTmpl := fstTmpl.ExecuteFuncString(func(w io.Writer, tag string) (int, error) { - replacement, ok := replaceMap[tag] + replacement, ok := replaceMap[strings.TrimSpace(tag)] if !ok { // Attempt to resolve nested tags, if possible if index := strings.LastIndex(tag, "{{"); index > 0 { diff --git a/workflow/common/util_test.go b/workflow/common/util_test.go index 0b66a00c7b9a..13f741d62d5d 100644 --- a/workflow/common/util_test.go +++ b/workflow/common/util_test.go @@ -157,3 +157,17 @@ func TestNestedReplaceString(t *testing.T) { } } } + +func TestReplaceStringWithWhiteSpace(t *testing.T) { + + replaceMap := map[string]string{"inputs.parameters.message": "hello world"} + + test := `{{ inputs.parameters.message }}` + fstTmpl, err := fasttemplate.NewTemplate(test, "{{", "}}") + if assert.NoError(t, err) { + replacement, err := Replace(fstTmpl, replaceMap, true) + if assert.NoError(t, err) { + assert.Equal(t, "hello world", replacement) + } + } +}