-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
taskrun_validation.go
165 lines (146 loc) · 5.3 KB
/
taskrun_validation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
Copyright 2019 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1beta1
import (
"context"
"fmt"
"strings"
"github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/apis/validate"
"k8s.io/apimachinery/pkg/util/sets"
"knative.dev/pkg/apis"
)
var _ apis.Validatable = (*TaskRun)(nil)
// Validate taskrun
func (tr *TaskRun) Validate(ctx context.Context) *apis.FieldError {
errs := validate.ObjectMetadata(tr.GetObjectMeta()).ViaField("metadata")
if apis.IsInDelete(ctx) {
return nil
}
return errs.Also(tr.Spec.Validate(apis.WithinSpec(ctx)).ViaField("spec"))
}
// Validate taskrun spec
func (ts *TaskRunSpec) Validate(ctx context.Context) (errs *apis.FieldError) {
// Must have exactly one of taskRef and taskSpec.
if ts.TaskRef == nil && ts.TaskSpec == nil {
errs = errs.Also(apis.ErrMissingOneOf("taskRef", "taskSpec"))
}
if ts.TaskRef != nil && ts.TaskSpec != nil {
errs = errs.Also(apis.ErrMultipleOneOf("taskRef", "taskSpec"))
}
// Validate TaskRef if it's present.
if ts.TaskRef != nil {
errs = errs.Also(ts.TaskRef.Validate(ctx).ViaField("taskRef"))
}
// Validate TaskSpec if it's present.
if ts.TaskSpec != nil {
errs = errs.Also(ts.TaskSpec.Validate(ctx).ViaField("taskSpec"))
}
errs = errs.Also(validateParameters(ts.Params).ViaField("params"))
errs = errs.Also(validateWorkspaceBindings(ctx, ts.Workspaces).ViaField("workspaces"))
errs = errs.Also(ts.Resources.Validate(ctx).ViaField("resources"))
if ts.Debug != nil {
errs = errs.Also(ValidateEnabledAPIFields(ctx, "debug", config.AlphaAPIFields).ViaField("debug"))
errs = errs.Also(validateDebug(ts.Debug).ViaField("debug"))
}
if ts.StepOverrides != nil {
errs = errs.Also(ValidateEnabledAPIFields(ctx, "stepOverrides", config.AlphaAPIFields).ViaField("stepOverrides"))
errs = errs.Also(validateStepOverrides(ts.StepOverrides).ViaField("stepOverrides"))
}
if ts.SidecarOverrides != nil {
errs = errs.Also(ValidateEnabledAPIFields(ctx, "sidecarOverrides", config.AlphaAPIFields).ViaField("sidecarOverrides"))
errs = errs.Also(validateSidecarOverrides(ts.SidecarOverrides).ViaField("sidecarOverrides"))
}
if ts.Status != "" {
if ts.Status != TaskRunSpecStatusCancelled {
errs = errs.Also(apis.ErrInvalidValue(fmt.Sprintf("%s should be %s", ts.Status, TaskRunSpecStatusCancelled), "status"))
}
}
if ts.Timeout != nil {
// timeout should be a valid duration of at least 0.
if ts.Timeout.Duration < 0 {
errs = errs.Also(apis.ErrInvalidValue(fmt.Sprintf("%s should be >= 0", ts.Timeout.Duration.String()), "timeout"))
}
}
return errs
}
// validateDebug
func validateDebug(db *TaskRunDebug) (errs *apis.FieldError) {
breakpointOnFailure := "onFailure"
validBreakpoints := sets.NewString()
validBreakpoints.Insert(breakpointOnFailure)
for _, b := range db.Breakpoint {
if !validBreakpoints.Has(b) {
errs = errs.Also(apis.ErrInvalidValue(fmt.Sprintf("%s is not a valid breakpoint. Available valid breakpoints include %s", b, validBreakpoints.List()), "breakpoint"))
}
}
return errs
}
// validateWorkspaceBindings makes sure the volumes provided for the Task's declared workspaces make sense.
func validateWorkspaceBindings(ctx context.Context, wb []WorkspaceBinding) (errs *apis.FieldError) {
var names []string
for idx, w := range wb {
names = append(names, w.Name)
errs = errs.Also(w.Validate(ctx).ViaIndex(idx))
}
errs = errs.Also(validateNoDuplicateNames(names, true))
return errs
}
func validateParameters(params []Param) (errs *apis.FieldError) {
var names []string
for _, p := range params {
names = append(names, p.Name)
}
return validateNoDuplicateNames(names, false)
}
func validateStepOverrides(overrides []TaskRunStepOverride) (errs *apis.FieldError) {
var names []string
for i, o := range overrides {
if o.Name == "" {
errs = errs.Also(apis.ErrMissingField("name").ViaIndex(i))
} else {
names = append(names, o.Name)
}
}
errs = errs.Also(validateNoDuplicateNames(names, true))
return errs
}
func validateSidecarOverrides(overrides []TaskRunSidecarOverride) (errs *apis.FieldError) {
var names []string
for i, o := range overrides {
if o.Name == "" {
errs = errs.Also(apis.ErrMissingField("name").ViaIndex(i))
} else {
names = append(names, o.Name)
}
}
errs = errs.Also(validateNoDuplicateNames(names, true))
return errs
}
// validateNoDuplicateNames returns an error for each name that is repeated in names.
// Case insensitive.
// If byIndex is true, the error will be reported by index instead of by key.
func validateNoDuplicateNames(names []string, byIndex bool) (errs *apis.FieldError) {
seen := sets.NewString()
for i, n := range names {
if seen.Has(strings.ToLower(n)) {
if byIndex {
errs = errs.Also(apis.ErrMultipleOneOf("name").ViaIndex(i))
} else {
errs = errs.Also(apis.ErrMultipleOneOf("name").ViaKey(n))
}
}
seen.Insert(n)
}
return errs
}