-
Notifications
You must be signed in to change notification settings - Fork 43
/
validate_component.go
229 lines (190 loc) · 7.27 KB
/
validate_component.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
// Copyright 2018 Google LLC
//
// 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
//
// https://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 validate
import (
"fmt"
"regexp"
"k8s.io/apimachinery/pkg/util/validation/field"
bundle "github.com/GoogleCloudPlatform/k8s-cluster-bundle/pkg/apis/bundle/v1alpha1"
"github.com/GoogleCloudPlatform/k8s-cluster-bundle/pkg/core"
)
var (
apiVersionPattern = regexp.MustCompile(`^bundle.gke.io/\w+$`)
// numPattern is a regexp string for numbers without leading zeros.
numPattern = `([1-9]\d*|0)`
// extraVersionInfo represents extra Sem Ver information (sometimes called
// extensions), such as release tags and build information (rc-foo,
// 12eha3+alpha).
extraVersionInfo = `-[a-zA-Z0-9_.+-]+`
// versionPattern matches version string X.Y.Z, where X, Y and Z are non-negative integers
// without leading zeros.
// TODO(kashomon): Use the K8S version validation for this.
// (k8s.io/apimachinery/pkg/util/version) after K8S v1.11
versionPattern = regexp.MustCompile(fmt.Sprintf(`^%s\.%s\.%s$`, numPattern, numPattern, numPattern))
// appVersionPattern matches app version string X.Y.Z or X.Y, where X, Y and
// Z are non-negative integers without leading zeros. Also can contain
// dangling extra info.
appVersionPattern = regexp.MustCompile(fmt.Sprintf(`^%s\.%s(\.%s(%s)?)?$`, numPattern, numPattern, numPattern, extraVersionInfo))
)
// All validates components and components sets, providing as many errors as it can.
func All(cp []*bundle.Component, cs *bundle.ComponentSet) field.ErrorList {
errs := field.ErrorList{}
errs = append(errs, ComponentSet(cs)...)
errs = append(errs, AllComponents(cp)...)
errs = append(errs, ComponentsAndComponentSet(cp, cs)...)
errs = append(errs, AllComponentObjects(cp)...)
return errs
}
func cPath(ref bundle.ComponentReference) *field.Path {
return field.NewPath("Component").Key(fmt.Sprintf("%v", ref))
}
// AllComponents validates a list of components.
func AllComponents(components []*bundle.Component) field.ErrorList {
errs := field.ErrorList{}
objCollect := make(map[bundle.ComponentReference]bool)
for _, c := range components {
errs = append(errs, Component(c)...)
ref := c.ComponentReference()
p := cPath(ref)
if _, ok := objCollect[ref]; ok {
errs = append(errs, field.Duplicate(p, fmt.Sprintf("component reference %v", ref)))
continue
}
objCollect[ref] = true
}
return errs
}
// Component validates a single component.
func Component(c *bundle.Component) field.ErrorList {
errs := field.ErrorList{}
pi := field.NewPath("Component")
n := c.Spec.ComponentName
if n == "" {
errs = append(errs, field.Required(pi.Child("Spec", "ComponentName"), "components must have ComponentName"))
}
ver := c.Spec.Version
if ver == "" {
errs = append(errs, field.Required(pi.Child("Spec", "Version"), "components must have a Version"))
}
if n == "" || ver == "" {
// Subsequent validation relies on components having a unique name+version pair.
return errs
}
p := cPath(c.ComponentReference())
if nameErrs := validateName(p.Child("Spec", "ComponentName"), n); len(errs) > 0 {
errs = append(errs, nameErrs...)
}
api := c.APIVersion
if !apiVersionPattern.MatchString(api) {
errs = append(errs, field.Invalid(p.Child("APIVersion"), api, "must have the form \"bundle.gke.io/<version>\""))
}
expType := "Component"
if k := c.Kind; k != expType {
errs = append(errs, field.Invalid(p.Child("Kind"), k, "kind must be Component"))
}
if !versionPattern.MatchString(ver) {
errs = append(errs, field.Invalid(p.Child("Spec", "Version"), ver, "must be of the form X.Y.Z"))
}
if c.Spec.AppVersion != "" && !appVersionPattern.MatchString(c.Spec.AppVersion) {
errs = append(errs, field.Invalid(p.Child("Spec", "AppVersion"), ver, "must be of the form X.Y.Z or X.Y"))
}
return errs
}
// ComponentSet validates a component.
func ComponentSet(cs *bundle.ComponentSet) field.ErrorList {
p := field.NewPath("ComponentSet")
errs := field.ErrorList{}
if cs == nil {
return errs
}
n := cs.Spec.SetName
if n == "" {
errs = append(errs, field.Required(p.Child("Spec", "SetName"), "setName is required"))
}
ver := cs.Spec.Version
if ver == "" {
errs = append(errs, field.Required(p.Child("Spec", "Version"), "version is required"))
}
if n == "" || ver == "" {
// Other validation relies on components having a unique name+version pair.
return errs
}
if nameErrs := validateName(p.Child("Spec", "SetName"), n); len(nameErrs) > 0 {
errs = append(errs, nameErrs...)
}
api := cs.APIVersion
if !apiVersionPattern.MatchString(api) {
errs = append(errs, field.Invalid(p.Child("APIVersion"), api, "must have an apiVersion of the form \"bundle.gke.io/<version>\""))
}
expType := "ComponentSet"
if k := cs.Kind; k != expType {
errs = append(errs, field.Invalid(p.Child("Kind"), k, "must be ComponentSet"))
}
if !versionPattern.MatchString(ver) {
errs = append(errs, field.Invalid(p.Child("Spec", "Version"), ver, `must be of the form X.Y.Z`))
}
return errs
}
// ComponentsAndComponentSet validates components in the context of a component set.
func ComponentsAndComponentSet(components []*bundle.Component, cs *bundle.ComponentSet) field.ErrorList {
errs := field.ErrorList{}
compMap := make(map[bundle.ComponentReference]*bundle.Component)
for _, c := range components {
compMap[c.ComponentReference()] = c
}
p := field.NewPath("ComponentSet")
// It is possible for there to be components that the component set does not
// know about, but all components in the component set must be in the
// components list
for _, ref := range cs.Spec.Components {
if _, ok := compMap[ref]; !ok {
errs = append(errs, field.NotFound(p.Child("Spec", "Components").Key(fmt.Sprintf("%v", ref)), "component reference from set not found in component list"))
}
}
return errs
}
// AllComponentObjects validates all objects in all componenst
func AllComponentObjects(components []*bundle.Component) field.ErrorList {
errs := field.ErrorList{}
for _, ca := range components {
errs = append(errs, ComponentObjects(ca)...)
}
return errs
}
// ComponentObjects validates objects in a componenst.
func ComponentObjects(cp *bundle.Component) field.ErrorList {
// Map to catch duplicate objects.
compObjects := make(map[core.ObjectRef]bool)
errs := field.ErrorList{}
ref := cp.ComponentReference()
basep := cPath(ref)
for i, obj := range cp.Spec.Objects {
n := obj.GetName()
if n == "" {
errs = append(errs, field.Required(basep.Child("Spec", "Objects").Index(i).Child("Metadata", "Name"),
""))
continue
}
p := basep.Child("Spec", "Objects").Key(n)
if nameErrs := validateName(p, n); len(nameErrs) > 0 {
errs = append(errs, nameErrs...)
}
oref := core.ObjectRefFromUnstructured(obj)
if _, ok := compObjects[oref]; ok {
errs = append(errs, field.Duplicate(p, fmt.Sprintf("object reference: %s", oref)))
}
compObjects[oref] = true
}
return errs
}