-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathclusterclass_rollout.go
1377 lines (1241 loc) · 67.2 KB
/
clusterclass_rollout.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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2020 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 e2e
import (
"context"
"fmt"
"math/rand"
"os"
"path/filepath"
"strings"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog/v2"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/controllers/external"
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
"sigs.k8s.io/cluster-api/internal/contract"
"sigs.k8s.io/cluster-api/internal/controllers/topology/machineset"
"sigs.k8s.io/cluster-api/test/e2e/internal/log"
"sigs.k8s.io/cluster-api/test/framework"
"sigs.k8s.io/cluster-api/test/framework/clusterctl"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/labels"
"sigs.k8s.io/cluster-api/util/patch"
)
// ClusterClassRolloutSpecInput is the input for ClusterClassRolloutSpec.
type ClusterClassRolloutSpecInput struct {
E2EConfig *clusterctl.E2EConfig
ClusterctlConfigPath string
BootstrapClusterProxy framework.ClusterProxy
ArtifactFolder string
SkipCleanup bool
ControlPlaneWaiters clusterctl.ControlPlaneWaiters
// InfrastructureProviders specifies the infrastructure to use for clusterctl
// operations (Example: get cluster templates).
// Note: In most cases this need not be specified. It only needs to be specified when
// multiple infrastructure providers are installed on the cluster as clusterctl will not be
// able to identify the default.
InfrastructureProvider *string
// Flavor is the cluster-template flavor used to create the Cluster for testing.
// NOTE: The template must be using ClusterClass, KCP and CABPK as this test is specifically
// testing ClusterClass and KCP rollout behavior.
Flavor string
// Allows to inject a function to be run after test namespace is created.
// If not specified, this is a no-op.
PostNamespaceCreated func(managementClusterProxy framework.ClusterProxy, workloadClusterNamespace string)
// FilterMetadataBeforeValidation allows filtering out labels and annotations of Machines, InfraMachines,
// BootstrapConfigs and Nodes before we validate them.
// This can be e.g. used to filter out additional infrastructure provider specific labels that would
// otherwise lead to a failed test.
FilterMetadataBeforeValidation func(object client.Object) clusterv1.ObjectMeta
}
// ClusterClassRolloutSpec implements a test that verifies the ClusterClass rollout behavior.
// It specifically covers the in-place propagation behavior from ClusterClass / Cluster topology to all
// objects of the Cluster topology (e.g. KCP, MD) and even tests label propagation to the Nodes of the
// workload cluster.
// Thus, the test consists of the following steps:
// - Deploy Cluster using a ClusterClass and wait until it is fully provisioned.
// - Assert cluster objects
// - Modify in-place mutable fields of KCP and the MachineDeployments
// - Verify that fields were mutated in-place and assert cluster objects
// - Modify fields in KCP and MachineDeployments to trigger a full rollout of all Machines
// - Verify that all Machines have been replaced and assert cluster objects
// - Set RolloutAfter on KCP and MachineDeployments to trigger a full rollout of all Machines
// - Verify that all Machines have been replaced and assert cluster objects
//
// While asserting cluster objects we check that all objects have the right labels, annotations and selectors.
func ClusterClassRolloutSpec(ctx context.Context, inputGetter func() ClusterClassRolloutSpecInput) {
var (
specName = "clusterclass-rollout"
input ClusterClassRolloutSpecInput
namespace *corev1.Namespace
cancelWatches context.CancelFunc
clusterResources *clusterctl.ApplyClusterTemplateAndWaitResult
)
BeforeEach(func() {
Expect(ctx).NotTo(BeNil(), "ctx is required for %s spec", specName)
input = inputGetter()
Expect(input.E2EConfig).ToNot(BeNil(), "Invalid argument. input.E2EConfig can't be nil when calling %s spec", specName)
Expect(input.ClusterctlConfigPath).To(BeAnExistingFile(), "Invalid argument. input.ClusterctlConfigPath must be an existing file when calling %s spec", specName)
Expect(input.BootstrapClusterProxy).ToNot(BeNil(), "Invalid argument. input.BootstrapClusterProxy can't be nil when calling %s spec", specName)
Expect(os.MkdirAll(input.ArtifactFolder, 0750)).To(Succeed(), "Invalid argument. input.ArtifactFolder can't be created for %s spec", specName)
Expect(input.E2EConfig.Variables).To(HaveKey(KubernetesVersion))
Expect(input.E2EConfig.Variables).To(HaveValidVersion(input.E2EConfig.MustGetVariable(KubernetesVersion)))
// Set a default function to ensure that FilterMetadataBeforeValidation has a default behavior for
// filtering metadata if it is not specified by infrastructure provider.
if input.FilterMetadataBeforeValidation == nil {
input.FilterMetadataBeforeValidation = func(obj client.Object) clusterv1.ObjectMeta {
return clusterv1.ObjectMeta{Labels: obj.GetLabels(), Annotations: obj.GetAnnotations()}
}
}
// Set up a Namespace where to host objects for this spec and create a watcher for the namespace events.
namespace, cancelWatches = framework.SetupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, input.PostNamespaceCreated)
clusterResources = new(clusterctl.ApplyClusterTemplateAndWaitResult)
})
It("Should successfully rollout the managed topology upon changes to the ClusterClass", func() {
By("Creating a workload cluster")
infrastructureProvider := clusterctl.DefaultInfrastructureProvider
if input.InfrastructureProvider != nil {
infrastructureProvider = *input.InfrastructureProvider
}
clusterctl.ApplyClusterTemplateAndWait(ctx, clusterctl.ApplyClusterTemplateAndWaitInput{
ClusterProxy: input.BootstrapClusterProxy,
ConfigCluster: clusterctl.ConfigClusterInput{
LogFolder: filepath.Join(input.ArtifactFolder, "clusters", input.BootstrapClusterProxy.GetName()),
ClusterctlConfigPath: input.ClusterctlConfigPath,
KubeconfigPath: input.BootstrapClusterProxy.GetKubeconfigPath(),
InfrastructureProvider: infrastructureProvider,
Flavor: input.Flavor,
Namespace: namespace.Name,
ClusterName: fmt.Sprintf("%s-%s", specName, util.RandomString(6)),
KubernetesVersion: input.E2EConfig.MustGetVariable(KubernetesVersion),
ControlPlaneMachineCount: ptr.To[int64](1),
WorkerMachineCount: ptr.To[int64](1),
},
ControlPlaneWaiters: input.ControlPlaneWaiters,
WaitForClusterIntervals: input.E2EConfig.GetIntervals(specName, "wait-cluster"),
WaitForControlPlaneIntervals: input.E2EConfig.GetIntervals(specName, "wait-control-plane"),
WaitForMachineDeployments: input.E2EConfig.GetIntervals(specName, "wait-worker-nodes"),
WaitForMachinePools: input.E2EConfig.GetIntervals(specName, "wait-machine-pool-nodes"),
}, clusterResources)
assertClusterObjects(ctx, input.BootstrapClusterProxy, clusterResources.Cluster, clusterResources.ClusterClass, input.FilterMetadataBeforeValidation)
By("Rolling out changes to control plane, MachineDeployments, and MachinePools (in-place)")
machinesBeforeUpgrade := getMachinesByCluster(ctx, input.BootstrapClusterProxy.GetClient(), clusterResources.Cluster)
By("Modifying the control plane configuration via Cluster topology and wait for changes to be applied to the control plane object (in-place)")
modifyControlPlaneViaClusterAndWait(ctx, modifyControlPlaneViaClusterAndWaitInput{
ClusterProxy: input.BootstrapClusterProxy,
Cluster: clusterResources.Cluster,
ModifyControlPlaneTopology: func(topology *clusterv1.ControlPlaneTopology) {
// Drop existing labels and annotations and set new ones.
topology.Metadata.Labels = map[string]string{
"Cluster.topology.controlPlane.newLabel": "Cluster.topology.controlPlane.newLabelValue",
}
topology.Metadata.Annotations = map[string]string{
"Cluster.topology.controlPlane.newAnnotation": "Cluster.topology.controlPlane.newAnnotationValue",
}
topology.NodeDrainTimeout = &metav1.Duration{Duration: time.Duration(rand.Intn(20)) * time.Second} //nolint:gosec
topology.NodeDeletionTimeout = &metav1.Duration{Duration: time.Duration(rand.Intn(20)) * time.Second} //nolint:gosec
topology.NodeVolumeDetachTimeout = &metav1.Duration{Duration: time.Duration(rand.Intn(20)) * time.Second} //nolint:gosec
},
WaitForControlPlane: input.E2EConfig.GetIntervals(specName, "wait-control-plane"),
})
By("Modifying the MachineDeployments configuration via Cluster topology and wait for changes to be applied to the MachineDeployments (in-place)")
modifyMachineDeploymentViaClusterAndWait(ctx, modifyMachineDeploymentViaClusterAndWaitInput{
ClusterProxy: input.BootstrapClusterProxy,
Cluster: clusterResources.Cluster,
ModifyMachineDeploymentTopology: func(topology *clusterv1.MachineDeploymentTopology) {
// Drop existing labels and annotations and set new ones.
topology.Metadata.Labels = map[string]string{
"Cluster.topology.machineDeployment.newLabel": "Cluster.topology.machineDeployment.newLabelValue",
}
topology.Metadata.Annotations = map[string]string{
"Cluster.topology.machineDeployment.newAnnotation": "Cluster.topology.machineDeployment.newAnnotationValue",
}
topology.NodeDrainTimeout = &metav1.Duration{Duration: time.Duration(rand.Intn(20)) * time.Second} //nolint:gosec
topology.NodeDeletionTimeout = &metav1.Duration{Duration: time.Duration(rand.Intn(20)) * time.Second} //nolint:gosec
topology.NodeVolumeDetachTimeout = &metav1.Duration{Duration: time.Duration(rand.Intn(20)) * time.Second} //nolint:gosec
topology.MinReadySeconds = ptr.To[int32](rand.Int31n(20)) //nolint:gosec
topology.Strategy = &clusterv1.MachineDeploymentStrategy{
Type: clusterv1.RollingUpdateMachineDeploymentStrategyType,
RollingUpdate: &clusterv1.MachineRollingUpdateDeployment{
MaxUnavailable: &intstr.IntOrString{Type: intstr.Int, IntVal: 0},
MaxSurge: &intstr.IntOrString{Type: intstr.Int, IntVal: 5 + rand.Int31n(20)}, //nolint:gosec
DeletePolicy: ptr.To(string(clusterv1.NewestMachineSetDeletePolicy)),
},
Remediation: &clusterv1.RemediationStrategy{
MaxInFlight: &intstr.IntOrString{Type: intstr.Int, IntVal: 2 + rand.Int31n(20)}, //nolint:gosec
},
}
},
WaitForMachineDeployments: input.E2EConfig.GetIntervals(specName, "wait-worker-nodes"),
})
By("Modifying the MachinePool configuration via Cluster topology and wait for changes to be applied to the MachinePools (in-place)")
modifyMachinePoolViaClusterAndWait(ctx, modifyMachinePoolViaClusterAndWaitInput{
ClusterProxy: input.BootstrapClusterProxy,
Cluster: clusterResources.Cluster,
ModifyMachinePoolTopology: func(topology *clusterv1.MachinePoolTopology) {
// Drop existing labels and annotations and set new ones.
topology.Metadata.Labels = map[string]string{
"Cluster.topology.machinePool.newLabel": "Cluster.topology.machinePool.newLabelValue",
}
topology.Metadata.Annotations = map[string]string{
"Cluster.topology.machinePool.newAnnotation": "Cluster.topology.machinePool.newAnnotationValue",
}
topology.NodeDrainTimeout = &metav1.Duration{Duration: time.Duration(rand.Intn(20)) * time.Second} //nolint:gosec
topology.NodeDeletionTimeout = &metav1.Duration{Duration: time.Duration(rand.Intn(20)) * time.Second} //nolint:gosec
topology.NodeVolumeDetachTimeout = &metav1.Duration{Duration: time.Duration(rand.Intn(20)) * time.Second} //nolint:gosec
topology.MinReadySeconds = ptr.To[int32](rand.Int31n(20)) //nolint:gosec
},
WaitForMachinePools: input.E2EConfig.GetIntervals(specName, "wait-machine-pool-nodes"),
})
By("Verifying there are no unexpected rollouts through in-place rollout")
Consistently(func(g Gomega) {
machinesAfterUpgrade := getMachinesByCluster(ctx, input.BootstrapClusterProxy.GetClient(), clusterResources.Cluster)
g.Expect(machinesAfterUpgrade.Equal(machinesBeforeUpgrade)).To(BeTrue(), "Machines must not be replaced through in-place rollout")
}, 30*time.Second, 1*time.Second).Should(Succeed())
assertClusterObjects(ctx, input.BootstrapClusterProxy, clusterResources.Cluster, clusterResources.ClusterClass, input.FilterMetadataBeforeValidation)
By("Rolling out changes to control plane, MachineDeployments, and MachinePools (rollout)")
machinesBeforeUpgrade = getMachinesByCluster(ctx, input.BootstrapClusterProxy.GetClient(), clusterResources.Cluster)
By("Modifying the control plane configuration via ClusterClass and wait for changes to be applied to the control plane object (rollout)")
modifyControlPlaneViaClusterClassAndWait(ctx, modifyClusterClassControlPlaneAndWaitInput{
ClusterProxy: input.BootstrapClusterProxy,
ClusterClass: clusterResources.ClusterClass,
Cluster: clusterResources.Cluster,
ModifyControlPlaneFields: map[string]interface{}{
"spec.kubeadmConfigSpec.verbosity": int64(4),
},
WaitForControlPlane: input.E2EConfig.GetIntervals(specName, "wait-control-plane"),
})
By("Modifying the MachineDeployment configuration via ClusterClass and wait for changes to be applied to the MachineDeployments (rollout)")
modifyMachineDeploymentViaClusterClassAndWait(ctx, modifyMachineDeploymentViaClusterClassAndWaitInput{
ClusterProxy: input.BootstrapClusterProxy,
ClusterClass: clusterResources.ClusterClass,
Cluster: clusterResources.Cluster,
ModifyBootstrapConfigTemplateFields: map[string]interface{}{
"spec.template.spec.verbosity": int64(4),
},
WaitForMachineDeployments: input.E2EConfig.GetIntervals(specName, "wait-worker-nodes"),
})
By("Modifying the MachinePool configuration via ClusterClass and wait for changes to be applied to the MachinePools (rollout)")
modifyMachinePoolViaClusterClassAndWait(ctx, modifyMachinePoolViaClusterClassAndWaitInput{
ClusterProxy: input.BootstrapClusterProxy,
ClusterClass: clusterResources.ClusterClass,
Cluster: clusterResources.Cluster,
ModifyBootstrapConfigTemplateFields: map[string]interface{}{
"spec.template.spec.verbosity": int64(4),
},
WaitForMachinePools: input.E2EConfig.GetIntervals(specName, "wait-machine-pool-nodes"),
})
By("Verifying all Machines are replaced through rollout")
Eventually(func(g Gomega) {
// Note: This excludes MachinePool Machines as they are not replaced by rollout yet.
// This is tracked by https://github.com/kubernetes-sigs/cluster-api/issues/8858.
machinesAfterUpgrade := getMachinesByCluster(ctx, input.BootstrapClusterProxy.GetClient(), clusterResources.Cluster)
g.Expect(machinesAfterUpgrade.HasAny(machinesBeforeUpgrade.UnsortedList()...)).To(BeFalse(), "All Machines must be replaced through rollout")
}, input.E2EConfig.GetIntervals(specName, "wait-control-plane")...).Should(Succeed())
assertClusterObjects(ctx, input.BootstrapClusterProxy, clusterResources.Cluster, clusterResources.ClusterClass, input.FilterMetadataBeforeValidation)
By("Rolling out control plane and MachineDeployment (rolloutAfter)")
machinesBeforeUpgrade = getMachinesByCluster(ctx, input.BootstrapClusterProxy.GetClient(), clusterResources.Cluster)
By("Setting rolloutAfter on control plane")
Eventually(func(g Gomega) {
kcp := clusterResources.ControlPlane
g.Expect(input.BootstrapClusterProxy.GetClient().Get(ctx, client.ObjectKeyFromObject(kcp), kcp)).To(Succeed())
patchHelper, err := patch.NewHelper(kcp, input.BootstrapClusterProxy.GetClient())
g.Expect(err).ToNot(HaveOccurred())
kcp.Spec.RolloutAfter = &metav1.Time{Time: time.Now()}
g.Expect(patchHelper.Patch(ctx, kcp)).To(Succeed())
}, 10*time.Second, 1*time.Second).Should(Succeed())
By("Setting rolloutAfter on MachineDeployments")
for _, md := range clusterResources.MachineDeployments {
Eventually(func(g Gomega) {
g.Expect(input.BootstrapClusterProxy.GetClient().Get(ctx, client.ObjectKeyFromObject(md), md)).To(Succeed())
patchHelper, err := patch.NewHelper(md, input.BootstrapClusterProxy.GetClient())
g.Expect(err).ToNot(HaveOccurred())
md.Spec.RolloutAfter = &metav1.Time{Time: time.Now()}
g.Expect(patchHelper.Patch(ctx, md)).To(Succeed())
}, 10*time.Second, 1*time.Second).Should(Succeed())
}
By("Verifying all Machines are replaced through rolloutAfter")
Eventually(func(g Gomega) {
machinesAfterUpgrade := getMachinesByCluster(ctx, input.BootstrapClusterProxy.GetClient(), clusterResources.Cluster)
g.Expect(machinesAfterUpgrade.HasAny(machinesBeforeUpgrade.UnsortedList()...)).To(BeFalse(), "All Machines must be replaced through rollout with rolloutAfter")
}, input.E2EConfig.GetIntervals(specName, "wait-machine-upgrade")...).Should(Succeed())
assertClusterObjects(ctx, input.BootstrapClusterProxy, clusterResources.Cluster, clusterResources.ClusterClass, input.FilterMetadataBeforeValidation)
By("PASSED!")
})
AfterEach(func() {
// Dumps all the resources in the spec namespace, then cleanups the cluster object and the spec namespace itself.
framework.DumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace, cancelWatches, clusterResources.Cluster, input.E2EConfig.GetIntervals, input.SkipCleanup)
})
}
// assertClusterObjects asserts cluster objects by checking that all objects have the right labels, annotations and selectors.
func assertClusterObjects(ctx context.Context, clusterProxy framework.ClusterProxy, cluster *clusterv1.Cluster, clusterClass *clusterv1.ClusterClass, filterMetadataBeforeValidation func(object client.Object) clusterv1.ObjectMeta) {
By("Checking cluster objects have the right labels, annotations and selectors")
Eventually(func(g Gomega) {
// Get Cluster and ClusterClass objects.
clusterObjects := getClusterObjects(ctx, g, clusterProxy, cluster)
clusterClassObjects := getClusterClassObjects(ctx, g, clusterProxy, clusterClass)
// InfrastructureCluster
assertInfrastructureCluster(g, clusterClassObjects, clusterObjects, cluster, clusterClass)
// ControlPlane
assertControlPlane(g, clusterClassObjects, clusterObjects, cluster, clusterClass)
assertControlPlaneMachines(g, clusterObjects, cluster, filterMetadataBeforeValidation)
// MachineDeployments
assertMachineDeployments(g, clusterClassObjects, clusterObjects, cluster, clusterClass)
assertMachineSets(g, clusterObjects, cluster)
assertMachineSetsMachines(g, clusterObjects, cluster, filterMetadataBeforeValidation)
// MachinePools
assertMachinePools(g, clusterClassObjects, clusterObjects, cluster, clusterClass)
By("All cluster objects have the right labels, annotations and selectors")
}, 30*time.Second, 1*time.Second).Should(Succeed())
}
func assertInfrastructureCluster(g Gomega, clusterClassObjects clusterClassObjects, clusterObjects clusterObjects, cluster *clusterv1.Cluster, clusterClass *clusterv1.ClusterClass) {
ccInfrastructureClusterTemplateTemplateMetadata := mustMetadata(contract.InfrastructureClusterTemplate().Template().Metadata().Get(clusterClassObjects.InfrastructureClusterTemplate))
// InfrastructureCluster.metadata
expectMapsToBeEquivalent(g, clusterObjects.InfrastructureCluster.GetLabels(),
union(
map[string]string{
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.ClusterTopologyOwnedLabel: "",
},
ccInfrastructureClusterTemplateTemplateMetadata.Labels,
),
)
expectMapsToBeEquivalent(g, clusterObjects.InfrastructureCluster.GetAnnotations(),
union(
map[string]string{
clusterv1.TemplateClonedFromGroupKindAnnotation: groupKind(clusterClass.Spec.Infrastructure.Ref),
clusterv1.TemplateClonedFromNameAnnotation: clusterClass.Spec.Infrastructure.Ref.Name,
},
ccInfrastructureClusterTemplateTemplateMetadata.Annotations,
),
)
}
func assertControlPlane(g Gomega, clusterClassObjects clusterClassObjects, clusterObjects clusterObjects, cluster *clusterv1.Cluster, clusterClass *clusterv1.ClusterClass) {
ccControlPlaneTemplateTemplateMetadata := mustMetadata(contract.ControlPlaneTemplate().Template().Metadata().Get(clusterClassObjects.ControlPlaneTemplate))
ccControlPlaneTemplateMachineTemplateMetadata := mustMetadata(contract.ControlPlaneTemplate().Template().MachineTemplate().Metadata().Get(clusterClassObjects.ControlPlaneTemplate))
ccControlPlaneInfrastructureMachineTemplateTemplateMetadata := mustMetadata(contract.InfrastructureMachineTemplate().Template().Metadata().Get(clusterClassObjects.ControlPlaneInfrastructureMachineTemplate))
controlPlaneMachineTemplateMetadata := mustMetadata(contract.ControlPlane().MachineTemplate().Metadata().Get(clusterObjects.ControlPlane))
controlPlaneInfrastructureMachineTemplateTemplateMetadata := mustMetadata(contract.InfrastructureMachineTemplate().Template().Metadata().Get(clusterObjects.ControlPlaneInfrastructureMachineTemplate))
// ControlPlane.metadata
expectMapsToBeEquivalent(g, clusterObjects.ControlPlane.GetLabels(),
union(
map[string]string{
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.ClusterTopologyOwnedLabel: "",
},
cluster.Spec.Topology.ControlPlane.Metadata.Labels,
clusterClass.Spec.ControlPlane.Metadata.Labels,
ccControlPlaneTemplateTemplateMetadata.Labels,
),
)
expectMapsToBeEquivalent(g, clusterObjects.ControlPlane.GetAnnotations(),
union(
map[string]string{
clusterv1.TemplateClonedFromGroupKindAnnotation: groupKind(clusterClass.Spec.ControlPlane.Ref),
clusterv1.TemplateClonedFromNameAnnotation: clusterClass.Spec.ControlPlane.Ref.Name,
},
cluster.Spec.Topology.ControlPlane.Metadata.Annotations,
clusterClass.Spec.ControlPlane.Metadata.Annotations,
ccControlPlaneTemplateTemplateMetadata.Annotations,
),
)
// ControlPlane.spec.machineTemplate.metadata
expectMapsToBeEquivalent(g, controlPlaneMachineTemplateMetadata.Labels,
union(
map[string]string{
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.ClusterTopologyOwnedLabel: "",
},
cluster.Spec.Topology.ControlPlane.Metadata.Labels,
clusterClass.Spec.ControlPlane.Metadata.Labels,
ccControlPlaneTemplateMachineTemplateMetadata.Labels,
),
)
expectMapsToBeEquivalent(g, controlPlaneMachineTemplateMetadata.Annotations,
union(
cluster.Spec.Topology.ControlPlane.Metadata.Annotations,
clusterClass.Spec.ControlPlane.Metadata.Annotations,
ccControlPlaneTemplateMachineTemplateMetadata.Annotations,
),
)
// ControlPlane InfrastructureMachineTemplate.metadata
expectMapsToBeEquivalent(g, clusterObjects.ControlPlaneInfrastructureMachineTemplate.GetLabels(),
union(
map[string]string{
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.ClusterTopologyOwnedLabel: "",
},
clusterClassObjects.ControlPlaneInfrastructureMachineTemplate.GetLabels(),
),
)
expectMapsToBeEquivalent(g, clusterObjects.ControlPlaneInfrastructureMachineTemplate.GetAnnotations(),
union(
map[string]string{
clusterv1.TemplateClonedFromGroupKindAnnotation: groupKind(clusterClass.Spec.ControlPlane.MachineInfrastructure.Ref),
clusterv1.TemplateClonedFromNameAnnotation: clusterClass.Spec.ControlPlane.MachineInfrastructure.Ref.Name,
},
clusterClassObjects.ControlPlaneInfrastructureMachineTemplate.GetAnnotations(),
),
)
// ControlPlane InfrastructureMachineTemplate.spec.template.metadata
expectMapsToBeEquivalent(g, controlPlaneInfrastructureMachineTemplateTemplateMetadata.Labels,
ccControlPlaneInfrastructureMachineTemplateTemplateMetadata.Labels,
)
expectMapsToBeEquivalent(g, controlPlaneInfrastructureMachineTemplateTemplateMetadata.Annotations,
ccControlPlaneInfrastructureMachineTemplateTemplateMetadata.Annotations,
)
}
func assertControlPlaneMachines(g Gomega, clusterObjects clusterObjects, cluster *clusterv1.Cluster, filterMetadataBeforeValidation func(object client.Object) clusterv1.ObjectMeta) {
controlPlaneMachineTemplateMetadata := mustMetadata(contract.ControlPlane().MachineTemplate().Metadata().Get(clusterObjects.ControlPlane))
controlPlaneInfrastructureMachineTemplateTemplateMetadata := mustMetadata(contract.InfrastructureMachineTemplate().Template().Metadata().Get(clusterObjects.ControlPlaneInfrastructureMachineTemplate))
for _, machine := range clusterObjects.ControlPlaneMachines {
// ControlPlane Machine.metadata
machineMetadata := filterMetadataBeforeValidation(machine)
expectMapsToBeEquivalent(g, machineMetadata.Labels,
union(
map[string]string{
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.ClusterTopologyOwnedLabel: "",
clusterv1.MachineControlPlaneLabel: "",
clusterv1.MachineControlPlaneNameLabel: clusterObjects.ControlPlane.GetName(),
},
controlPlaneMachineTemplateMetadata.Labels,
),
)
expectMapsToBeEquivalent(g,
union(
machineMetadata.Annotations,
).without(g, controlplanev1.KubeadmClusterConfigurationAnnotation, controlplanev1.PreTerminateHookCleanupAnnotation),
controlPlaneMachineTemplateMetadata.Annotations,
)
// ControlPlane Machine InfrastructureMachine.metadata
infrastructureMachine := clusterObjects.InfrastructureMachineByMachine[machine.Name]
infrastructureMachineMetadata := filterMetadataBeforeValidation(infrastructureMachine)
controlPlaneMachineTemplateInfrastructureRef, err := contract.ControlPlane().MachineTemplate().InfrastructureRef().Get(clusterObjects.ControlPlane)
g.Expect(err).ToNot(HaveOccurred())
expectMapsToBeEquivalent(g, infrastructureMachineMetadata.Labels,
union(
map[string]string{
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.ClusterTopologyOwnedLabel: "",
clusterv1.MachineControlPlaneLabel: "",
clusterv1.MachineControlPlaneNameLabel: clusterObjects.ControlPlane.GetName(),
},
controlPlaneMachineTemplateMetadata.Labels,
controlPlaneInfrastructureMachineTemplateTemplateMetadata.Labels,
),
)
expectMapsToBeEquivalent(g, infrastructureMachineMetadata.Annotations,
union(
map[string]string{
clusterv1.TemplateClonedFromGroupKindAnnotation: groupKind(controlPlaneMachineTemplateInfrastructureRef),
clusterv1.TemplateClonedFromNameAnnotation: controlPlaneMachineTemplateInfrastructureRef.Name,
},
controlPlaneMachineTemplateMetadata.Annotations,
controlPlaneInfrastructureMachineTemplateTemplateMetadata.Annotations,
),
)
// ControlPlane Machine BootstrapConfig.metadata
bootstrapConfig := clusterObjects.BootstrapConfigByMachine[machine.Name]
bootstrapConfigMetadata := filterMetadataBeforeValidation(bootstrapConfig)
expectMapsToBeEquivalent(g, bootstrapConfigMetadata.Labels,
union(
map[string]string{
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.ClusterTopologyOwnedLabel: "",
clusterv1.MachineControlPlaneLabel: "",
clusterv1.MachineControlPlaneNameLabel: clusterObjects.ControlPlane.GetName(),
},
controlPlaneMachineTemplateMetadata.Labels,
),
)
expectMapsToBeEquivalent(g,
union(
bootstrapConfigMetadata.Annotations,
).without(g, clusterv1.MachineCertificatesExpiryDateAnnotation),
controlPlaneMachineTemplateMetadata.Annotations,
)
// ControlPlane Machine Node.metadata
node := clusterObjects.NodesByMachine[machine.Name]
nodeMetadata := filterMetadataBeforeValidation(node)
for k, v := range getManagedLabels(machineMetadata.Labels) {
g.Expect(nodeMetadata.Labels).To(HaveKeyWithValue(k, v))
}
}
}
func assertMachineDeployments(g Gomega, clusterClassObjects clusterClassObjects, clusterObjects clusterObjects, cluster *clusterv1.Cluster, clusterClass *clusterv1.ClusterClass) {
for _, machineDeployment := range clusterObjects.MachineDeployments {
mdTopology := getMDTopology(cluster, machineDeployment)
mdClass := getMDClass(cluster, clusterClass, machineDeployment)
// MachineDeployment.metadata
expectMapsToBeEquivalent(g, machineDeployment.Labels,
union(
map[string]string{
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.ClusterTopologyOwnedLabel: "",
clusterv1.ClusterTopologyMachineDeploymentNameLabel: mdTopology.Name,
},
mdTopology.Metadata.Labels,
mdClass.Template.Metadata.Labels,
),
)
expectMapsToBeEquivalent(g,
union(
machineDeployment.Annotations,
).without(g, clusterv1.RevisionAnnotation),
union(
mdTopology.Metadata.Annotations,
mdClass.Template.Metadata.Annotations,
),
)
// MachineDeployment.spec.selector
expectMapsToBeEquivalent(g, machineDeployment.Spec.Selector.MatchLabels,
union(
map[string]string{
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.ClusterTopologyOwnedLabel: "",
clusterv1.ClusterTopologyMachineDeploymentNameLabel: mdTopology.Name,
},
),
)
// MachineDeployment.spec.template.metadata
expectMapsToBeEquivalent(g, machineDeployment.Spec.Template.Labels,
union(
map[string]string{
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.ClusterTopologyOwnedLabel: "",
clusterv1.ClusterTopologyMachineDeploymentNameLabel: mdTopology.Name,
},
mdTopology.Metadata.Labels,
mdClass.Template.Metadata.Labels,
),
)
expectMapsToBeEquivalent(g, machineDeployment.Spec.Template.Annotations,
union(
mdTopology.Metadata.Annotations,
mdClass.Template.Metadata.Annotations,
),
)
// MachineDeployment InfrastructureMachineTemplate.metadata
ccInfrastructureMachineTemplate := clusterClassObjects.InfrastructureMachineTemplateByMachineDeploymentClass[mdClass.Class]
ccInfrastructureMachineTemplateTemplateMetadata := mustMetadata(contract.InfrastructureMachineTemplate().Template().Metadata().Get(ccInfrastructureMachineTemplate))
infrastructureMachineTemplate := clusterObjects.InfrastructureMachineTemplateByMachineDeployment[machineDeployment.Name]
infrastructureMachineTemplateTemplateMetadata := mustMetadata(contract.InfrastructureMachineTemplate().Template().Metadata().Get(infrastructureMachineTemplate))
expectMapsToBeEquivalent(g, infrastructureMachineTemplate.GetLabels(),
union(
map[string]string{
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.ClusterTopologyOwnedLabel: "",
clusterv1.ClusterTopologyMachineDeploymentNameLabel: mdTopology.Name,
},
ccInfrastructureMachineTemplate.GetLabels(),
),
)
expectMapsToBeEquivalent(g, infrastructureMachineTemplate.GetAnnotations(),
union(
map[string]string{
clusterv1.TemplateClonedFromGroupKindAnnotation: groupKind(mdClass.Template.Infrastructure.Ref),
clusterv1.TemplateClonedFromNameAnnotation: mdClass.Template.Infrastructure.Ref.Name,
},
ccInfrastructureMachineTemplate.GetAnnotations(),
),
)
// MachineDeployment InfrastructureMachineTemplate.spec.template.metadata
expectMapsToBeEquivalent(g, infrastructureMachineTemplateTemplateMetadata.Labels,
ccInfrastructureMachineTemplateTemplateMetadata.Labels,
)
expectMapsToBeEquivalent(g, infrastructureMachineTemplateTemplateMetadata.Annotations,
ccInfrastructureMachineTemplateTemplateMetadata.Annotations,
)
// MachineDeployment BootstrapConfigTemplate.metadata
ccBootstrapConfigTemplate := clusterClassObjects.BootstrapConfigTemplateByMachineDeploymentClass[mdClass.Class]
ccBootstrapConfigTemplateTemplateMetadata := mustMetadata(contract.BootstrapConfigTemplate().Template().Metadata().Get(ccBootstrapConfigTemplate))
bootstrapConfigTemplate := clusterObjects.BootstrapConfigTemplateByMachineDeployment[machineDeployment.Name]
bootstrapConfigTemplateTemplateMetadata := mustMetadata(contract.BootstrapConfigTemplate().Template().Metadata().Get(bootstrapConfigTemplate))
expectMapsToBeEquivalent(g, bootstrapConfigTemplate.GetLabels(),
union(
map[string]string{
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.ClusterTopologyOwnedLabel: "",
clusterv1.ClusterTopologyMachineDeploymentNameLabel: mdTopology.Name,
},
ccBootstrapConfigTemplate.GetLabels(),
),
)
expectMapsToBeEquivalent(g, bootstrapConfigTemplate.GetAnnotations(),
union(
map[string]string{
clusterv1.TemplateClonedFromGroupKindAnnotation: groupKind(mdClass.Template.Bootstrap.Ref),
clusterv1.TemplateClonedFromNameAnnotation: mdClass.Template.Bootstrap.Ref.Name,
},
ccBootstrapConfigTemplate.GetAnnotations(),
),
)
// MachineDeployment BootstrapConfigTemplate.spec.template.metadata
expectMapsToBeEquivalent(g, bootstrapConfigTemplateTemplateMetadata.Labels,
ccBootstrapConfigTemplateTemplateMetadata.Labels,
)
expectMapsToBeEquivalent(g, bootstrapConfigTemplateTemplateMetadata.Annotations,
ccBootstrapConfigTemplateTemplateMetadata.Annotations,
)
}
}
func assertMachinePools(g Gomega, clusterClassObjects clusterClassObjects, clusterObjects clusterObjects, cluster *clusterv1.Cluster, clusterClass *clusterv1.ClusterClass) {
for _, machinePool := range clusterObjects.MachinePools {
mpTopology := getMPTopology(cluster, machinePool)
mpClass := getMPClass(cluster, clusterClass, machinePool)
// MachinePool.metadata
expectMapsToBeEquivalent(g, machinePool.Labels,
union(
map[string]string{
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.ClusterTopologyOwnedLabel: "",
clusterv1.ClusterTopologyMachinePoolNameLabel: mpTopology.Name,
},
mpTopology.Metadata.Labels,
mpClass.Template.Metadata.Labels,
),
)
expectMapsToBeEquivalent(g, machinePool.Annotations,
union(
mpTopology.Metadata.Annotations,
mpClass.Template.Metadata.Annotations,
),
)
// MachinePool.spec.template.metadata
expectMapsToBeEquivalent(g, machinePool.Spec.Template.Labels,
union(
map[string]string{
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.ClusterTopologyOwnedLabel: "",
clusterv1.ClusterTopologyMachinePoolNameLabel: mpTopology.Name,
},
mpTopology.Metadata.Labels,
mpClass.Template.Metadata.Labels,
),
)
expectMapsToBeEquivalent(g, machinePool.Spec.Template.Annotations,
union(
mpTopology.Metadata.Annotations,
mpClass.Template.Metadata.Annotations,
),
)
// MachinePool InfrastructureMachinePool.metadata
ccInfrastructureMachinePoolTemplate := clusterClassObjects.InfrastructureMachinePoolTemplateByMachinePoolClass[mpClass.Class]
ccInfrastructureMachinePoolTemplateTemplateMetadata := mustMetadata(contract.InfrastructureMachinePoolTemplate().Template().Metadata().Get(ccInfrastructureMachinePoolTemplate))
infrastructureMachinePool := clusterObjects.InfrastructureMachinePoolByMachinePool[machinePool.Name]
expectMapsToBeEquivalent(g, infrastructureMachinePool.GetLabels(),
union(
map[string]string{
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.ClusterTopologyOwnedLabel: "",
clusterv1.ClusterTopologyMachinePoolNameLabel: mpTopology.Name,
},
ccInfrastructureMachinePoolTemplateTemplateMetadata.Labels,
),
)
expectMapsToBeEquivalent(g, infrastructureMachinePool.GetAnnotations(),
union(
map[string]string{
clusterv1.TemplateClonedFromGroupKindAnnotation: groupKind(mpClass.Template.Infrastructure.Ref),
clusterv1.TemplateClonedFromNameAnnotation: mpClass.Template.Infrastructure.Ref.Name,
},
ccInfrastructureMachinePoolTemplateTemplateMetadata.Annotations,
),
)
// MachinePool BootstrapConfig.metadata
ccBootstrapConfigTemplate := clusterClassObjects.BootstrapConfigTemplateByMachinePoolClass[mpClass.Class]
ccBootstrapConfigTemplateTemplateMetadata := mustMetadata(contract.BootstrapConfigTemplate().Template().Metadata().Get(ccBootstrapConfigTemplate))
bootstrapConfig := clusterObjects.BootstrapConfigByMachinePool[machinePool.Name]
expectMapsToBeEquivalent(g, bootstrapConfig.GetLabels(),
union(
map[string]string{
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.ClusterTopologyOwnedLabel: "",
clusterv1.ClusterTopologyMachinePoolNameLabel: mpTopology.Name,
},
ccBootstrapConfigTemplateTemplateMetadata.Labels,
),
)
expectMapsToBeEquivalent(g, bootstrapConfig.GetAnnotations(),
union(
map[string]string{
clusterv1.TemplateClonedFromGroupKindAnnotation: groupKind(mpClass.Template.Bootstrap.Ref),
clusterv1.TemplateClonedFromNameAnnotation: mpClass.Template.Bootstrap.Ref.Name,
},
ccBootstrapConfigTemplateTemplateMetadata.Annotations,
),
)
}
}
func assertMachineSets(g Gomega, clusterObjects clusterObjects, cluster *clusterv1.Cluster) {
for _, machineDeployment := range clusterObjects.MachineDeployments {
mdTopology := getMDTopology(cluster, machineDeployment)
for _, machineSet := range clusterObjects.MachineSetsByMachineDeployment[machineDeployment.Name] {
machineTemplateHash := machineSet.Labels[clusterv1.MachineDeploymentUniqueLabel]
// MachineDeployment MachineSet.metadata
expectMapsToBeEquivalent(g, machineSet.Labels,
union(
map[string]string{
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.ClusterTopologyOwnedLabel: "",
clusterv1.ClusterTopologyMachineDeploymentNameLabel: mdTopology.Name,
clusterv1.MachineDeploymentNameLabel: machineDeployment.Name,
clusterv1.MachineDeploymentUniqueLabel: machineTemplateHash,
},
machineDeployment.Spec.Template.Labels,
),
)
expectMapsToBeEquivalent(g,
union(
machineSet.Annotations,
).without(g, clusterv1.DesiredReplicasAnnotation, clusterv1.MaxReplicasAnnotation, clusterv1.RevisionAnnotation),
union(
machineDeployment.Annotations,
).without(g, clusterv1.RevisionAnnotation),
)
// MachineDeployment MachineSet.spec.selector
expectMapsToBeEquivalent(g, machineSet.Spec.Selector.MatchLabels,
union(
map[string]string{
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.ClusterTopologyOwnedLabel: "",
clusterv1.ClusterTopologyMachineDeploymentNameLabel: mdTopology.Name,
clusterv1.MachineDeploymentUniqueLabel: machineTemplateHash,
},
),
)
// MachineDeployment MachineSet.spec.template.metadata
expectMapsToBeEquivalent(g, machineSet.Spec.Template.Labels,
union(
map[string]string{
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.ClusterTopologyOwnedLabel: "",
clusterv1.ClusterTopologyMachineDeploymentNameLabel: mdTopology.Name,
clusterv1.MachineDeploymentUniqueLabel: machineTemplateHash,
},
machineDeployment.Spec.Template.Labels,
),
)
expectMapsToBeEquivalent(g, machineSet.Spec.Template.Annotations,
machineDeployment.Spec.Template.Annotations,
)
}
}
}
func assertMachineSetsMachines(g Gomega, clusterObjects clusterObjects, cluster *clusterv1.Cluster, filterMetadataBeforeValidation func(object client.Object) clusterv1.ObjectMeta) {
for _, machineDeployment := range clusterObjects.MachineDeployments {
mdTopology := getMDTopology(cluster, machineDeployment)
infrastructureMachineTemplate := clusterObjects.InfrastructureMachineTemplateByMachineDeployment[machineDeployment.Name]
infrastructureMachineTemplateTemplateMetadata := mustMetadata(contract.InfrastructureMachineTemplate().Template().Metadata().Get(infrastructureMachineTemplate))
bootstrapConfigTemplate := clusterObjects.BootstrapConfigTemplateByMachineDeployment[machineDeployment.Name]
bootstrapConfigTemplateTemplateMetadata := mustMetadata(contract.BootstrapConfigTemplate().Template().Metadata().Get(bootstrapConfigTemplate))
for _, machineSet := range clusterObjects.MachineSetsByMachineDeployment[machineDeployment.Name] {
machineTemplateHash := machineSet.Labels[clusterv1.MachineDeploymentUniqueLabel]
for _, machine := range clusterObjects.MachinesByMachineSet[machineSet.Name] {
machineMetadata := filterMetadataBeforeValidation(machine)
// MachineDeployment MachineSet Machine.metadata
expectMapsToBeEquivalent(g, machineMetadata.Labels,
union(
map[string]string{
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.ClusterTopologyOwnedLabel: "",
clusterv1.ClusterTopologyMachineDeploymentNameLabel: mdTopology.Name,
clusterv1.MachineDeploymentNameLabel: machineDeployment.Name,
clusterv1.MachineSetNameLabel: machineSet.Name,
clusterv1.MachineDeploymentUniqueLabel: machineTemplateHash,
},
machineSet.Spec.Template.Labels,
),
)
expectMapsToBeEquivalent(g, machineMetadata.Annotations,
machineSet.Spec.Template.Annotations,
)
// MachineDeployment MachineSet Machine InfrastructureMachine.metadata
infrastructureMachine := clusterObjects.InfrastructureMachineByMachine[machine.Name]
infrastructureMachineMetadata := filterMetadataBeforeValidation(infrastructureMachine)
expectMapsToBeEquivalent(g, infrastructureMachineMetadata.Labels,
union(
map[string]string{
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.ClusterTopologyOwnedLabel: "",
clusterv1.ClusterTopologyMachineDeploymentNameLabel: mdTopology.Name,
clusterv1.MachineDeploymentNameLabel: machineDeployment.Name,
clusterv1.MachineSetNameLabel: machineSet.Name,
clusterv1.MachineDeploymentUniqueLabel: machineTemplateHash,
},
machineSet.Spec.Template.Labels,
infrastructureMachineTemplateTemplateMetadata.Labels,
),
)
expectMapsToBeEquivalent(g, infrastructureMachineMetadata.Annotations,
union(
map[string]string{
clusterv1.TemplateClonedFromGroupKindAnnotation: groupKind(&machineSet.Spec.Template.Spec.InfrastructureRef),
clusterv1.TemplateClonedFromNameAnnotation: machineSet.Spec.Template.Spec.InfrastructureRef.Name,
},
machineSet.Spec.Template.Annotations,
infrastructureMachineTemplateTemplateMetadata.Annotations,
),
)
// MachineDeployment MachineSet Machine BootstrapConfig.metadata
bootstrapConfig := clusterObjects.BootstrapConfigByMachine[machine.Name]
bootstrapConfigMetadata := filterMetadataBeforeValidation(bootstrapConfig)
expectMapsToBeEquivalent(g, bootstrapConfigMetadata.Labels,
union(
map[string]string{
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.ClusterTopologyOwnedLabel: "",
clusterv1.ClusterTopologyMachineDeploymentNameLabel: mdTopology.Name,
clusterv1.MachineDeploymentNameLabel: machineDeployment.Name,
clusterv1.MachineSetNameLabel: machineSet.Name,
clusterv1.MachineDeploymentUniqueLabel: machineTemplateHash,
},
machineSet.Spec.Template.Labels,
bootstrapConfigTemplateTemplateMetadata.Labels,
),
)
expectMapsToBeEquivalent(g, bootstrapConfigMetadata.Annotations,
union(
map[string]string{
clusterv1.TemplateClonedFromGroupKindAnnotation: groupKind(machineSet.Spec.Template.Spec.Bootstrap.ConfigRef),
clusterv1.TemplateClonedFromNameAnnotation: machineSet.Spec.Template.Spec.Bootstrap.ConfigRef.Name,
},
machineSet.Spec.Template.Annotations,
bootstrapConfigTemplateTemplateMetadata.Annotations,
),
)
// MachineDeployment MachineSet Machine Node.metadata
node := clusterObjects.NodesByMachine[machine.Name]
nodeMetadata := filterMetadataBeforeValidation(node)
for k, v := range getManagedLabels(machineMetadata.Labels) {
g.Expect(nodeMetadata.Labels).To(HaveKeyWithValue(k, v))
}
}
}
}
}
func mustMetadata(metadata *clusterv1.ObjectMeta, err error) *clusterv1.ObjectMeta {
if err != nil {
panic(err)
}
return metadata
}
// getMachinesByCluster gets the Machines of a Cluster and returns them as a Set of Machine names.
// Note: This excludes MachinePool Machines as they are not replaced by rollout yet.
func getMachinesByCluster(ctx context.Context, client client.Client, cluster *clusterv1.Cluster) sets.Set[string] {
machines := sets.Set[string]{}
machinesByCluster := framework.GetMachinesByCluster(ctx, framework.GetMachinesByClusterInput{
Lister: client,
ClusterName: cluster.Name,
Namespace: cluster.Namespace,
})
for i := range machinesByCluster {
m := machinesByCluster[i]
if !labels.IsMachinePoolOwned(&m) {
machines.Insert(m.Name)
}
}
return machines
}
// getMDClass looks up the MachineDeploymentClass for a md in the ClusterClass.
func getMDClass(cluster *clusterv1.Cluster, clusterClass *clusterv1.ClusterClass, md *clusterv1.MachineDeployment) *clusterv1.MachineDeploymentClass {
mdTopology := getMDTopology(cluster, md)
for _, mdClass := range clusterClass.Spec.Workers.MachineDeployments {
if mdClass.Class == mdTopology.Class {
return &mdClass
}
}
Fail(fmt.Sprintf("could not find MachineDeployment class %q", mdTopology.Class))
return nil
}
// getMDTopology looks up the MachineDeploymentTopology for a md in the Cluster.
func getMDTopology(cluster *clusterv1.Cluster, md *clusterv1.MachineDeployment) *clusterv1.MachineDeploymentTopology {
for _, mdTopology := range cluster.Spec.Topology.Workers.MachineDeployments {
if mdTopology.Name == md.Labels[clusterv1.ClusterTopologyMachineDeploymentNameLabel] {
return &mdTopology
}
}
Fail(fmt.Sprintf("could not find MachineDeployment topology %q", md.Labels[clusterv1.ClusterTopologyMachineDeploymentNameLabel]))
return nil
}
// getMPClass looks up the MachinePoolClass for a MachinePool in the ClusterClass.
func getMPClass(cluster *clusterv1.Cluster, clusterClass *clusterv1.ClusterClass, mp *expv1.MachinePool) *clusterv1.MachinePoolClass {
mpTopology := getMPTopology(cluster, mp)
for _, mdClass := range clusterClass.Spec.Workers.MachinePools {
if mdClass.Class == mpTopology.Class {
return &mdClass
}
}
Fail(fmt.Sprintf("could not find MachinePool class %q", mpTopology.Class))
return nil
}
// getMPTopology looks up the MachinePoolTopology for a mp in the Cluster.
func getMPTopology(cluster *clusterv1.Cluster, mp *expv1.MachinePool) *clusterv1.MachinePoolTopology {
for _, mpTopology := range cluster.Spec.Topology.Workers.MachinePools {
if mpTopology.Name == mp.Labels[clusterv1.ClusterTopologyMachinePoolNameLabel] {
return &mpTopology
}
}
Fail(fmt.Sprintf("could not find MachinePool topology %q", mp.Labels[clusterv1.ClusterTopologyMachinePoolNameLabel]))
return nil
}
// groupKind returns the GroupKind string of a ref.
func groupKind(ref *corev1.ObjectReference) string {
gv, err := schema.ParseGroupVersion(ref.APIVersion)
Expect(err).ToNot(HaveOccurred())
gk := metav1.GroupKind{
Group: gv.Group,
Kind: ref.Kind,
}
return gk.String()
}
type unionMap map[string]string
// union merges maps.
// NOTE: In case a key exists in multiple maps, the value of the first map is preserved.
func union(maps ...map[string]string) unionMap {
res := make(map[string]string)
for i := len(maps) - 1; i >= 0; i-- {
for k, v := range maps[i] {
res[k] = v
}
}
return res