Skip to content

Commit 1a13076

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 53c936b commit 1a13076

File tree

14 files changed

+436
-45
lines changed

14 files changed

+436
-45
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
@@ -306,6 +306,12 @@ tasks:
306306
name: build-push
307307
conditions:
308308
- conditionRef: my-condition
309+
params:
310+
- name: my-param
311+
value: my-value
312+
resources:
313+
- name: workspace
314+
resource: source-repo
309315
```
310316

311317
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
@@ -580,7 +580,7 @@ func getTaskRunTimeout(pr *v1alpha1.PipelineRun) *metav1.Duration {
580580
}
581581

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

628+
taskSpec, err := rcc.ConditionToTaskSpec()
629+
if err != nil {
630+
return nil, err
631+
}
632+
628633
tr := &v1alpha1.TaskRun{
629634
ObjectMeta: metav1.ObjectMeta{
630635
Name: rcc.ConditionCheckName,
@@ -634,10 +639,11 @@ func (c *Reconciler) makeConditionCheckContainer(rprt *resources.ResolvedPipelin
634639
Annotations: getTaskrunAnnotations(pr), // Propagate annotations from PipelineRun to TaskRun.
635640
},
636641
Spec: v1alpha1.TaskRunSpec{
637-
TaskSpec: rcc.ConditionToTaskSpec(),
642+
TaskSpec: taskSpec,
638643
ServiceAccount: getServiceAccount(pr, rprt.PipelineTask.Name),
639644
Inputs: v1alpha1.TaskRunInputs{
640-
Params: rcc.PipelineTaskCondition.Params,
645+
Params: rcc.PipelineTaskCondition.Params,
646+
Resources: rcc.ToTaskResourceBindings(),
641647
},
642648
Timeout: getTaskRunTimeout(pr),
643649
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
@@ -1421,7 +1421,10 @@ func makeExpectedTr(condName, ccName string) *v1alpha1.TaskRun {
14211421
tb.TaskRunLabel("tekton.dev/conditionCheck", ccName),
14221422
tb.TaskRunAnnotation("PipelineRunAnnotation", "PipelineRunValue"),
14231423
tb.TaskRunSpec(
1424-
tb.TaskRunTaskSpec(tb.Step("condition-check-"+condName, "foo", tb.StepArgs("bar"))),
1424+
tb.TaskRunTaskSpec(
1425+
tb.Step("condition-check-"+condName, "foo", tb.StepArgs("bar")),
1426+
tb.TaskInputs(),
1427+
),
14251428
tb.TaskRunServiceAccount("test-sa"),
14261429
),
14271430
)

0 commit comments

Comments
 (0)