Skip to content

Commit 3a5b98e

Browse files
committed
[TEP-0076] Add indexing into array for params
This commit provides the indexing into array for params and gated by alpha feature flag. Before this commit we can only refer to the whole array param, with this feature we can refer to array's element such as $(params.param-name[0]).
1 parent 3f01be0 commit 3a5b98e

File tree

9 files changed

+943
-1
lines changed

9 files changed

+943
-1
lines changed

docs/variables.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ For instructions on using variable substitutions see the relevant section of [th
1919
| `params.<param name>` | The value of the parameter at runtime. |
2020
| `params['<param name>']` | (see above) |
2121
| `params["<param name>"]` | (see above) |
22+
| `params.<param name>[i]` | Get the i-th element of param array. This is alpha feature, set `enable-api-fields` to `alpha` to use it.|
23+
| `params['<param name>'][i]` | (see above) |
24+
| `params["<param name>"][i]` | (see above) |
2225
| `tasks.<taskName>.results.<resultName>` | The value of the `Task's` result. Can alter `Task` execution order within a `Pipeline`.) |
2326
| `tasks.<taskName>.results['<resultName>']` | (see above)) |
2427
| `tasks.<taskName>.results["<resultName>"]` | (see above)) |
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
apiVersion: tekton.dev/v1beta1
2+
kind: Pipeline
3+
metadata:
4+
name: deploy
5+
spec:
6+
params:
7+
- name: environments
8+
type: array
9+
tasks:
10+
- name: deploy
11+
params:
12+
- name: environment1
13+
value: '$(params.environments[0])'
14+
- name: environment2
15+
value: '$(params.environments[1])'
16+
taskSpec:
17+
params:
18+
- name: environment1
19+
type: string
20+
- name: environment2
21+
type: string
22+
steps:
23+
# this step should echo "staging"
24+
- name: echo-params-1
25+
image: bash:3.2
26+
args: [
27+
"echo",
28+
"$(params.environment1)",
29+
]
30+
# this step should echo "staging"
31+
- name: echo-params-2
32+
image: ubuntu
33+
script: |
34+
#!/bin/bash
35+
VALUE=$(params.environment2)
36+
EXPECTED="qa"
37+
diff=$(diff <(printf "%s\n" "${VALUE[@]}") <(printf "%s\n" "${EXPECTED[@]}"))
38+
if [[ -z "$diff" ]]; then
39+
echo "Get expected: ${VALUE}"
40+
exit 0
41+
else
42+
echo "Want: ${EXPECTED} Got: ${VALUE}"
43+
exit 1
44+
fi
45+
---
46+
apiVersion: tekton.dev/v1beta1
47+
kind: PipelineRun
48+
metadata:
49+
name: deployrun
50+
spec:
51+
pipelineRef:
52+
name: deploy
53+
params:
54+
- name: environments
55+
value:
56+
- 'staging'
57+
- 'qa'
58+
- 'prod'

pkg/apis/pipeline/v1beta1/param_types_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,13 @@ func TestArrayOrString_ApplyReplacements(t *testing.T) {
230230
arrayReplacements: map[string][]string{"params.myarray": {"a", "b", "c"}},
231231
},
232232
expectedOutput: v1beta1.NewArrayOrString("a", "b", "c"),
233+
}, {
234+
name: "array indexing replacement on string val",
235+
args: args{
236+
input: v1beta1.NewArrayOrString("$(params.myarray[0])"),
237+
stringReplacements: map[string]string{"params.myarray[0]": "a", "params.myarray[1]": "b"},
238+
},
239+
expectedOutput: v1beta1.NewArrayOrString("a"),
233240
}, {
234241
name: "object replacement on string val",
235242
args: args{

pkg/reconciler/pipelinerun/pipelinerun.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,15 @@ func (c *Reconciler) reconcile(ctx context.Context, pr *v1beta1.PipelineRun, get
447447
return controller.NewPermanentError(err)
448448
}
449449

450+
// Ensure that the array reference is not out of bound
451+
if err := resources.ValidateParamArrayIndex(ctx, pipelineSpec, pr); err != nil {
452+
// This Run has failed, so we need to mark it as failed and stop reconciling it
453+
pr.Status.MarkFailed(ReasonObjectParameterMissKeys,
454+
"PipelineRun %s/%s parameters is missing object keys required by Pipeline %s/%s's parameters: %s",
455+
pr.Namespace, pr.Name, pr.Namespace, pipelineMeta.Name, err)
456+
return controller.NewPermanentError(err)
457+
}
458+
450459
// Ensure that the workspaces expected by the Pipeline are provided by the PipelineRun.
451460
if err := resources.ValidateWorkspaceBindings(pipelineSpec, pr); err != nil {
452461
pr.Status.MarkFailed(ReasonInvalidWorkspaceBinding,

pkg/reconciler/pipelinerun/resources/apply.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func ApplyParameters(ctx context.Context, p *v1beta1.PipelineSpec, pr *v1beta1.P
3939
stringReplacements := map[string]string{}
4040
arrayReplacements := map[string][]string{}
4141
objectReplacements := map[string]map[string]string{}
42+
cfg := config.FromContextOrDefaults(ctx)
4243

4344
patterns := []string{
4445
"params.%s",
@@ -55,6 +56,12 @@ func ApplyParameters(ctx context.Context, p *v1beta1.PipelineSpec, pr *v1beta1.P
5556
switch p.Default.Type {
5657
case v1beta1.ParamTypeArray:
5758
for _, pattern := range patterns {
59+
// array indexing for param is alpha feature
60+
if cfg.FeatureFlags.EnableAPIFields == config.AlphaAPIFields {
61+
for i := 0; i < len(p.Default.ArrayVal); i++ {
62+
stringReplacements[fmt.Sprintf(pattern+"[%d]", p.Name, i)] = p.Default.ArrayVal[i]
63+
}
64+
}
5865
arrayReplacements[fmt.Sprintf(pattern, p.Name)] = p.Default.ArrayVal
5966
}
6067
case v1beta1.ParamTypeObject:
@@ -76,6 +83,12 @@ func ApplyParameters(ctx context.Context, p *v1beta1.PipelineSpec, pr *v1beta1.P
7683
switch p.Value.Type {
7784
case v1beta1.ParamTypeArray:
7885
for _, pattern := range patterns {
86+
// array indexing for param is alpha feature
87+
if cfg.FeatureFlags.EnableAPIFields == config.AlphaAPIFields {
88+
for i := 0; i < len(p.Value.ArrayVal); i++ {
89+
stringReplacements[fmt.Sprintf(pattern+"[%d]", p.Name, i)] = p.Value.ArrayVal[i]
90+
}
91+
}
7992
arrayReplacements[fmt.Sprintf(pattern, p.Name)] = p.Value.ArrayVal
8093
}
8194
case v1beta1.ParamTypeObject:

0 commit comments

Comments
 (0)