Skip to content

Commit 1ee4780

Browse files
committed
Adds resource support to conditions
Allow condition to declare pipeline resources in their definitions. The resource can be accessed by the condition container during a check. Only input resources are supported. The declared resources can be specified in a PipelineTaskCondition and variable substitution for the resource is supported just like with a `TaskResource`. Signed-off-by: Dibyo Mukherjee <dibyo@google.com>
1 parent cff3043 commit 1ee4780

File tree

14 files changed

+439
-48
lines changed

14 files changed

+439
-48
lines changed

docs/conditions.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This document defines `Conditions` and their capabilities.
99
- [Syntax](#syntax)
1010
- [Check](#check)
1111
- [Parameters](#parameters)
12+
- [Resources](#resources)
1213
- [Examples](#examples)
1314

1415
## Syntax
@@ -57,10 +58,31 @@ only start with alpha characters and `_`. For example, `fooIs-Bar_` is a valid
5758
parameter name, `barIsBa$` or `0banana` are not.
5859

5960
Each declared parameter has a type field, assumed to be string if not provided by the user.
60-
The other possible type is array — useful, checking a pushed branch name doesn't match any of
61+
The other possible type is array — useful, for instance, checking that a pushed branch name doesn't match any of
6162
multiple protected branch names. When the actual parameter value is supplied, its parsed type
6263
is validated against the type field.
6364

65+
### Resources
66+
67+
Conditions can declare input [`PipelineResources`](resources.md) via the `resources` field to
68+
provide the Condition container step with data or context that is needed to perform the check.
69+
70+
Input resources, like source code (git), are dumped at path
71+
`/workspace/resource_name` within a mounted
72+
[volume](https://kubernetes.io/docs/concepts/storage/volumes/). The condition container can use the `path`
73+
[template](./tasks.md#Templating) key to refer to the local path to the mounted resource.
74+
75+
```yaml
76+
spec:
77+
resources:
78+
- name: workspace
79+
type: git
80+
check:
81+
image: alpine
82+
command: ["/bin/sh"]
83+
args: ['-c', 'test -f ${resources.workspace.path}/README.md']
84+
```
85+
6486
## Examples
6587

6688
For complete examples, see

docs/pipelines.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,12 @@ tasks:
304304
name: build-push
305305
conditions:
306306
- conditionRef: my-condition
307+
params:
308+
- name: my-param
309+
value: my-value
310+
resources:
311+
- name: workspace
312+
resource: source-repo
307313
```
308314

309315
In this example, `my-condition` refers to a [Condition](#conditions) custom resource. The `build-push`

examples/pipelineruns/conditional-pipelinerun.yaml

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
1+
#apiVersion: tekton.dev/v1alpha1
2+
#kind: Condition
3+
#metadata:
4+
# name: strings-equal
5+
#spec:
6+
# params:
7+
# - name: "x"
8+
# type: string
9+
# - name: "y"
10+
# type: string
11+
# check:
12+
# image: alpine
13+
# command: ["/bin/sh"]
14+
# args: ['-c', 'echo "Comparing ${params.x} and ${params.y}" && [ "${params.x}" == "${params.y}" ]']
15+
---
116
apiVersion: tekton.dev/v1alpha1
217
kind: Condition
318
metadata:
4-
name: strings-equal
19+
name: file-exists
520
spec:
6-
params:
7-
- name: "x"
8-
type: string
9-
- name: "y"
10-
type: string
11-
check:
21+
params:
22+
- name: "path"
23+
type: string
24+
resources:
25+
- name: workspace
26+
type: git
27+
check:
1228
image: alpine
1329
command: ["/bin/sh"]
14-
args: ['-c', 'echo "Comparing ${params.x} and ${params.y}" && [ "${params.x}" == "${params.y}" ]']
30+
args: ['-c', 'test -f ${resources.workspace.path}/${params.path}']
1531
---
1632
apiVersion: tekton.dev/v1alpha1
1733
kind: PipelineResource
@@ -49,21 +65,20 @@ spec:
4965
- name: source-repo
5066
type: git
5167
params:
52-
- name: "x"
53-
default: "abc"
54-
- name: "y"
55-
default: "abc"
68+
- name: "path"
69+
default: "README.md"
5670
tasks:
5771
- name: list-files-1
5872
taskRef:
5973
name: list-files
6074
conditions:
61-
- conditionRef: "strings-equal"
75+
- conditionRef: "file-exists"
6276
params:
63-
- name: "x"
64-
value: "${params.x}"
65-
- name: "y"
66-
value: "${params.y}"
77+
- name: "path"
78+
value: "${params.path}"
79+
resources:
80+
- name: workspace
81+
resource: source-repo
6782
resources:
6883
inputs:
6984
- name: workspace

pkg/apis/pipeline/v1alpha1/condition_types.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,26 @@ type ConditionSpec struct {
7373
// is evaluated
7474
// +optional
7575
Params []ParamSpec `json:"params,omitempty"`
76+
77+
// Resources is a list of the ConditionResources required to run the condition.
78+
// +optional
79+
Resources []ConditionResource `json:"resources,omitempty"`
80+
}
81+
82+
// ConditionResource defines an input PipelineResource declared as a requirement
83+
// by a Task. The Name field will be used to refer to these Resources within
84+
// the Condition definition and will be the path to the volume mounted containing this
85+
// Resource as an input (e.g. an Resource named `workspace` will be mounted at `/workspace`).
86+
type ConditionResource struct {
87+
// Name declares the name by which a resource is referenced in the Condition's
88+
// definition.
89+
Name string `json:"name"`
90+
// Type is the type of this resource;
91+
Type PipelineResourceType `json:"type"`
92+
// TargetPath is the path in workspace directory where the resource
93+
// will be copied.
94+
// +optional
95+
TargetPath string `json:"targetPath"`
7696
}
7797

7898
// ConditionCheck represents a single evaluation of a Condition step.

pkg/apis/pipeline/v1alpha1/pipeline_types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ type PipelineTaskCondition struct {
120120
// Params declare parameters passed to this Condition
121121
// +optional
122122
Params []Param `json:"params,omitempty"`
123+
124+
// Resources declare the resources provided to this Condition as input
125+
Resources []PipelineConditionResource `json:"resources,omitempty"`
123126
}
124127

125128
// PipelineDeclaredResource is used by a Pipeline to declare the types of the
@@ -135,6 +138,15 @@ type PipelineDeclaredResource struct {
135138
Type PipelineResourceType `json:"type"`
136139
}
137140

141+
// PipelineTaskResources allows a Pipeline to declare how its DeclaredPipelineResources
142+
// should be provided to a Condition as its inputs.
143+
type PipelineConditionResource struct {
144+
// Name is the name of the PipelineResource as declared by the Condition.
145+
Name string `json:"name"`
146+
// Resource is the name of the DeclaredPipelineResource to use.
147+
Resource string `json:"resource"`
148+
}
149+
138150
// PipelineTaskResources allows a Pipeline to declare how its DeclaredPipelineResources
139151
// should be provided to a Task as its inputs and outputs.
140152
type PipelineTaskResources struct {

pkg/apis/pipeline/v1alpha1/zz_generated.deepcopy.go

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/reconciler/v1alpha1/pipelinerun/pipelinerun.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ func getTaskRunTimeout(pr *v1alpha1.PipelineRun) *metav1.Duration {
581581
}
582582

583583
func getServiceAccount(pr *v1alpha1.PipelineRun, pipelineTaskName string) string {
584-
// If service account is configured for a given PipelineTask, override PipelineRun's seviceAccount
584+
// If service account is configured for a given PipelineTask, override PipelineRun's serviceAccount
585585
serviceAccount := pr.Spec.ServiceAccount
586586
for _, sa := range pr.Spec.ServiceAccounts {
587587
if sa.TaskName == pipelineTaskName {
@@ -626,6 +626,11 @@ func (c *Reconciler) makeConditionCheckContainer(rprt *resources.ResolvedPipelin
626626
labels := getTaskrunLabels(pr, rprt.PipelineTask.Name)
627627
labels[pipeline.GroupName+pipeline.ConditionCheckKey] = rcc.ConditionCheckName
628628

629+
taskSpec, err := rcc.ConditionToTaskSpec()
630+
if err != nil {
631+
return nil, err
632+
}
633+
629634
tr := &v1alpha1.TaskRun{
630635
ObjectMeta: metav1.ObjectMeta{
631636
Name: rcc.ConditionCheckName,
@@ -635,10 +640,11 @@ func (c *Reconciler) makeConditionCheckContainer(rprt *resources.ResolvedPipelin
635640
Annotations: getTaskrunAnnotations(pr), // Propagate annotations from PipelineRun to TaskRun.
636641
},
637642
Spec: v1alpha1.TaskRunSpec{
638-
TaskSpec: rcc.ConditionToTaskSpec(),
643+
TaskSpec: taskSpec,
639644
ServiceAccount: getServiceAccount(pr, rprt.PipelineTask.Name),
640645
Inputs: v1alpha1.TaskRunInputs{
641-
Params: rcc.PipelineTaskCondition.Params,
646+
Params: rcc.PipelineTaskCondition.Params,
647+
Resources: rcc.ToTaskResourceBindings(),
642648
},
643649
Timeout: getTaskRunTimeout(pr),
644650
NodeSelector: pr.Spec.NodeSelector,

pkg/reconciler/v1alpha1/pipelinerun/pipelinerun_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1405,7 +1405,10 @@ func makeExpectedTr(condName, ccName string) *v1alpha1.TaskRun {
14051405
tb.TaskRunLabel("tekton.dev/conditionCheck", ccName),
14061406
tb.TaskRunAnnotation("PipelineRunAnnotation", "PipelineRunValue"),
14071407
tb.TaskRunSpec(
1408-
tb.TaskRunTaskSpec(tb.Step("condition-check-"+condName, "foo", tb.Args("bar"))),
1408+
tb.TaskRunTaskSpec(
1409+
tb.Step("condition-check-"+condName, "foo", tb.Args("bar")),
1410+
tb.TaskInputs(),
1411+
),
14091412
tb.TaskRunServiceAccount("test-sa"),
14101413
),
14111414
)

0 commit comments

Comments
 (0)