-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
patch_validation.go
393 lines (347 loc) · 12.5 KB
/
patch_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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
Copyright 2021 The Kubernetes 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 webhooks
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"text/template"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation/field"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)
// validatePatches returns errors if the Patches in the ClusterClass violate any validation rules.
func validatePatches(clusterClass *clusterv1.ClusterClass) field.ErrorList {
var allErrs field.ErrorList
names := sets.String{}
for i, patch := range clusterClass.Spec.Patches {
allErrs = append(
allErrs,
validatePatch(patch, names, clusterClass, field.NewPath("spec", "patches").Index(i))...,
)
names.Insert(patch.Name)
}
return allErrs
}
func validatePatch(patch clusterv1.ClusterClassPatch, names sets.String, clusterClass *clusterv1.ClusterClass, path *field.Path) field.ErrorList {
var allErrs field.ErrorList
allErrs = append(allErrs,
validatePatchName(patch, names, path)...,
)
allErrs = append(allErrs,
validatePatchDefinitions(patch, clusterClass, path)...,
)
return allErrs
}
func validatePatchName(patch clusterv1.ClusterClassPatch, names sets.String, path *field.Path) field.ErrorList {
var allErrs field.ErrorList
if patch.Name == "" {
allErrs = append(allErrs,
field.Required(
path.Child("name"),
"patch name must be defined",
),
)
}
if names.Has(patch.Name) {
allErrs = append(allErrs,
field.Invalid(
path.Child("name"),
patch.Name,
fmt.Sprintf("patch names must be unique. Patch with name %q is defined more than once", patch.Name),
),
)
}
return allErrs
}
func validatePatchDefinitions(patch clusterv1.ClusterClassPatch, clusterClass *clusterv1.ClusterClass, path *field.Path) field.ErrorList {
var allErrs field.ErrorList
allErrs = append(allErrs, validateEnabledIf(patch.EnabledIf, path.Child("enabledIf"))...)
for i, definition := range patch.Definitions {
allErrs = append(allErrs,
validateJSONPatches(definition.JSONPatches, clusterClass.Spec.Variables, path.Child("definitions").Index(i).Child("jsonPatches"))...)
allErrs = append(allErrs,
validateSelectors(definition.Selector, clusterClass, path.Child("definitions").Index(i).Child("selector"))...)
}
return allErrs
}
// validateSelectors validates if enabledIf is a valid template if it is set.
func validateEnabledIf(enabledIf *string, path *field.Path) field.ErrorList {
var allErrs field.ErrorList
if enabledIf != nil {
// Error if template can not be parsed.
_, err := template.New("enabledIf").Parse(*enabledIf)
if err != nil {
allErrs = append(allErrs,
field.Invalid(
path,
*enabledIf,
fmt.Sprintf("template can not be parsed: %v", err),
))
}
}
return allErrs
}
// validateSelectors tests to see if the selector matches any template in the ClusterClass.
// It returns nil as soon as it finds any matching template and an error if there is no match.
func validateSelectors(selector clusterv1.PatchSelector, class *clusterv1.ClusterClass, path *field.Path) field.ErrorList {
var allErrs field.ErrorList
// Return an error if none of the possible selectors are enabled.
if !(selector.MatchResources.InfrastructureCluster || selector.MatchResources.ControlPlane ||
(selector.MatchResources.MachineDeploymentClass != nil && len(selector.MatchResources.MachineDeploymentClass.Names) > 0)) {
return append(allErrs,
field.Invalid(
path,
prettyPrint(selector),
"no selector enabled",
))
}
if selector.MatchResources.InfrastructureCluster {
if selectorMatchTemplate(selector, class.Spec.Infrastructure.Ref) {
return nil
}
}
if selector.MatchResources.ControlPlane {
if selectorMatchTemplate(selector, class.Spec.ControlPlane.Ref) {
return nil
}
}
if selector.MatchResources.ControlPlane && class.Spec.ControlPlane.MachineInfrastructure != nil {
if selectorMatchTemplate(selector, class.Spec.ControlPlane.MachineInfrastructure.Ref) {
return nil
}
}
if selector.MatchResources.MachineDeploymentClass != nil && len(selector.MatchResources.MachineDeploymentClass.Names) > 0 {
for _, name := range selector.MatchResources.MachineDeploymentClass.Names {
for _, md := range class.Spec.Workers.MachineDeployments {
if md.Class == name {
if selectorMatchTemplate(selector, md.Template.Infrastructure.Ref) {
return nil
}
if selectorMatchTemplate(selector, md.Template.Bootstrap.Ref) {
return nil
}
}
}
}
}
// if the code has not returned at this point there is no matching template in the ClusterClass. Return an error.
return append(allErrs,
field.Invalid(
path,
prettyPrint(selector),
"selector did not match any template in the ClusterClass",
))
}
// selectorMatchTemplate returns true if APIVersion and Kind for the given selector match the reference.
func selectorMatchTemplate(selector clusterv1.PatchSelector, reference *corev1.ObjectReference) bool {
if reference == nil {
return false
}
return selector.Kind == reference.Kind && selector.APIVersion == reference.APIVersion
}
var validOps = sets.NewString("add", "replace", "remove")
func validateJSONPatches(jsonPatches []clusterv1.JSONPatch, variables []clusterv1.ClusterClassVariable, path *field.Path) field.ErrorList {
var allErrs field.ErrorList
variableSet, _ := getClusterClassVariablesMapWithReverseIndex(variables)
for i, jsonPatch := range jsonPatches {
if !validOps.Has(jsonPatch.Op) {
allErrs = append(allErrs,
field.NotSupported(
path.Index(i).Child("op"),
prettyPrint(jsonPatch),
validOps.List(),
))
}
if !strings.HasPrefix(jsonPatch.Path, "/spec/") {
allErrs = append(allErrs,
field.Invalid(
path.Index(i).Child("path"),
prettyPrint(jsonPatch),
"jsonPatch path must start with \"/spec/\"",
))
}
// Validate that array access is only prepend or append for add and not allowed for replace or remove.
allErrs = append(allErrs,
validateIndexAccess(jsonPatch, path.Index(i).Child("path"))...,
)
// Validate the value and valueFrom fields for the patch.
allErrs = append(allErrs,
validateJSONPatchValues(jsonPatch, variableSet, path.Index(i))...,
)
}
return allErrs
}
func validateJSONPatchValues(jsonPatch clusterv1.JSONPatch, variableSet map[string]*clusterv1.ClusterClassVariable, path *field.Path) field.ErrorList {
var allErrs field.ErrorList
// move to the next variable if the jsonPatch does not have "replace" or "add" op. Additional validation is not needed.
if jsonPatch.Op != "add" && jsonPatch.Op != "replace" {
return allErrs
}
if jsonPatch.Value == nil && jsonPatch.ValueFrom == nil {
allErrs = append(allErrs,
field.Invalid(
path,
prettyPrint(jsonPatch),
"jsonPatch must define one of value or valueFrom",
))
}
if jsonPatch.Value != nil && jsonPatch.ValueFrom != nil {
allErrs = append(allErrs,
field.Invalid(
path,
prettyPrint(jsonPatch),
"jsonPatch can not define both value and valueFrom",
))
}
// Attempt to marshal the JSON to discover if it is valid. If jsonPatch.Value.Raw is set to nil skip this check
// and accept the nil value.
if jsonPatch.Value != nil && jsonPatch.Value.Raw != nil {
var v interface{}
if err := json.Unmarshal(jsonPatch.Value.Raw, &v); err != nil {
allErrs = append(allErrs,
field.Invalid(
path.Child("value"),
string(jsonPatch.Value.Raw),
"jsonPatch Value is invalid JSON",
))
}
}
if jsonPatch.ValueFrom != nil && jsonPatch.ValueFrom.Template == nil && jsonPatch.ValueFrom.Variable == nil {
allErrs = append(allErrs,
field.Invalid(
path.Child("valueFrom"),
prettyPrint(jsonPatch.ValueFrom),
"valueFrom must set either template or variable",
))
}
if jsonPatch.ValueFrom != nil && jsonPatch.ValueFrom.Template != nil && jsonPatch.ValueFrom.Variable != nil {
allErrs = append(allErrs,
field.Invalid(
path.Child("valueFrom"),
prettyPrint(jsonPatch.ValueFrom),
"valueFrom can not set both template and variable",
))
}
if jsonPatch.ValueFrom != nil && jsonPatch.ValueFrom.Template != nil {
// Error if template can not be parsed.
_, err := template.New("valueFrom.template").Parse(*jsonPatch.ValueFrom.Template)
if err != nil {
allErrs = append(allErrs,
field.Invalid(
path.Child("valueFrom", "template"),
*jsonPatch.ValueFrom.Template,
fmt.Sprintf("template can not be parsed: %v", err),
))
}
}
// If set validate that the variable is valid.
if jsonPatch.ValueFrom != nil && jsonPatch.ValueFrom.Variable != nil {
// If the variable is one of the list of builtin variables it's valid.
if strings.HasPrefix(*jsonPatch.ValueFrom.Variable, "builtin.") {
if _, ok := builtinVariables[*jsonPatch.ValueFrom.Variable]; !ok {
allErrs = append(allErrs,
field.Invalid(
path.Child("valueFrom", "variable"),
*jsonPatch.ValueFrom.Variable,
"not a defined builtin variable",
))
}
} else {
// Note: We're only validating if the variable name exists without
// validating if the whole path is an existing variable.
// This could be done by re-using getVariableValue of the json patch
// generator but requires a refactoring first.
variableName := getVariableName(*jsonPatch.ValueFrom.Variable)
if _, ok := variableSet[variableName]; !ok {
allErrs = append(allErrs,
field.Invalid(
path.Child("valueFrom", "variable"),
*jsonPatch.ValueFrom.Variable,
fmt.Sprintf("variable with name %s cannot be found", *jsonPatch.ValueFrom.Variable),
))
}
}
}
return allErrs
}
func getVariableName(variable string) string {
return strings.FieldsFunc(variable, func(r rune) bool {
return r == '[' || r == '.'
})[0]
}
// This contains a list of all of the valid builtin variables.
// TODO(killianmuldoon): Match this list to controllers/topology/internal/extensions/patches/variables as those structs become available across the code base i.e. public or top-level internal.
var builtinVariables = sets.NewString(
"builtin",
// Cluster builtins.
"builtin.cluster",
"builtin.cluster.name",
"builtin.cluster.namespace",
// ClusterTopology builtins.
"builtin.cluster.topology",
"builtin.cluster.topology.version",
"builtin.cluster.topology.class",
// ControlPlane builtins.
"builtin.controlPlane",
"builtin.controlPlane.version",
"builtin.controlPlane.replicas",
// MachineDeployment builtins.
"builtin.machineDeployment",
"builtin.machineDeployment.version",
"builtin.machineDeployment.class",
"builtin.machineDeployment.name",
"builtin.machineDeployment.topologyName",
"builtin.machineDeployment.replicas",
)
// validateIndexAccess checks to see if the jsonPath is attempting to add an element in the array i.e. access by number
// If the operation is add an error is thrown if a number greater than 0 is used as an index.
// If the operation is replace an error is thrown if an index is used.
func validateIndexAccess(jsonPatch clusterv1.JSONPatch, path *field.Path) field.ErrorList {
var allErrs field.ErrorList
pathParts := strings.Split(jsonPatch.Path, "/")
for _, part := range pathParts {
// Check if the path segment is a valid number. If an error is thrown continue to the next segment.
index, err := strconv.Atoi(part)
if err != nil {
continue
}
// If the operation is add an error is thrown if a number greater than 0 is used as an index.
if jsonPatch.Op == "add" && index != 0 {
allErrs = append(allErrs,
field.Invalid(path,
jsonPatch.Path,
"arrays can only be accessed using \"0\" (prepend) or \"-\" (append)",
))
}
// If the jsonPatch operation is replace or remove disallow any number as an element in the path.
if jsonPatch.Op == "replace" || jsonPatch.Op == "remove" {
allErrs = append(allErrs,
field.Invalid(path,
jsonPatch.Path,
fmt.Sprintf("elements in arrays can not be accessed in a %s operation", jsonPatch.Op),
))
}
}
return allErrs
}
func prettyPrint(v interface{}) string {
b, err := json.MarshalIndent(v, "", " ")
if err != nil {
return errors.Wrapf(err, "failed to marshal field value").Error()
}
return string(b)
}