Skip to content

Commit 4cec444

Browse files
committed
Add validation of embedded TaskSpec in a TaskRun
If a `TaskRun` embeddeds a Task spec (using `taskSpec` instead of `taskRef`) we should validate the `TaskSpec` too. It fixes supports validation for unmamed steps too. Signed-off-by: Vincent Demeester <vdemeest@redhat.com>
1 parent 744df2d commit 4cec444

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

pkg/apis/pipeline/v1alpha1/task_validation.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (ts *TaskSpec) Validate(ctx context.Context) *apis.FieldError {
9494

9595
// Validate task step names
9696
for _, step := range ts.Steps {
97-
if errs := validation.IsDNS1123Label(step.Name); len(errs) > 0 {
97+
if errs := validation.IsDNS1123Label(step.Name); step.Name != "" && len(errs) > 0 {
9898
return &apis.FieldError{
9999
Message: fmt.Sprintf("invalid value %q", step.Name),
100100
Paths: []string{"taskspec.steps.name"},

pkg/apis/pipeline/v1alpha1/task_validation_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ func TestTaskSpecValidate(t *testing.T) {
5959
name string
6060
fields fields
6161
}{{
62+
name: "unnamed steps",
63+
fields: fields{
64+
BuildSteps: []corev1.Container{{
65+
Image: "myimage",
66+
}, {
67+
Image: "myotherimage",
68+
}},
69+
},
70+
}, {
6271
name: "valid inputs",
6372
fields: fields{
6473
Inputs: &v1alpha1.Inputs{

pkg/apis/pipeline/v1alpha1/taskrun_validation.go

+7
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ func (ts *TaskRunSpec) Validate(ctx context.Context) *apis.FieldError {
4949
return apis.ErrMissingField("spec.taskref.name", "spec.taskspec")
5050
}
5151

52+
// Validate TaskSpec if it's present
53+
if ts.TaskSpec != nil {
54+
if err := ts.TaskSpec.Validate(ctx); err != nil {
55+
return err
56+
}
57+
}
58+
5259
// check for input resources
5360
if err := ts.Inputs.Validate(ctx, "spec.Inputs"); err != nil {
5461
return err

pkg/apis/pipeline/v1alpha1/taskrun_validation_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,22 @@ func TestTaskRunSpec_Invalidate(t *testing.T) {
113113
},
114114
wantErr: apis.ErrInvalidValue("-48h0m0s should be >= 0", "spec.timeout"),
115115
},
116+
{
117+
name: "invalid taskspec",
118+
spec: v1alpha1.TaskRunSpec{
119+
TaskSpec: &v1alpha1.TaskSpec{
120+
Steps: []corev1.Container{{
121+
Name: "invalid-name-with-$weird-char*/%",
122+
Image: "myimage",
123+
}},
124+
},
125+
},
126+
wantErr: &apis.FieldError{
127+
Message: `invalid value "invalid-name-with-$weird-char*/%"`,
128+
Paths: []string{"taskspec.steps.name"},
129+
Details: "Task step name must be a valid DNS Label, For more info refer to https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names",
130+
},
131+
},
116132
}
117133

118134
for _, ts := range tests {

0 commit comments

Comments
 (0)