diff --git a/docs/variables.md b/docs/variables.md index 24b5fbb50ecf..149bb6b5aa1f 100644 --- a/docs/variables.md +++ b/docs/variables.md @@ -1,5 +1,33 @@ # Workflow Variables +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 **may** include whitespace between the brackets and variable. + + ``` yaml + apiVersion: argoproj.io/v1alpha1 + kind: Workflow + metadata: + generateName: hello-world-parameters- + spec: + entrypoint: whalesay + arguments: + parameters: + - name: message + value: hello world + templates: + - name: whalesay + inputs: + parameters: + - name: message + container: + image: docker/whalesay + command: [cowsay] + # args: ["{{ inputs.parameters.message }}"] <- good + args: ["{{inputs.parameters.message}}"] # good + ``` + The following variables are made available to reference various metadata of a workflow: ## All Templates diff --git a/workflow/common/util.go b/workflow/common/util.go index 4d32e146d8e2..b09f630a0c2a 100644 --- a/workflow/common/util.go +++ b/workflow/common/util.go @@ -372,7 +372,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) + } + } +}