Skip to content

Commit c722343

Browse files
Vincent-DeSousa-Teresotekton-robot
authored andcommitted
Update PipelineSpec Task name and TaskRef name validation to prevents errors at runtime
1 parent 7962731 commit c722343

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

docs/pipelines.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,11 @@ spec:
139139
### Pipeline Tasks
140140

141141
A `Pipeline` will execute a graph of [`Tasks`](tasks.md) (see
142-
[ordering](#ordering) for how to express this graph). At a minimum, this
143-
declaration must include a reference to the [`Task`](tasks.md):
142+
[ordering](#ordering) for how to express this graph). A valid `Pipeline`
143+
declaration must include a reference to at least one [`Task`](tasks.md). Each
144+
`Task` within a `Pipeline` must have a
145+
[valid](https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names)
146+
name and task reference, for example:
144147

145148
```yaml
146149
tasks:

pkg/apis/pipeline/v1alpha1/pipeline_validation.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ package v1alpha1
1919
import (
2020
"context"
2121
"fmt"
22+
"strings"
2223

2324
"github.com/tektoncd/pipeline/pkg/list"
2425
"golang.org/x/xerrors"
2526
"k8s.io/apimachinery/pkg/api/equality"
27+
"k8s.io/apimachinery/pkg/util/validation"
2628
"knative.dev/pkg/apis"
2729
)
2830

@@ -123,9 +125,18 @@ func (ps *PipelineSpec) Validate(ctx context.Context) *apis.FieldError {
123125

124126
// Names cannot be duplicated
125127
taskNames := map[string]struct{}{}
126-
for _, t := range ps.Tasks {
128+
for i, t := range ps.Tasks {
129+
// Task names are appended to the container name, which must exist and
130+
// must be a valid k8s name
131+
if errSlice := validation.IsQualifiedName(t.Name); len(errSlice) != 0 {
132+
return apis.ErrInvalidValue(strings.Join(errSlice, ","), fmt.Sprintf("spec.tasks[%d].name", i))
133+
}
134+
// TaskRef name must be a valid k8s name
135+
if errSlice := validation.IsQualifiedName(t.TaskRef.Name); len(errSlice) != 0 {
136+
return apis.ErrInvalidValue(strings.Join(errSlice, ","), fmt.Sprintf("spec.tasks[%d].taskRef.name", i))
137+
}
127138
if _, ok := taskNames[t.Name]; ok {
128-
return apis.ErrMultipleOneOf("spec.tasks.name")
139+
return apis.ErrMultipleOneOf(fmt.Sprintf("spec.tasks[%d].name", i))
129140
}
130141
taskNames[t.Name] = struct{}{}
131142
}

pkg/apis/pipeline/v1alpha1/pipeline_validation_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,24 @@ func TestPipeline_Validate(t *testing.T) {
6666
tb.PipelineTask("foo", "foo-task"),
6767
)),
6868
failureExpected: true,
69+
}, {
70+
name: "pipeline spec empty task name",
71+
p: tb.Pipeline("pipeline", "namespace", tb.PipelineSpec(
72+
tb.PipelineTask("", "foo-task"),
73+
)),
74+
failureExpected: true,
75+
}, {
76+
name: "pipeline spec invalid task name",
77+
p: tb.Pipeline("pipeline", "namespace", tb.PipelineSpec(
78+
tb.PipelineTask("_foo", "foo-task"),
79+
)),
80+
failureExpected: true,
81+
}, {
82+
name: "pipeline spec invalid taskref name",
83+
p: tb.Pipeline("pipeline", "namespace", tb.PipelineSpec(
84+
tb.PipelineTask("foo", "_foo-task"),
85+
)),
86+
failureExpected: true,
6987
}}
7088
for _, tt := range tests {
7189
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)