forked from kubernetes-sigs/cluster-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompatibility.go
488 lines (435 loc) · 19 KB
/
compatibility.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
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 check implements checks for managed topology.
package check
import (
"fmt"
"strings"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/apimachinery/pkg/util/validation/field"
"sigs.k8s.io/controller-runtime/pkg/client"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)
// ObjectsAreStrictlyCompatible checks if two referenced objects are strictly compatible, meaning that
// they are compatible and the name of the objects do not change.
func ObjectsAreStrictlyCompatible(current, desired client.Object) field.ErrorList {
var allErrs field.ErrorList
if current.GetName() != desired.GetName() {
allErrs = append(allErrs, field.Forbidden(
field.NewPath("metadata", "name"),
fmt.Sprintf("metadata.name of %s/%s cannot be changed from %q to %q to prevent incompatible changes in the Cluster",
current.GetObjectKind().GroupVersionKind().GroupKind().String(), current.GetName(), current.GetName(), desired.GetName()),
))
}
allErrs = append(allErrs, ObjectsAreCompatible(current, desired)...)
return allErrs
}
// ObjectsAreCompatible checks if two referenced objects are compatible, meaning that
// they are of the same GroupKind and in the same namespace.
func ObjectsAreCompatible(current, desired client.Object) field.ErrorList {
var allErrs field.ErrorList
currentGK := current.GetObjectKind().GroupVersionKind().GroupKind()
desiredGK := desired.GetObjectKind().GroupVersionKind().GroupKind()
if currentGK.Group != desiredGK.Group {
allErrs = append(allErrs, field.Forbidden(
field.NewPath("metadata", "apiVersion"),
fmt.Sprintf("apiVersion.group of %s/%s cannot be changed from %q to %q to prevent incompatible changes in the Cluster",
currentGK.String(), current.GetName(), currentGK.Group, desiredGK.Group),
))
}
if currentGK.Kind != desiredGK.Kind {
allErrs = append(allErrs, field.Forbidden(
field.NewPath("metadata", "kind"),
fmt.Sprintf("apiVersion.kind of %s/%s cannot be changed from %q to %q to prevent incompatible changes in the Cluster",
currentGK.String(), current.GetName(), currentGK.Kind, desiredGK.Kind),
))
}
allErrs = append(allErrs, ObjectsAreInTheSameNamespace(current, desired)...)
return allErrs
}
// ObjectsAreInTheSameNamespace checks if two referenced objects are in the same namespace.
func ObjectsAreInTheSameNamespace(current, desired client.Object) field.ErrorList {
var allErrs field.ErrorList
// NOTE: this should never happen (webhooks prevent it), but checking for extra safety.
if current.GetNamespace() != desired.GetNamespace() {
allErrs = append(allErrs, field.Forbidden(
field.NewPath("metadata", "namespace"),
fmt.Sprintf("metadata.namespace of %s/%s cannot be changed from %q to %q because templates must be in the same namespace as the Cluster",
current.GetObjectKind().GroupVersionKind().GroupKind().String(), current.GetName(), current.GetNamespace(), desired.GetNamespace()),
))
}
return allErrs
}
// LocalObjectTemplatesAreCompatible checks if two referenced objects are compatible, meaning that
// they are of the same GroupKind and in the same namespace.
func LocalObjectTemplatesAreCompatible(current, desired clusterv1.LocalObjectTemplate, pathPrefix *field.Path) field.ErrorList {
var allErrs field.ErrorList
currentGK := current.Ref.GetObjectKind().GroupVersionKind().GroupKind()
desiredGK := desired.Ref.GetObjectKind().GroupVersionKind().GroupKind()
if currentGK.Group != desiredGK.Group {
allErrs = append(allErrs, field.Forbidden(
pathPrefix.Child("ref", "apiVersion"),
fmt.Sprintf("apiVersion.group cannot be changed from %q to %q to prevent incompatible changes in the Clusters",
currentGK.Group, desiredGK.Group),
))
}
if currentGK.Kind != desiredGK.Kind {
allErrs = append(allErrs, field.Forbidden(
pathPrefix.Child("ref", "kind"),
fmt.Sprintf("apiVersion.kind cannot be changed from %q to %q to prevent incompatible changes in the Clusters",
currentGK.Kind, desiredGK.Kind),
))
}
allErrs = append(allErrs, LocalObjectTemplatesAreInSameNamespace(current, desired, pathPrefix)...)
return allErrs
}
// LocalObjectTemplatesAreInSameNamespace checks if two referenced objects are in the same namespace.
func LocalObjectTemplatesAreInSameNamespace(current, desired clusterv1.LocalObjectTemplate, pathPrefix *field.Path) field.ErrorList {
var allErrs field.ErrorList
if current.Ref.Namespace != desired.Ref.Namespace {
allErrs = append(allErrs, field.Forbidden(
pathPrefix.Child("ref", "namespace"),
fmt.Sprintf("templates must be in the same namespace as the ClusterClass (%s)",
current.Ref.Namespace),
))
}
return allErrs
}
// LocalObjectTemplateIsValid ensures the template is in the correct namespace, has no nil references, and has a valid Kind and GroupVersion.
func LocalObjectTemplateIsValid(template *clusterv1.LocalObjectTemplate, namespace string, pathPrefix *field.Path) field.ErrorList {
var allErrs field.ErrorList
// check if ref is not nil.
if template.Ref == nil {
return field.ErrorList{field.Required(
pathPrefix.Child("ref"),
"template reference must be defined",
)}
}
// check if a name is provided
if template.Ref.Name == "" {
allErrs = append(allErrs,
field.Required(
pathPrefix.Child("ref", "name"),
"template name must be defined",
),
)
}
// validate if namespace matches the provided namespace
if namespace != "" && template.Ref.Namespace != namespace {
allErrs = append(
allErrs,
field.Invalid(
pathPrefix.Child("ref", "namespace"),
template.Ref.Namespace,
fmt.Sprintf("template must be in the same namespace as the ClusterClass (%s)", namespace),
),
)
}
// check if kind is a template
if len(template.Ref.Kind) <= len(clusterv1.TemplateSuffix) || !strings.HasSuffix(template.Ref.Kind, clusterv1.TemplateSuffix) {
allErrs = append(allErrs,
field.Invalid(
pathPrefix.Child("ref", "kind"),
template.Ref.Kind,
fmt.Sprintf("template kind must be of form \"<name>%s\"", clusterv1.TemplateSuffix),
),
)
}
// check if apiVersion is valid
gv, err := schema.ParseGroupVersion(template.Ref.APIVersion)
if err != nil {
allErrs = append(allErrs,
field.Invalid(
pathPrefix.Child("ref", "apiVersion"),
template.Ref.APIVersion,
fmt.Sprintf("template apiVersion must be a valid Kubernetes apiVersion: %v", err),
),
)
}
if err == nil && gv.Empty() {
allErrs = append(allErrs,
field.Required(
pathPrefix.Child("ref", "apiVersion"),
"template apiVersion must be defined",
),
)
}
return allErrs
}
// ClusterClassesAreCompatible checks the compatibility between new and old versions of a Cluster Class.
// It checks that:
// 1) InfrastructureCluster Templates are compatible.
// 2) ControlPlane Templates are compatible.
// 3) ControlPlane InfrastructureMachineTemplates are compatible.
// 4) MachineDeploymentClasses are compatible.
// 5) MachinePoolClasses are compatible.
func ClusterClassesAreCompatible(current, desired *clusterv1.ClusterClass) field.ErrorList {
var allErrs field.ErrorList
if current == nil {
return append(allErrs, field.Invalid(field.NewPath(""), "", "could not compare ClusterClass compatibility: current ClusterClass must not be nil"))
}
if desired == nil {
return append(allErrs, field.Invalid(field.NewPath(""), "", "could not compare ClusterClass compatibility: desired ClusterClass must not be nil"))
}
// Validate InfrastructureClusterTemplate changes desired a compatible way.
allErrs = append(allErrs, LocalObjectTemplatesAreCompatible(current.Spec.Infrastructure, desired.Spec.Infrastructure,
field.NewPath("spec", "infrastructure"))...)
// Validate control plane changes desired a compatible way.
allErrs = append(allErrs, LocalObjectTemplatesAreCompatible(current.Spec.ControlPlane.LocalObjectTemplate, desired.Spec.ControlPlane.LocalObjectTemplate,
field.NewPath("spec", "controlPlane"))...)
if desired.Spec.ControlPlane.MachineInfrastructure != nil && current.Spec.ControlPlane.MachineInfrastructure != nil {
allErrs = append(allErrs, LocalObjectTemplatesAreCompatible(*current.Spec.ControlPlane.MachineInfrastructure, *desired.Spec.ControlPlane.MachineInfrastructure,
field.NewPath("spec", "controlPlane", "machineInfrastructure"))...)
}
// Validate changes to MachineDeployments.
allErrs = append(allErrs, MachineDeploymentClassesAreCompatible(current, desired)...)
// Validate changes to MachinePools.
allErrs = append(allErrs, MachinePoolClassesAreCompatible(current, desired)...)
return allErrs
}
// MachineDeploymentClassesAreCompatible checks if each MachineDeploymentClass in the new ClusterClass is a compatible change from the previous ClusterClass.
// It checks if the MachineDeploymentClass.Template.Infrastructure reference has changed its Group or Kind.
func MachineDeploymentClassesAreCompatible(current, desired *clusterv1.ClusterClass) field.ErrorList {
var allErrs field.ErrorList
// Ensure previous MachineDeployment class was modified in a compatible way.
for _, class := range desired.Spec.Workers.MachineDeployments {
for i, oldClass := range current.Spec.Workers.MachineDeployments {
if class.Class == oldClass.Class {
// NOTE: class.Template.Metadata and class.Template.Bootstrap are allowed to change;
// class.Template.Bootstrap is ensured syntactically correct by LocalObjectTemplateIsValid.
// Validates class.Template.Infrastructure template changes in a compatible way
allErrs = append(allErrs, LocalObjectTemplatesAreCompatible(oldClass.Template.Infrastructure, class.Template.Infrastructure,
field.NewPath("spec", "workers", "machineDeployments").Index(i))...)
}
}
}
return allErrs
}
// MachineDeploymentClassesAreUnique checks that no two MachineDeploymentClasses in a ClusterClass share a name.
func MachineDeploymentClassesAreUnique(clusterClass *clusterv1.ClusterClass) field.ErrorList {
var allErrs field.ErrorList
classes := sets.Set[string]{}
for i, class := range clusterClass.Spec.Workers.MachineDeployments {
if classes.Has(class.Class) {
allErrs = append(allErrs,
field.Invalid(
field.NewPath("spec", "workers", "machineDeployments").Index(i).Child("class"),
class.Class,
fmt.Sprintf("MachineDeployment class must be unique. MachineDeployment with class %q is defined more than once", class.Class),
),
)
}
classes.Insert(class.Class)
}
return allErrs
}
// MachinePoolClassesAreCompatible checks if each MachinePoolClass in the new ClusterClass is a compatible change from the previous ClusterClass.
// It checks if the MachinePoolClass.Template.Infrastructure reference has changed its Group or Kind.
func MachinePoolClassesAreCompatible(current, desired *clusterv1.ClusterClass) field.ErrorList {
var allErrs field.ErrorList
// Ensure previous MachinePool class was modified in a compatible way.
for _, class := range desired.Spec.Workers.MachinePools {
for i, oldClass := range current.Spec.Workers.MachinePools {
if class.Class == oldClass.Class {
// NOTE: class.Template.Metadata and class.Template.Bootstrap are allowed to change;
// class.Template.Bootstrap is ensured syntactically correct by LocalObjectTemplateIsValid.
// Validates class.Template.Infrastructure template changes in a compatible way
allErrs = append(allErrs, LocalObjectTemplatesAreCompatible(oldClass.Template.Infrastructure, class.Template.Infrastructure,
field.NewPath("spec", "workers", "machinePools").Index(i))...)
}
}
}
return allErrs
}
// MachinePoolClassesAreUnique checks that no two MachinePoolClasses in a ClusterClass share a name.
func MachinePoolClassesAreUnique(clusterClass *clusterv1.ClusterClass) field.ErrorList {
var allErrs field.ErrorList
classes := sets.Set[string]{}
for i, class := range clusterClass.Spec.Workers.MachinePools {
if classes.Has(class.Class) {
allErrs = append(allErrs,
field.Invalid(
field.NewPath("spec", "workers", "machinePools").Index(i).Child("class"),
class.Class,
fmt.Sprintf("MachinePool class must be unique. MachinePool with class %q is defined more than once", class.Class),
),
)
}
classes.Insert(class.Class)
}
return allErrs
}
// MachineDeploymentTopologiesAreValidAndDefinedInClusterClass checks that each MachineDeploymentTopology name is not empty
// and unique, and each class in use is defined in ClusterClass.spec.Workers.MachineDeployments.
func MachineDeploymentTopologiesAreValidAndDefinedInClusterClass(desired *clusterv1.Cluster, clusterClass *clusterv1.ClusterClass) field.ErrorList {
var allErrs field.ErrorList
if desired.Spec.Topology.Workers == nil {
return nil
}
if len(desired.Spec.Topology.Workers.MachineDeployments) == 0 {
return nil
}
// MachineDeployment clusterClass must be defined in the ClusterClass.
machineDeploymentClasses := mdClassNamesFromWorkerClass(clusterClass.Spec.Workers)
names := sets.Set[string]{}
for i, md := range desired.Spec.Topology.Workers.MachineDeployments {
if errs := validation.IsValidLabelValue(md.Name); len(errs) != 0 {
for _, err := range errs {
allErrs = append(
allErrs,
field.Invalid(
field.NewPath("spec", "topology", "workers", "machineDeployments").Index(i).Child("name"),
md.Name,
fmt.Sprintf("must be a valid label value %s", err),
),
)
}
}
if !machineDeploymentClasses.Has(md.Class) {
allErrs = append(allErrs,
field.Invalid(
field.NewPath("spec", "topology", "workers", "machineDeployments").Index(i).Child("class"),
md.Class,
fmt.Sprintf("MachineDeploymentClass with name %q does not exist in ClusterClass %q",
md.Class, clusterClass.Name),
),
)
}
// MachineDeploymentTopology name should not be empty.
if md.Name == "" {
allErrs = append(
allErrs,
field.Required(
field.NewPath("spec", "topology", "workers", "machineDeployments").Index(i).Child("name"),
"name must not be empty",
),
)
continue
}
if names.Has(md.Name) {
allErrs = append(allErrs,
field.Invalid(
field.NewPath("spec", "topology", "workers", "machineDeployments").Index(i).Child("name"),
md.Name,
fmt.Sprintf("name must be unique. MachineDeployment with name %q is defined more than once", md.Name),
),
)
}
names.Insert(md.Name)
}
return allErrs
}
// MachinePoolTopologiesAreValidAndDefinedInClusterClass checks that each MachinePoolTopology name is not empty
// and unique, and each class in use is defined in ClusterClass.spec.Workers.MachinePools.
func MachinePoolTopologiesAreValidAndDefinedInClusterClass(desired *clusterv1.Cluster, clusterClass *clusterv1.ClusterClass) field.ErrorList {
var allErrs field.ErrorList
if desired.Spec.Topology.Workers == nil {
return nil
}
if len(desired.Spec.Topology.Workers.MachinePools) == 0 {
return nil
}
// MachinePool clusterClass must be defined in the ClusterClass.
machinePoolClasses := mpClassNamesFromWorkerClass(clusterClass.Spec.Workers)
names := sets.Set[string]{}
for i, mp := range desired.Spec.Topology.Workers.MachinePools {
if errs := validation.IsValidLabelValue(mp.Name); len(errs) != 0 {
for _, err := range errs {
allErrs = append(
allErrs,
field.Invalid(
field.NewPath("spec", "topology", "workers", "machinePools").Index(i).Child("name"),
mp.Name,
fmt.Sprintf("must be a valid label value %s", err),
),
)
}
}
if !machinePoolClasses.Has(mp.Class) {
allErrs = append(allErrs,
field.Invalid(
field.NewPath("spec", "topology", "workers", "machinePools").Index(i).Child("class"),
mp.Class,
fmt.Sprintf("MachinePoolClass with name %q does not exist in ClusterClass %q",
mp.Class, clusterClass.Name),
),
)
}
// MachinePoolTopology name should not be empty.
if mp.Name == "" {
allErrs = append(
allErrs,
field.Required(
field.NewPath("spec", "topology", "workers", "machinePools").Index(i).Child("name"),
"name must not be empty",
),
)
continue
}
if names.Has(mp.Name) {
allErrs = append(allErrs,
field.Invalid(
field.NewPath("spec", "topology", "workers", "machinePools").Index(i).Child("name"),
mp.Name,
fmt.Sprintf("name must be unique. MachinePool with name %q is defined more than once", mp.Name),
),
)
}
names.Insert(mp.Name)
}
return allErrs
}
// ClusterClassReferencesAreValid checks that each template reference in the ClusterClass is valid .
func ClusterClassReferencesAreValid(clusterClass *clusterv1.ClusterClass) field.ErrorList {
var allErrs field.ErrorList
allErrs = append(allErrs, LocalObjectTemplateIsValid(&clusterClass.Spec.Infrastructure, clusterClass.Namespace,
field.NewPath("spec", "infrastructure"))...)
allErrs = append(allErrs, LocalObjectTemplateIsValid(&clusterClass.Spec.ControlPlane.LocalObjectTemplate, clusterClass.Namespace,
field.NewPath("spec", "controlPlane"))...)
if clusterClass.Spec.ControlPlane.MachineInfrastructure != nil {
allErrs = append(allErrs, LocalObjectTemplateIsValid(clusterClass.Spec.ControlPlane.MachineInfrastructure, clusterClass.Namespace, field.NewPath("spec", "controlPlane", "machineInfrastructure"))...)
}
for i := range clusterClass.Spec.Workers.MachineDeployments {
mdc := clusterClass.Spec.Workers.MachineDeployments[i]
allErrs = append(allErrs, LocalObjectTemplateIsValid(&mdc.Template.Bootstrap, clusterClass.Namespace,
field.NewPath("spec", "workers", "machineDeployments").Index(i).Child("template", "bootstrap"))...)
allErrs = append(allErrs, LocalObjectTemplateIsValid(&mdc.Template.Infrastructure, clusterClass.Namespace,
field.NewPath("spec", "workers", "machineDeployments").Index(i).Child("template", "infrastructure"))...)
}
for i := range clusterClass.Spec.Workers.MachinePools {
mpc := clusterClass.Spec.Workers.MachinePools[i]
allErrs = append(allErrs, LocalObjectTemplateIsValid(&mpc.Template.Bootstrap, clusterClass.Namespace,
field.NewPath("spec", "workers", "machinePools").Index(i).Child("template", "bootstrap"))...)
allErrs = append(allErrs, LocalObjectTemplateIsValid(&mpc.Template.Infrastructure, clusterClass.Namespace,
field.NewPath("spec", "workers", "machinePools").Index(i).Child("template", "infrastructure"))...)
}
return allErrs
}
// mdClassNamesFromWorkerClass returns the set of MachineDeployment class names.
func mdClassNamesFromWorkerClass(w clusterv1.WorkersClass) sets.Set[string] {
classes := sets.Set[string]{}
for _, class := range w.MachineDeployments {
classes.Insert(class.Class)
}
return classes
}
// mpClassNamesFromWorkerClass returns the set of MachinePool class names.
func mpClassNamesFromWorkerClass(w clusterv1.WorkersClass) sets.Set[string] {
classes := sets.Set[string]{}
for _, class := range w.MachinePools {
classes.Insert(class.Class)
}
return classes
}