Skip to content

Commit

Permalink
[TEP-0076] Add indexing into array for params
Browse files Browse the repository at this point in the history
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]).
  • Loading branch information
Yongxuanzhang committed Jul 12, 2022
1 parent 3f01be0 commit 6366031
Show file tree
Hide file tree
Showing 9 changed files with 947 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ For instructions on using variable substitutions see the relevant section of [th
| `params.<param name>` | The value of the parameter at runtime. |
| `params['<param name>']` | (see above) |
| `params["<param name>"]` | (see above) |
| `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.|
| `params['<param name>'][i]` | (see above) |
| `params["<param name>"][i]` | (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)) |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
name: deploy
spec:
params:
- name: environments
type: array
tasks:
- name: deploy
params:
- name: environment1
value: '$(params.environments[0])'
- name: environment2
value: '$(params.environments[1])'
taskSpec:
params:
- name: environment1
type: string
- name: environment2
type: string
steps:
# this step should echo "staging"
- name: echo-params-1
image: bash:3.2
args: [
"echo",
"$(params.environment1)",
]
# this step should echo "staging"
- name: echo-params-2
image: ubuntu
script: |
#!/bin/bash
VALUE=$(params.environment2)
EXPECTED="qa"
diff=$(diff <(printf "%s\n" "${VALUE[@]}") <(printf "%s\n" "${EXPECTED[@]}"))
if [[ -z "$diff" ]]; then
echo "Get expected: ${VALUE}"
exit 0
else
echo "Want: ${EXPECTED} Got: ${VALUE}"
exit 1
fi
---
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
name: deployrun
spec:
pipelineRef:
name: deploy
params:
- name: environments
value:
- 'staging'
- 'qa'
- 'prod'
7 changes: 7 additions & 0 deletions pkg/apis/pipeline/v1beta1/param_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,13 @@ func TestArrayOrString_ApplyReplacements(t *testing.T) {
arrayReplacements: map[string][]string{"params.myarray": {"a", "b", "c"}},
},
expectedOutput: v1beta1.NewArrayOrString("a", "b", "c"),
}, {
name: "array indexing replacement on string val",
args: args{
input: v1beta1.NewArrayOrString("$(params.myarray[0])"),
stringReplacements: map[string]string{"params.myarray[0]": "a", "params.myarray[1]": "b"},
},
expectedOutput: v1beta1.NewArrayOrString("a"),
}, {
name: "object replacement on string val",
args: args{
Expand Down
9 changes: 9 additions & 0 deletions pkg/reconciler/pipelinerun/pipelinerun.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,15 @@ func (c *Reconciler) reconcile(ctx context.Context, pr *v1beta1.PipelineRun, get
return controller.NewPermanentError(err)
}

// Ensure that the array reference is not out of bound
if err := resources.ValidateParamArrayIndex(ctx, pipelineSpec, pr); err != nil {
// This Run has failed, so we need to mark it as failed and stop reconciling it
pr.Status.MarkFailed(ReasonObjectParameterMissKeys,
"PipelineRun %s/%s parameters is missing object keys required by Pipeline %s/%s's parameters: %s",
pr.Namespace, pr.Name, pr.Namespace, pipelineMeta.Name, err)
return controller.NewPermanentError(err)
}

// Ensure that the workspaces expected by the Pipeline are provided by the PipelineRun.
if err := resources.ValidateWorkspaceBindings(pipelineSpec, pr); err != nil {
pr.Status.MarkFailed(ReasonInvalidWorkspaceBinding,
Expand Down
13 changes: 13 additions & 0 deletions pkg/reconciler/pipelinerun/resources/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func ApplyParameters(ctx context.Context, p *v1beta1.PipelineSpec, pr *v1beta1.P
stringReplacements := map[string]string{}
arrayReplacements := map[string][]string{}
objectReplacements := map[string]map[string]string{}
cfg := config.FromContextOrDefaults(ctx)

patterns := []string{
"params.%s",
Expand All @@ -55,6 +56,12 @@ func ApplyParameters(ctx context.Context, p *v1beta1.PipelineSpec, pr *v1beta1.P
switch p.Default.Type {
case v1beta1.ParamTypeArray:
for _, pattern := range patterns {
// array indexing for param is alpha feature
if cfg.FeatureFlags.EnableAPIFields == config.AlphaAPIFields {
for i := 0; i < len(p.Default.ArrayVal); i++ {
stringReplacements[fmt.Sprintf(pattern+"[%d]", p.Name, i)] = p.Default.ArrayVal[i]
}
}
arrayReplacements[fmt.Sprintf(pattern, p.Name)] = p.Default.ArrayVal
}
case v1beta1.ParamTypeObject:
Expand All @@ -76,6 +83,12 @@ func ApplyParameters(ctx context.Context, p *v1beta1.PipelineSpec, pr *v1beta1.P
switch p.Value.Type {
case v1beta1.ParamTypeArray:
for _, pattern := range patterns {
// array indexing for param is alpha feature
if cfg.FeatureFlags.EnableAPIFields == config.AlphaAPIFields {
for i := 0; i < len(p.Value.ArrayVal); i++ {
stringReplacements[fmt.Sprintf(pattern+"[%d]", p.Name, i)] = p.Value.ArrayVal[i]
}
}
arrayReplacements[fmt.Sprintf(pattern, p.Name)] = p.Value.ArrayVal
}
case v1beta1.ParamTypeObject:
Expand Down
Loading

0 comments on commit 6366031

Please sign in to comment.