Skip to content

Commit

Permalink
feat(controller): Allow whitespace in variable substitution. Fixes ar…
Browse files Browse the repository at this point in the history
  • Loading branch information
idsvandermolen committed Oct 17, 2020
1 parent db20b4f commit 4524d12
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
6 changes: 3 additions & 3 deletions docs/variables.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
```

Expand Down
2 changes: 1 addition & 1 deletion workflow/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
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 4524d12

Please sign in to comment.