Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose all input parameters to template as JSON #1488

Merged
merged 3 commits into from Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/variables.md
Expand Up @@ -6,6 +6,7 @@ The following variables are made available to reference various metadata of a wo
| Variable | Description|
|----------|------------|
| `inputs.parameters.<NAME>`| Input parameter to a template |
| `inputs.parameters`| All input parameters to a template as a JSON string |
| `inputs.artifacts.<NAME>` | Input artifact to a template |

## Steps Templates:
Expand Down
9 changes: 7 additions & 2 deletions workflow/common/util.go
Expand Up @@ -325,6 +325,12 @@ func substituteParams(tmpl *wfv1.Template, globalParams, localParams map[string]
}
replaceMap["inputs.parameters."+inParam.Name] = *inParam.Value
}
//allow {{inputs.parameters}} to fetch the entire input parameters list as JSON
jsonInputParametersBytes, err := json.Marshal(globalReplacedTmpl.Inputs.Parameters)
if err != nil {
return nil, errors.InternalWrapError(err)
}
replaceMap["inputs.parameters"] = string(jsonInputParametersBytes)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we limit the string length, I'm afraid the whole parameters will be too long for env

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually all parameters end up in the command-line anyways (usually prefixed by parameter flags). Passing them as a single argument won't significantly. change the command-line length.

for _, inArt := range globalReplacedTmpl.Inputs.Artifacts {
if inArt.Path != "" {
replaceMap["inputs.artifacts."+inArt.Name+".path"] = inArt.Path
Expand Down Expand Up @@ -356,8 +362,7 @@ func substituteParams(tmpl *wfv1.Template, globalParams, localParams map[string]

// Replace executes basic string substitution of a template with replacement values.
// allowUnresolved indicates whether or not it is acceptable to have unresolved variables
// remaining in the substituted template. prefixFilter will apply the replacements only
// to variables with the specified prefix
// remaining in the substituted template.
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) {
Expand Down
58 changes: 58 additions & 0 deletions workflow/controller/operator_test.go
Expand Up @@ -504,6 +504,64 @@ func TestSuspendResume(t *testing.T) {
assert.Equal(t, 2, len(pods.Items))
}

var inputParametersAsJson = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
name: whalesay
spec:
entrypoint: steps
arguments:
parameters:
- name: parameter1
value: value1
- name: parameter2
value: value2
templates:
- name: steps
inputs:
parameters:
- name: parameter1
- name: parameter2
steps:
- - name: step1
template: whalesay
arguments:
parameters:
- name: json
value: "{{inputs.parameters}}"

- name: whalesay
inputs:
parameters:
- name: json
container:
image: docker/whalesay:latest
command: [cowsay]
`

func TestInputParametersAsJson(t *testing.T) {
controller := newController()
wfcset := controller.wfclientset.ArgoprojV1alpha1().Workflows("")

wf := unmarshalWF(inputParametersAsJson)
wf, err := wfcset.Create(wf)
assert.Nil(t, err)
woc := newWorkflowOperationCtx(wf, controller)
woc.operate()
updatedWf, err := wfcset.Get(wf.Name, metav1.GetOptions{})
assert.Nil(t, err)
found := false
for _, node := range updatedWf.Status.Nodes {
if node.Type == wfv1.NodeTypePod {
expectedJson := `[{"name":"parameter1","value":"value1"},{"name":"parameter2","value":"value2"}]`
assert.Equal(t, expectedJson, *node.Inputs.Parameters[0].Value)
found = true
}
}
assert.Equal(t, true, found)
}

var expandWithItems = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
Expand Down
3 changes: 3 additions & 0 deletions workflow/validate/validate.go
Expand Up @@ -213,6 +213,9 @@ func validateInputs(tmpl *wfv1.Template) (map[string]interface{}, error) {
for _, param := range tmpl.Inputs.Parameters {
scope[fmt.Sprintf("inputs.parameters.%s", param.Name)] = true
}
if len(tmpl.Inputs.Parameters) > 0 {
scope["inputs.parameters"] = true
}

for _, art := range tmpl.Inputs.Artifacts {
artRef := fmt.Sprintf("inputs.artifacts.%s", art.Name)
Expand Down