Skip to content

Commit

Permalink
Support for single-quote bracket notation for params
Browse files Browse the repository at this point in the history
This change adds single-quote bracket notation to the work done in #4215. This is consistent with how referencing is done else in K8s in the downwards api.

The original patch used the name subscript notation to describe this however the standard name for this approach is bracket notation and updates the doc accordingly.
  • Loading branch information
skaegi authored and tekton-robot committed Oct 5, 2021
1 parent 00b55df commit 72388ca
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 9 deletions.
4 changes: 3 additions & 1 deletion docs/tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -796,8 +796,10 @@ variable values as follows:

- To reference a parameter in a `Task`, use the following syntax, where `<name>` is the name of the parameter:
```shell
# dot notation
$(params.<name>)
# or subscript form:
# or bracket notation (wrapping <name> with either single or double quotes):
$(params['<name>'])
$(params["<name>"])
```
- To access parameter values from resources, see [variable substitution](resources.md#variable-substitution)
Expand Down
4 changes: 4 additions & 0 deletions docs/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ For instructions on using variable substitutions see the relevant section of [th
| Variable | Description |
| -------- | ----------- |
| `params.<param name>` | The value of the parameter at runtime. |
| `params['<param name>']` | (see above) |
| `params["<param name>"]` | (see above) |
| `tasks.<taskName>.results.<resultName>` | The value of the `Task's` result. Can alter `Task` execution order within a `Pipeline`.) |
| `tasks.<taskName>.results['<resultName>']` | (see above)) |
| `tasks.<taskName>.results["<resultName>"]` | (see above)) |
| `workspaces.<workspaceName>.bound` | Whether a `Workspace` has been bound or not. "false" if the `Workspace` declaration has `optional: true` and the Workspace binding was omitted by the PipelineRun. |
| `context.pipelineRun.name` | The name of the `PipelineRun` that this `Pipeline` is running in. |
Expand All @@ -34,10 +36,12 @@ For instructions on using variable substitutions see the relevant section of [th
| Variable | Description |
| -------- | ----------- |
| `params.<param name>` | The value of the parameter at runtime. |
| `params['<param name>']` | (see above) |
| `params["<param name>"]` | (see above) |
| `resources.inputs.<resourceName>.path` | The path to the input resource's directory. |
| `resources.outputs.<resourceName>.path` | The path to the output resource's directory. |
| `results.<resultName>.path` | The path to the file where the `Task` writes its results data. |
| `results['<resultName>'].path` | (see above) |
| `results["<resultName>"].path` | (see above) |
| `workspaces.<workspaceName>.path` | The path to the mounted `Workspace`. Empty string if an optional `Workspace` has not been provided by the TaskRun. |
| `workspaces.<workspaceName>.bound` | Whether a `Workspace` has been bound or not. "false" if an optional`Workspace` has not been provided by the TaskRun. |
Expand Down
1 change: 1 addition & 0 deletions pkg/reconciler/pipelinerun/resources/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func ApplyParameters(p *v1beta1.PipelineSpec, pr *v1beta1.PipelineRun) *v1beta1.
patterns := []string{
"params.%s",
"params[%q]",
"params['%s']",
}

// Set all the default stringReplacements
Expand Down
19 changes: 15 additions & 4 deletions pkg/reconciler/pipelinerun/resources/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,31 +272,42 @@ func TestApplyParameters(t *testing.T) {
}},
},
}, {
name: "parameter references with subscript notation and special characters",
name: "parameter references with bracket notation and special characters",
original: v1beta1.PipelineSpec{
Params: []v1beta1.ParamSpec{
{Name: "first.param", Type: v1beta1.ParamTypeString, Default: v1beta1.NewArrayOrString("default-value")},
{Name: "second/param", Type: v1beta1.ParamTypeString},
{Name: "third.param", Type: v1beta1.ParamTypeString, Default: v1beta1.NewArrayOrString("default-value")},
{Name: "fourth/param", Type: v1beta1.ParamTypeString},
},
Tasks: []v1beta1.PipelineTask{{
Params: []v1beta1.Param{
{Name: "first-task-first-param", Value: *v1beta1.NewArrayOrString(`$(params["first.param"])`)},
{Name: "first-task-second-param", Value: *v1beta1.NewArrayOrString(`$(params["second/param"])`)},
{Name: "first-task-third-param", Value: *v1beta1.NewArrayOrString("static value")},
{Name: "first-task-third-param", Value: *v1beta1.NewArrayOrString(`$(params['third.param'])`)},
{Name: "first-task-fourth-param", Value: *v1beta1.NewArrayOrString(`$(params['fourth/param'])`)},
{Name: "first-task-fifth-param", Value: *v1beta1.NewArrayOrString("static value")},
},
}},
},
params: []v1beta1.Param{{Name: "second/param", Value: *v1beta1.NewArrayOrString("second-value")}},
params: []v1beta1.Param{
{Name: "second/param", Value: *v1beta1.NewArrayOrString("second-value")},
{Name: "fourth/param", Value: *v1beta1.NewArrayOrString("fourth-value")},
},
expected: v1beta1.PipelineSpec{
Params: []v1beta1.ParamSpec{
{Name: "first.param", Type: v1beta1.ParamTypeString, Default: v1beta1.NewArrayOrString("default-value")},
{Name: "second/param", Type: v1beta1.ParamTypeString},
{Name: "third.param", Type: v1beta1.ParamTypeString, Default: v1beta1.NewArrayOrString("default-value")},
{Name: "fourth/param", Type: v1beta1.ParamTypeString},
},
Tasks: []v1beta1.PipelineTask{{
Params: []v1beta1.Param{
{Name: "first-task-first-param", Value: *v1beta1.NewArrayOrString("default-value")},
{Name: "first-task-second-param", Value: *v1beta1.NewArrayOrString("second-value")},
{Name: "first-task-third-param", Value: *v1beta1.NewArrayOrString("static value")},
{Name: "first-task-third-param", Value: *v1beta1.NewArrayOrString("default-value")},
{Name: "first-task-fourth-param", Value: *v1beta1.NewArrayOrString("fourth-value")},
{Name: "first-task-fifth-param", Value: *v1beta1.NewArrayOrString("static value")},
},
}},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,5 +196,6 @@ func (r *ResolvedResultRef) getReplaceTarget() []string {
return []string{
fmt.Sprintf("%s.%s.%s.%s", v1beta1.ResultTaskPart, r.ResultReference.PipelineTask, v1beta1.ResultResultPart, r.ResultReference.Result),
fmt.Sprintf("%s.%s.%s[%q]", v1beta1.ResultTaskPart, r.ResultReference.PipelineTask, v1beta1.ResultResultPart, r.ResultReference.Result),
fmt.Sprintf("%s.%s.%s['%s']", v1beta1.ResultTaskPart, r.ResultReference.PipelineTask, v1beta1.ResultResultPart, r.ResultReference.Result),
}
}
2 changes: 2 additions & 0 deletions pkg/reconciler/taskrun/resources/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func ApplyParameters(spec *v1beta1.TaskSpec, tr *v1beta1.TaskRun, defaults ...v1
patterns := []string{
"params.%s",
"params[%q]",
"params['%s']",
// FIXME(vdemeester) Remove that with deprecating v1beta1
"inputs.params.%s",
}
Expand Down Expand Up @@ -203,6 +204,7 @@ func ApplyTaskResults(spec *v1beta1.TaskSpec) *v1beta1.TaskSpec {
patterns := []string{
"results.%s.path",
"results[%q].path",
"results['%s'].path",
}

for _, result := range spec.Results {
Expand Down
9 changes: 8 additions & 1 deletion pkg/reconciler/taskrun/resources/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var (
Image: `$(params["myimage"])`,
Env: []corev1.EnvVar{{
Name: "foo",
Value: "$(params.FOO)",
Value: "$(params['FOO'])",
}},
},
}},
Expand Down Expand Up @@ -1164,12 +1164,19 @@ func TestTaskResults(t *testing.T) {
Image: "bash:latest",
},
Script: "#!/usr/bin/env bash\ndate | tee $(results.current-date-human-readable.path)",
}, {
Container: corev1.Container{
Name: "print-date-human-readable-again",
Image: "bash:latest",
},
Script: "#!/usr/bin/env bash\ndate | tee $(results['current-date-human-readable'].path)",
}},
}
want := applyMutation(ts, func(spec *v1beta1.TaskSpec) {
spec.Steps[0].Script = "#!/usr/bin/env bash\ndate +%s | tee /tekton/results/current.date.unix.timestamp"
spec.Steps[0].Args[0] = "/tekton/results/current.date.unix.timestamp"
spec.Steps[1].Script = "#!/usr/bin/env bash\ndate | tee /tekton/results/current-date-human-readable"
spec.Steps[2].Script = "#!/usr/bin/env bash\ndate | tee /tekton/results/current-date-human-readable"
})
got := resources.ApplyTaskResults(ts)
if d := cmp.Diff(want, got); d != "" {
Expand Down
6 changes: 3 additions & 3 deletions test/pipelinerun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@ func TestPipelineRun(t *testing.T) {
Params: []v1beta1.ParamSpec{{
Name: "the.path", Type: v1beta1.ParamTypeString,
}, {
Name: "dest", Type: v1beta1.ParamTypeString,
Name: "the.dest", Type: v1beta1.ParamTypeString,
}},
Steps: []v1beta1.Step{{
Container: corev1.Container{
Name: "config-docker",
Image: "gcr.io/tekton-releases/dogfooding/skopeo:latest",
Command: []string{"skopeo"},
Args: []string{"copy", `$(params["the.path"])`, "$(params.dest)"},
Args: []string{"copy", `$(params["the.path"])`, "$(params['the.dest'])"},
}},
},
},
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestPipelineRun(t *testing.T) {
Name: "config-docker",
Image: "gcr.io/tekton-releases/dogfooding/skopeo:latest",
Command: []string{"skopeo"},
Args: []string{"copy", `$(params["the.path"])`, `$(params["dest"])`},
Args: []string{"copy", `$(params["the.path"])`, `$(params['dest'])`},
}},
},
},
Expand Down

0 comments on commit 72388ca

Please sign in to comment.