Skip to content

Commit

Permalink
feat(controller): Allow whitespace in variable substitution. Fixes #4286
Browse files Browse the repository at this point in the history
  • Loading branch information
idsvandermolen authored and simster7 committed Nov 17, 2020
1 parent bf3fec1 commit 66f2306
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
28 changes: 28 additions & 0 deletions docs/variables.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion workflow/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions workflow/common/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

0 comments on commit 66f2306

Please sign in to comment.