forked from tektoncd/pipeline
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtaskrun_validation.go
305 lines (277 loc) · 10.8 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
Copyright 2022 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 v1
import (
"context"
"fmt"
"strings"
"github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/pod"
"github.com/tektoncd/pipeline/pkg/apis/validate"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/utils/strings/slices"
"knative.dev/pkg/apis"
"knative.dev/pkg/webhook/resourcesemantics"
)
var _ apis.Validatable = (*TaskRun)(nil)
var _ resourcesemantics.VerbLimited = (*TaskRun)(nil)
// SupportedVerbs returns the operations that validation should be called for
func (tr *TaskRun) SupportedVerbs() []admissionregistrationv1.OperationType {
return []admissionregistrationv1.OperationType{admissionregistrationv1.Create, admissionregistrationv1.Update}
}
// Validate taskrun
func (tr *TaskRun) Validate(ctx context.Context) *apis.FieldError {
errs := validate.ObjectMetadata(tr.GetObjectMeta()).ViaField("metadata")
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(ctx, ts.Params).ViaField("params"))
// Validate propagated parameters
errs = errs.Also(ts.validateInlineParameters(ctx))
errs = errs.Also(ValidateWorkspaceBindings(ctx, ts.Workspaces).ViaField("workspaces"))
if ts.Debug != nil {
errs = errs.Also(config.ValidateEnabledAPIFields(ctx, "debug", config.AlphaAPIFields).ViaField("debug"))
errs = errs.Also(validateDebug(ts.Debug).ViaField("debug"))
}
if ts.StepSpecs != nil {
errs = errs.Also(config.ValidateEnabledAPIFields(ctx, "stepSpecs", config.AlphaAPIFields).ViaField("stepSpecs"))
errs = errs.Also(validateStepSpecs(ts.StepSpecs).ViaField("stepSpecs"))
}
if ts.SidecarSpecs != nil {
errs = errs.Also(config.ValidateEnabledAPIFields(ctx, "sidecarSpecs", config.AlphaAPIFields).ViaField("sidecarSpecs"))
errs = errs.Also(validateSidecarSpecs(ts.SidecarSpecs).ViaField("sidecarSpecs"))
}
if ts.ComputeResources != nil {
errs = errs.Also(config.ValidateEnabledAPIFields(ctx, "computeResources", config.BetaAPIFields).ViaField("computeResources"))
errs = errs.Also(validateTaskRunComputeResources(ts.ComputeResources, ts.StepSpecs))
}
if ts.Status != "" {
if ts.Status != TaskRunSpecStatusCancelled {
errs = errs.Also(apis.ErrInvalidValue(fmt.Sprintf("%s should be %s", ts.Status, TaskRunSpecStatusCancelled), "status"))
}
}
if ts.Status == "" {
if ts.StatusMessage != "" {
errs = errs.Also(apis.ErrInvalidValue(fmt.Sprintf("statusMessage should not be set if status is not set, but it is currently set to %s", ts.StatusMessage), "statusMessage"))
}
}
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"))
}
}
if ts.PodTemplate != nil {
errs = errs.Also(validatePodTemplateEnv(ctx, *ts.PodTemplate))
}
return errs
}
// validateInlineParameters validates that any parameters called in the
// Task spec are declared in the TaskRun.
// This is crucial for propagated parameters because the parameters could
// be defined under taskRun and then called directly in the task steps.
// In this case, parameters cannot be validated by the underlying taskSpec
// since they may not have the parameters declared because of propagation.
func (ts *TaskRunSpec) validateInlineParameters(ctx context.Context) (errs *apis.FieldError) {
if ts.TaskSpec == nil {
return errs
}
paramSpecForValidation := make(map[string]ParamSpec)
for _, p := range ts.Params {
paramSpecForValidation = createParamSpecFromParam(p, paramSpecForValidation)
}
for _, p := range ts.TaskSpec.Params {
var err *apis.FieldError
paramSpecForValidation, err = combineParamSpec(p, paramSpecForValidation)
if err != nil {
errs = errs.Also(err)
}
}
var paramSpec []ParamSpec
for _, v := range paramSpecForValidation {
paramSpec = append(paramSpec, v)
}
if ts.TaskSpec != nil && ts.TaskSpec.Steps != nil {
errs = errs.Also(ValidateParameterTypes(ctx, paramSpec))
errs = errs.Also(ValidateParameterVariables(ctx, ts.TaskSpec.Steps, paramSpec))
errs = errs.Also(ValidateUsageOfDeclaredParameters(ctx, ts.TaskSpec.Steps, paramSpec))
}
return errs
}
func validatePodTemplateEnv(ctx context.Context, podTemplate pod.Template) (errs *apis.FieldError) {
forbiddenEnvsConfigured := config.FromContextOrDefaults(ctx).Defaults.DefaultForbiddenEnv
if len(forbiddenEnvsConfigured) == 0 {
return errs
}
for _, pEnv := range podTemplate.Env {
if slices.Contains(forbiddenEnvsConfigured, pEnv.Name) {
errs = errs.Also(apis.ErrInvalidValue("PodTemplate cannot update a forbidden env: "+pEnv.Name, "PodTemplate.Env"))
}
}
return errs
}
func createParamSpecFromParam(p Param, paramSpecForValidation map[string]ParamSpec) map[string]ParamSpec {
value := p.Value
pSpec := ParamSpec{
Name: p.Name,
Default: &value,
Type: p.Value.Type,
}
if p.Value.ObjectVal != nil {
pSpec.Properties = make(map[string]PropertySpec)
prop := make(map[string]PropertySpec)
for k := range p.Value.ObjectVal {
prop[k] = PropertySpec{Type: ParamTypeString}
}
pSpec.Properties = prop
}
paramSpecForValidation[p.Name] = pSpec
return paramSpecForValidation
}
func combineParamSpec(p ParamSpec, paramSpecForValidation map[string]ParamSpec) (map[string]ParamSpec, *apis.FieldError) {
if pSpec, ok := paramSpecForValidation[p.Name]; ok {
// Merge defaults with provided values in the taskrun.
if p.Default != nil && p.Default.ObjectVal != nil {
for k, v := range p.Default.ObjectVal {
if pSpec.Default.ObjectVal == nil {
pSpec.Default.ObjectVal = map[string]string{k: v}
} else {
pSpec.Default.ObjectVal[k] = v
}
}
// If Default values of object type are provided then Properties must also be fully declared.
if p.Properties == nil {
return paramSpecForValidation, apis.ErrMissingField(fmt.Sprintf("%s.properties", p.Name))
}
}
// Properties must be defined if paramSpec is of object Type
if pSpec.Type == ParamTypeObject {
if p.Properties == nil {
return paramSpecForValidation, apis.ErrMissingField(fmt.Sprintf("%s.properties", p.Name))
}
// Expect Properties to be complete
pSpec.Properties = p.Properties
}
paramSpecForValidation[p.Name] = pSpec
} else {
// No values provided by task run but found a paramSpec declaration.
// Expect it to be fully speced out.
paramSpecForValidation[p.Name] = p
}
return paramSpecForValidation, nil
}
// validateDebug validates the debug section of the TaskRun.
// if set, onFailure breakpoint must be "enabled"
func validateDebug(db *TaskRunDebug) (errs *apis.FieldError) {
if db == nil || db.Breakpoints == nil {
return errs
}
if db.Breakpoints.OnFailure != "" && db.Breakpoints.OnFailure != EnabledOnFailureBreakpoint {
errs = errs.Also(apis.ErrInvalidValue(fmt.Sprintf("%s is not a valid onFailure breakpoint value, onFailure breakpoint is only allowed to be set as enabled", db.Breakpoints.OnFailure), "breakpoints.onFailure"))
}
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
}
// ValidateParameters makes sure the params for the Task are valid.
func ValidateParameters(ctx context.Context, params Params) (errs *apis.FieldError) {
var names []string
for _, p := range params {
if p.Value.Type == ParamTypeObject {
// Object type parameter is a beta feature and will fail validation if it's used in a taskrun spec
// when the enable-api-fields feature gate is not "alpha" or "beta".
errs = errs.Also(config.ValidateEnabledAPIFields(ctx, "object type parameter", config.BetaAPIFields))
}
names = append(names, p.Name)
}
return errs.Also(validateNoDuplicateNames(names, false))
}
func validateStepSpecs(specs []TaskRunStepSpec) (errs *apis.FieldError) {
var names []string
for i, o := range specs {
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
}
// validateTaskRunComputeResources ensures that compute resources are not configured at both the step level and the task level
func validateTaskRunComputeResources(computeResources *corev1.ResourceRequirements, specs []TaskRunStepSpec) (errs *apis.FieldError) {
for _, spec := range specs {
if spec.ComputeResources.Size() != 0 && computeResources != nil {
return apis.ErrMultipleOneOf(
"stepSpecs.resources",
"computeResources",
)
}
}
return nil
}
func validateSidecarSpecs(specs []TaskRunSidecarSpec) (errs *apis.FieldError) {
var names []string
for i, o := range specs {
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(strings.ToLower(n))
}
return errs
}