Skip to content

Commit

Permalink
Propagate params in pipelines
Browse files Browse the repository at this point in the history
Prior to this, we allowed parameter propagation in an inlined
pipelinerun. However, within a pipeline, we requrie a verbose spec.
This was an oversight as indicated in
tektoncd#7901.
This PR fixes that issue by updating the validation logic in the
webhook.

Fixes tektoncd#7901.

Propagate params in pipelines

Prior to this, we allowed parameter propagation in an inlined
pipelinerun. However, within a pipeline, we requrie a verbose spec.
This was an oversight as indicated in
tektoncd#7901.
This PR fixes that issue by updating the validation logic in the
webhook.

Fixes tektoncd#7901.
  • Loading branch information
chitrangpatel committed May 13, 2024
1 parent 59794e3 commit ebd497d
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 4 deletions.
39 changes: 39 additions & 0 deletions docs/pipelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,45 @@ any resolved `param` value against the `enum` specified in each `PipelineTask` b

See usage in this [example](../examples/v1/pipelineruns/alpha/param-enum.yaml)

#### Propagated Params

Like with embedded [pipelineruns](pipelineruns.md#propagated-parameters), you can propagate `params` declared in the `pipeline` down to the inlined `pipelineTasks` and its inlined `Steps`. Wherever a resource (e.g. a `pipelineTask`) or a `StepAction` is referenced, the parameters need to be passed explicitly.

For example, the following is a valid yaml.

```yaml
apiVersion: tekton.dev/v1 # or tekton.dev/v1beta1
kind: Pipeline
metadata:
name: pipelien-propagated-params
spec:
params:
- name: HELLO
default: "Hello World!"
- name: BYE
default: "Bye World!"
tasks:
- name: echo-hello
taskSpec:
steps:
- name: echo
image: ubuntu
script: |
#!/usr/bin/env bash
echo "$(params.HELLO)"
- name: echo-bye
taskSpec:
steps:
- name: echo-action
ref:
name: step-action-echo
params:
- name: msg
value: "$(params.BYE)"
```
The same rules defined in [pipelineruns](pipelineruns.md#propagated-parameters) apply here.


## Adding `Tasks` to the `Pipeline`

Your `Pipeline` definition must reference at least one [`Task`](tasks.md).
Expand Down
34 changes: 34 additions & 0 deletions examples/v1/pipelineruns/propagating-params-in-pipelines.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
name: param-example
spec:
params:
- name: p1
- name: p2
tasks:
- name: t1
taskSpec:
steps:
- image: ubuntu
command: ["echo"]
args: ["$(params.p1)"]
- name: t2
taskSpec:
steps:
- image: ubuntu
command: ["echo"]
args: ["$(params.p2)"]
---
apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
name: param-example-run
spec:
pipelineRef:
name: param-example
params:
- name: p1
value: foo
- name: p2
value: bar
32 changes: 32 additions & 0 deletions examples/v1/pipelineruns/propagating-workspaces-in-pipelines.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
name: workspace-example
spec:
workspaces:
- name: shared-data
tasks:
- name: t1
taskSpec:
steps:
- image: ubuntu
command: ["ls"]
args: ["$(workspaces.shared-data.path)"]
---
apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
name: workspace-example-run
spec:
pipelineRef:
name: workspace-example
workspaces:
- name: shared-data
volumeClaimTemplate:
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 16Mi
volumeMode: Filesystem
37 changes: 37 additions & 0 deletions examples/v1/pipelineruns/propagating_params_in_pipeline.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
name: propagating-params-in-pipeline
spec:
params:
- name: HELLO
default: "Pipeline Hello World!"
tasks:
- name: echo-hello
taskSpec:
steps:
- name: echo
image: ubuntu
script: |
#!/usr/bin/env bash
echo "$(params.HELLO)"
finally:
- name: echo-hello-finally
taskSpec:
steps:
- name: echo
image: ubuntu
script: |
#!/usr/bin/env bash
echo "And Finally ... $(params.HELLO)"
---
apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
generateName: propagating-params-in-pipeline-
spec:
params:
- name: HELLO
value: "Hello from pipeline run"
pipelineRef:
name: propagating-params-in-pipeline
8 changes: 4 additions & 4 deletions pkg/apis/pipeline/v1/pipeline_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ func (l PipelineTaskList) Validate(ctx context.Context, taskNames sets.String, p
}

// validateUsageOfDeclaredPipelineTaskParameters validates that all parameters referenced in the pipeline Task are declared by the pipeline Task.
func (l PipelineTaskList) validateUsageOfDeclaredPipelineTaskParameters(ctx context.Context, path string) (errs *apis.FieldError) {
func (l PipelineTaskList) validateUsageOfDeclaredPipelineTaskParameters(ctx context.Context, additionalParams []ParamSpec, path string) (errs *apis.FieldError) {
for i, t := range l {
if t.TaskSpec != nil {
errs = errs.Also(ValidateUsageOfDeclaredParameters(ctx, t.TaskSpec.Steps, t.TaskSpec.Params).ViaFieldIndex(path, i))
errs = errs.Also(ValidateUsageOfDeclaredParameters(ctx, t.TaskSpec.Steps, append(t.TaskSpec.Params, additionalParams...)).ViaFieldIndex(path, i))
}
}
return errs
Expand Down Expand Up @@ -385,8 +385,8 @@ func validatePipelineWorkspacesDeclarations(wss []PipelineWorkspaceDeclaration)

// validatePipelineParameterUsage validates that parameters referenced in the Pipeline are declared by the Pipeline
func (ps *PipelineSpec) validatePipelineParameterUsage(ctx context.Context) (errs *apis.FieldError) {
errs = errs.Also(PipelineTaskList(ps.Tasks).validateUsageOfDeclaredPipelineTaskParameters(ctx, "tasks"))
errs = errs.Also(PipelineTaskList(ps.Finally).validateUsageOfDeclaredPipelineTaskParameters(ctx, "finally"))
errs = errs.Also(PipelineTaskList(ps.Tasks).validateUsageOfDeclaredPipelineTaskParameters(ctx, ps.Params, "tasks"))
errs = errs.Also(PipelineTaskList(ps.Finally).validateUsageOfDeclaredPipelineTaskParameters(ctx, ps.Params, "finally"))
errs = errs.Also(validatePipelineTaskParameterUsage(ps.Tasks, ps.Params).ViaField("tasks"))
errs = errs.Also(validatePipelineTaskParameterUsage(ps.Finally, ps.Params).ViaField("finally"))
return errs
Expand Down

0 comments on commit ebd497d

Please sign in to comment.