forked from tektoncd/pipeline
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtask_validation.go
91 lines (77 loc) · 2.45 KB
/
task_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
/*
Copyright 2018 The Knative 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 v1alpha1
import (
"fmt"
"strings"
"github.com/knative/pkg/apis"
"k8s.io/apimachinery/pkg/api/equality"
)
func (t *Task) Validate() *apis.FieldError {
if err := validateObjectMetadata(t.GetObjectMeta()); err != nil {
return err.ViaField("metadata")
}
return t.Spec.Validate()
}
func (ts *TaskSpec) Validate() *apis.FieldError {
if equality.Semantic.DeepEqual(ts, &TaskSpec{}) {
return apis.ErrMissingField(apis.CurrentField)
}
// A Task must have a valid BuildSpec.
if ts.BuildSpec == nil {
return apis.ErrMissingField("taskspec.BuildSpec")
}
if err := ts.BuildSpec.Validate(); err != nil {
return err
}
// We also require that a BuildSpec is NOT a template.
if ts.BuildSpec.Template != nil {
return apis.ErrDisallowedFields("taskspec.BuildSpec.Template")
}
// A task doesn't have to have inputs or outputs, but if it does they must be valid.
// A task can't duplicate input or output names.
if ts.Inputs != nil {
for _, resource := range ts.Inputs.Resources {
if err := validateResourceType(resource, fmt.Sprintf("taskspec.Inputs.Resources.%s.Type", resource.Name)); err != nil {
return err
}
}
if err := checkForDuplicates(ts.Inputs.Resources, "taskspec.Inputs.Resources.Name"); err != nil {
return err
}
}
if ts.Outputs != nil {
if err := ts.Outputs.Validate("taskspec.Outputs"); err != nil {
return err
}
}
return nil
}
func checkForDuplicates(resources []TaskResource, path string) *apis.FieldError {
encountered := map[string]struct{}{}
for _, r := range resources {
if _, ok := encountered[strings.ToLower(r.Name)]; ok {
return apis.ErrMultipleOneOf(path)
}
encountered[strings.ToLower(r.Name)] = struct{}{}
}
return nil
}
func validateResourceType(r TaskResource, path string) *apis.FieldError {
for _, allowed := range AllResourceTypes {
if r.Type == allowed {
return nil
}
}
return apis.ErrInvalidValue(string(r.Type), path)
}