Skip to content

Commit b5775fd

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 79e591b commit b5775fd

File tree

9 files changed

+662
-8
lines changed

9 files changed

+662
-8
lines changed

config/config-feature-flags.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ data:
6969
enable-custom-tasks: "false"
7070
# Setting this flag will determine which gated features are enabled.
7171
# Acceptable values are "stable" or "alpha".
72-
enable-api-fields: "stable"
72+
enable-api-fields: "alpha"
7373
# Setting this flag to "true" enables CloudEvents for Runs, as long as a
7474
# CloudEvents sink is configured in the config-defaults config map
7575
send-cloudevents-for-runs: "false"

docs/variables.md

Lines changed: 6 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 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)) |
@@ -38,6 +41,9 @@ For instructions on using variable substitutions see the relevant section of [th
3841
| `params.<param name>` | The value of the parameter at runtime. |
3942
| `params['<param name>']` | (see above) |
4043
| `params["<param name>"]` | (see above) |
44+
| `params.<param name>[i]` | Get the i element of param array. This is alpha feature, set `enable-api-fields` to `alpha` to use it.|
45+
| `params['<param name>'][i]` | (see above) |
46+
| `params["<param name>"][i]` | (see above) |
4147
| `resources.inputs.<resourceName>.path` | The path to the input resource's directory. |
4248
| `resources.outputs.<resourceName>.path` | The path to the output resource's directory. |
4349
| `results.<resultName>.path` | The path to the file where the `Task` writes its results data. |
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
apiVersion: tekton.dev/v1beta1
2+
kind: TaskRun
3+
metadata:
4+
generateName: params-array-indexing-
5+
spec:
6+
params:
7+
- name: array-to-echo
8+
value:
9+
- "foo"
10+
- "bar"
11+
taskSpec:
12+
params:
13+
- name: array-to-echo
14+
type: array
15+
steps:
16+
# this step should echo "foo"
17+
- name: echo-params-1
18+
image: bash:3.2
19+
args: [
20+
"echo",
21+
"$(params.array-to-echo[0])",
22+
]
23+
# this step should echo "bar"
24+
- name: echo-params-2
25+
image: bash:3.2
26+
script: "echo $(params.array-to-echo[1])"

pkg/reconciler/pipelinerun/pipelinerun.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ func (c *Reconciler) reconcile(ctx context.Context, pr *v1beta1.PipelineRun, get
454454
}
455455

456456
// Apply parameter substitution from the PipelineRun
457-
pipelineSpec = resources.ApplyParameters(pipelineSpec, pr)
457+
pipelineSpec = resources.ApplyParameters(ctx, pipelineSpec, pr)
458458
pipelineSpec = resources.ApplyContexts(pipelineSpec, pipelineMeta.Name, pr)
459459
pipelineSpec = resources.ApplyWorkspaces(pipelineSpec, pr)
460460

pkg/reconciler/pipelinerun/resources/apply.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,27 @@ limitations under the License.
1717
package resources
1818

1919
import (
20+
"context"
2021
"fmt"
2122
"strconv"
2223
"strings"
2324

25+
"github.com/tektoncd/pipeline/pkg/apis/config"
2426
"github.com/tektoncd/pipeline/pkg/apis/run/v1alpha1"
2527

2628
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
2729
"github.com/tektoncd/pipeline/pkg/substitution"
2830
)
2931

3032
// ApplyParameters applies the params from a PipelineRun.Params to a PipelineSpec.
31-
func ApplyParameters(p *v1beta1.PipelineSpec, pr *v1beta1.PipelineRun) *v1beta1.PipelineSpec {
33+
func ApplyParameters(ctx context.Context, p *v1beta1.PipelineSpec, pr *v1beta1.PipelineRun) *v1beta1.PipelineSpec {
3234
// This assumes that the PipelineRun inputs have been validated against what the Pipeline requests.
3335

3436
// stringReplacements is used for standard single-string stringReplacements, while arrayReplacements contains arrays
3537
// that need to be further processed.
3638
stringReplacements := map[string]string{}
3739
arrayReplacements := map[string][]string{}
40+
cfg := config.FromContextOrDefaults(ctx)
3841

3942
patterns := []string{
4043
"params.%s",
@@ -51,6 +54,13 @@ func ApplyParameters(p *v1beta1.PipelineSpec, pr *v1beta1.PipelineRun) *v1beta1.
5154
}
5255
} else {
5356
for _, pattern := range patterns {
57+
// array indexing for param is alpha feature
58+
// TODO(#4723): Validate the array reference is not our of bound
59+
if cfg.FeatureFlags.EnableAPIFields == config.AlphaAPIFields {
60+
for i := 0; i < len(p.Default.ArrayVal); i++ {
61+
stringReplacements[fmt.Sprintf(pattern+"[%d]", p.Name, i)] = p.Default.ArrayVal[i]
62+
}
63+
}
5464
arrayReplacements[fmt.Sprintf(pattern, p.Name)] = p.Default.ArrayVal
5565
}
5666
}
@@ -64,6 +74,12 @@ func ApplyParameters(p *v1beta1.PipelineSpec, pr *v1beta1.PipelineRun) *v1beta1.
6474
}
6575
} else {
6676
for _, pattern := range patterns {
77+
// array indexing for param is alpha feature
78+
if cfg.FeatureFlags.EnableAPIFields == config.AlphaAPIFields {
79+
for i := 0; i < len(p.Value.ArrayVal); i++ {
80+
stringReplacements[fmt.Sprintf(pattern+"[%d]", p.Name, i)] = p.Value.ArrayVal[i]
81+
}
82+
}
6783
arrayReplacements[fmt.Sprintf(pattern, p.Name)] = p.Value.ArrayVal
6884
}
6985
}

0 commit comments

Comments
 (0)