/
fake_objects.go
967 lines (864 loc) · 33.3 KB
/
fake_objects.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
/*
Copyright 2019 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 test
import (
"fmt"
"strings"
corev1 "k8s.io/api/core/v1"
apiextensionslv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
fakebootstrap "sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test/providers/bootstrap"
fakecontrolplane "sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test/providers/controlplane"
fakeinfrastructure "sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test/providers/infrastructure"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1alpha3"
)
type FakeCluster struct {
namespace string
name string
controlPlane *FakeControlPlane
machinePools []*FakeMachinePool
machineDeployments []*FakeMachineDeployment
machineSets []*FakeMachineSet
machines []*FakeMachine
}
// NewFakeCluster return a FakeCluster that can generate a cluster object, all its own ancillary objects:
// - the clusterInfrastructure object
// - the kubeconfig secret object (if there is no a control plane object)
// - a user defined ca secret
// and all the objects for the defined FakeControlPlane, FakeMachinePools, FakeMachineDeployments, FakeMachineSets, FakeMachines
// Nb. if there is no a control plane object, the first FakeMachine gets a generated sa secret
func NewFakeCluster(namespace, name string) *FakeCluster {
return &FakeCluster{
namespace: namespace,
name: name,
}
}
func (f *FakeCluster) WithControlPlane(fakeControlPlane *FakeControlPlane) *FakeCluster {
f.controlPlane = fakeControlPlane
return f
}
func (f *FakeCluster) WithMachinePools(fakeMachinePool ...*FakeMachinePool) *FakeCluster {
f.machinePools = append(f.machinePools, fakeMachinePool...)
return f
}
func (f *FakeCluster) WithMachineDeployments(fakeMachineDeployment ...*FakeMachineDeployment) *FakeCluster {
f.machineDeployments = append(f.machineDeployments, fakeMachineDeployment...)
return f
}
func (f *FakeCluster) WithMachineSets(fakeMachineSet ...*FakeMachineSet) *FakeCluster {
f.machineSets = append(f.machineSets, fakeMachineSet...)
return f
}
func (f *FakeCluster) WithMachines(fakeMachine ...*FakeMachine) *FakeCluster {
f.machines = append(f.machines, fakeMachine...)
return f
}
func (f *FakeCluster) Objs() []runtime.Object {
clusterInfrastructure := &fakeinfrastructure.DummyInfrastructureCluster{
TypeMeta: metav1.TypeMeta{
APIVersion: fakeinfrastructure.GroupVersion.String(),
Kind: "DummyInfrastructureCluster",
},
ObjectMeta: metav1.ObjectMeta{
Name: f.name,
Namespace: f.namespace,
// OwnerReferences: cluster, Added by the cluster controller (see below) -- RECONCILED
// Labels: cluster.x-k8s.io/cluster-name=cluster, Added by the cluster controller (see below) -- RECONCILED
},
}
cluster := &clusterv1.Cluster{
TypeMeta: metav1.TypeMeta{
Kind: "Cluster",
APIVersion: clusterv1.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: f.name,
Namespace: f.namespace,
// Labels: cluster.x-k8s.io/cluster-name=cluster MISSING??
},
Spec: clusterv1.ClusterSpec{
InfrastructureRef: &corev1.ObjectReference{
APIVersion: clusterInfrastructure.APIVersion,
Kind: clusterInfrastructure.Kind,
Name: clusterInfrastructure.Name,
Namespace: clusterInfrastructure.Namespace,
},
},
}
// Ensure the cluster gets a UID to be used by dependant objects for creating OwnerReferences.
setUID(cluster)
clusterInfrastructure.SetOwnerReferences([]metav1.OwnerReference{
{
APIVersion: cluster.APIVersion,
Kind: cluster.Kind,
Name: cluster.Name,
UID: cluster.UID,
},
})
clusterInfrastructure.SetLabels(map[string]string{
clusterv1.ClusterLabelName: cluster.Name,
})
caSecret := &corev1.Secret{ // provided by the user -- ** NOT RECONCILED **
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: f.name + "-ca",
Namespace: f.namespace,
},
}
objs := []runtime.Object{
cluster,
clusterInfrastructure,
caSecret,
}
// if the cluster has a control plane object
if f.controlPlane != nil {
// Adds the objects for the controlPlane
objs = append(objs, f.controlPlane.Objs(cluster)...)
} else {
// Adds the kubeconfig object generated by the cluster controller -- ** NOT RECONCILED **
kubeconfigSecret := &corev1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: f.name + "-kubeconfig",
Namespace: f.namespace,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: cluster.APIVersion,
Kind: cluster.Kind,
Name: cluster.Name,
UID: cluster.UID,
},
},
// Labels: cluster.x-k8s.io/cluster-name=cluster MISSING??
},
}
objs = append(objs, kubeconfigSecret)
}
// Adds the objects for the machinePools
for _, machinePool := range f.machinePools {
objs = append(objs, machinePool.Objs(cluster)...)
}
// Adds the objects for the machineDeployments
for _, machineDeployment := range f.machineDeployments {
objs = append(objs, machineDeployment.Objs(cluster)...)
}
// Adds the objects for the machineSets directly attached to the cluster
for _, machineSet := range f.machineSets {
objs = append(objs, machineSet.Objs(cluster, nil)...)
}
// Adds the objects for the machines directly attached to the cluster
// Nb. In case there is no control plane, the first machine is arbitrarily used to simulate the generation of certificates Secrets implemented by the bootstrap controller.
for i, machine := range f.machines {
generateCerts := false
if f.controlPlane == nil && i == 0 {
generateCerts = true
}
objs = append(objs, machine.Objs(cluster, generateCerts, nil, nil)...)
}
// Ensure all the objects gets UID.
// Nb. This adds UID to all the objects; it does not change the UID explicitly sets in advance for the objects involved in the object graphs.
for _, o := range objs {
setUID(o)
}
return objs
}
type FakeControlPlane struct {
name string
machines []*FakeMachine
}
// NewFakeControlPlane return a FakeControlPlane that can generate a controlPlane object, all its own ancillary objects:
// - the controlPlaneInfrastructure template object
// - the kubeconfig secret object
// - a generated sa secret
// and all the objects for the defined FakeMachines
func NewFakeControlPlane(name string) *FakeControlPlane {
return &FakeControlPlane{
name: name,
}
}
func (f *FakeControlPlane) WithMachines(fakeMachine ...*FakeMachine) *FakeControlPlane {
f.machines = append(f.machines, fakeMachine...)
return f
}
func (f *FakeControlPlane) Objs(cluster *clusterv1.Cluster) []runtime.Object {
controlPlaneInfrastructure := &fakeinfrastructure.DummyInfrastructureMachineTemplate{
TypeMeta: metav1.TypeMeta{
APIVersion: fakeinfrastructure.GroupVersion.String(),
Kind: "DummyInfrastructureMachineTemplate",
},
ObjectMeta: metav1.ObjectMeta{
Name: f.name,
Namespace: cluster.Namespace,
OwnerReferences: []metav1.OwnerReference{ // Added by the control plane controller (see below) -- RECONCILED
{
APIVersion: clusterv1.GroupVersion.String(),
Kind: "Cluster",
Name: cluster.Name,
UID: cluster.UID,
},
},
// Labels: MISSING
},
}
controlPlane := &fakecontrolplane.DummyControlPlane{
TypeMeta: metav1.TypeMeta{
APIVersion: fakecontrolplane.GroupVersion.String(),
Kind: "DummyControlPlane",
},
ObjectMeta: metav1.ObjectMeta{
Name: f.name,
Namespace: cluster.Namespace,
OwnerReferences: []metav1.OwnerReference{ // Added by the control plane controller (see below) -- RECONCILED
{
APIVersion: clusterv1.GroupVersion.String(),
Kind: "Cluster",
Name: cluster.Name,
UID: cluster.UID,
},
},
Labels: map[string]string{ // cluster.x-k8s.io/cluster-name=cluster, Added by the control plane controller (see below) -- RECONCILED
clusterv1.ClusterLabelName: cluster.Name,
},
},
Spec: fakecontrolplane.DummyControlPlaneSpec{
InfrastructureTemplate: corev1.ObjectReference{
APIVersion: controlPlaneInfrastructure.APIVersion,
Kind: controlPlaneInfrastructure.Kind,
Namespace: controlPlaneInfrastructure.Namespace,
Name: controlPlaneInfrastructure.Name,
},
},
}
// Ensure the controlPlane gets a UID to be used by dependant objects for creating OwnerReferences.
setUID(controlPlane)
// sets the reference from the cluster to the plane object
cluster.Spec.ControlPlaneRef = &corev1.ObjectReference{
APIVersion: controlPlane.APIVersion,
Kind: controlPlane.Kind,
Namespace: controlPlane.Namespace,
Name: controlPlane.Name,
}
// Adds the kubeconfig object generated by the control plane controller -- ** NOT RECONCILED **
kubeconfigSecret := &corev1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: cluster.GetName() + "-kubeconfig",
Namespace: cluster.GetNamespace(),
// Labels: cluster.x-k8s.io/cluster-name=cluster MISSING??
},
}
kubeconfigSecret.SetOwnerReferences([]metav1.OwnerReference{*metav1.NewControllerRef(controlPlane, controlPlane.GroupVersionKind())})
// Adds one of the certificate secret object generated by the control plane controller -- ** NOT RECONCILED **
saSecret := &corev1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: cluster.GetName() + "-sa",
Namespace: cluster.GetNamespace(),
// Labels: cluster.x-k8s.io/cluster-name=cluster MISSING??
},
}
saSecret.SetOwnerReferences([]metav1.OwnerReference{*metav1.NewControllerRef(controlPlane, controlPlane.GroupVersionKind())})
objs := []runtime.Object{
controlPlane,
controlPlaneInfrastructure,
kubeconfigSecret,
saSecret,
}
// Adds the objects for the machines controlled by the controlPlane
for _, machine := range f.machines {
objs = append(objs, machine.Objs(cluster, false, nil, controlPlane)...)
}
return objs
}
type FakeMachinePool struct {
name string
}
// NewFakeMachinePool return a FakeMachinePool that can generate a MachinePool object, all its own ancillary objects:
// - the machinePoolInfrastructure object
// - the machinePoolBootstrap object
func NewFakeMachinePool(name string) *FakeMachinePool {
return &FakeMachinePool{
name: name,
}
}
func (f *FakeMachinePool) Objs(cluster *clusterv1.Cluster) []runtime.Object {
machinePoolInfrastructure := &fakeinfrastructure.DummyInfrastructureMachineTemplate{
TypeMeta: metav1.TypeMeta{
APIVersion: fakeinfrastructure.GroupVersion.String(),
Kind: "DummyInfrastructureMachineTemplate",
},
ObjectMeta: metav1.ObjectMeta{
Name: f.name,
Namespace: cluster.Namespace,
OwnerReferences: []metav1.OwnerReference{ // Added by the machinePool controller (mirrors machinePool.spec.ClusterName) -- RECONCILED
{
APIVersion: clusterv1.GroupVersion.String(),
Kind: "Cluster",
Name: cluster.Name,
UID: cluster.UID,
},
},
// Labels: MISSING
},
}
machinePoolBootstrap := &fakebootstrap.DummyBootstrapConfigTemplate{
TypeMeta: metav1.TypeMeta{
APIVersion: fakebootstrap.GroupVersion.String(),
Kind: "DummyBootstrapConfigTemplate",
},
ObjectMeta: metav1.ObjectMeta{
Name: f.name,
Namespace: cluster.Namespace,
OwnerReferences: []metav1.OwnerReference{ // Added by the machinePool controller (mirrors machinePool.spec.ClusterName) -- RECONCILED
{
APIVersion: clusterv1.GroupVersion.String(),
Kind: "Cluster",
Name: cluster.Name,
UID: cluster.UID,
},
},
// Labels: MISSING
},
}
machinePool := &expv1.MachinePool{
TypeMeta: metav1.TypeMeta{
Kind: "MachinePool",
APIVersion: expv1.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: f.name,
Namespace: cluster.Namespace,
OwnerReferences: []metav1.OwnerReference{ // Added by the machinePool controller (mirrors machinePool.spec.ClusterName) -- RECONCILED
{
APIVersion: clusterv1.GroupVersion.String(),
Kind: "Cluster",
Name: cluster.Name,
UID: cluster.UID,
},
},
Labels: map[string]string{
clusterv1.ClusterLabelName: cluster.Name, // Added by the machinePool controller (mirrors machinePoolt.spec.ClusterName) -- RECONCILED
},
},
Spec: expv1.MachinePoolSpec{
Template: clusterv1.MachineTemplateSpec{
Spec: clusterv1.MachineSpec{
InfrastructureRef: corev1.ObjectReference{
APIVersion: machinePoolInfrastructure.APIVersion,
Kind: machinePoolInfrastructure.Kind,
Name: machinePoolInfrastructure.Name,
Namespace: machinePoolInfrastructure.Namespace,
},
Bootstrap: clusterv1.Bootstrap{
ConfigRef: &corev1.ObjectReference{
APIVersion: machinePoolBootstrap.APIVersion,
Kind: machinePoolBootstrap.Kind,
Name: machinePoolBootstrap.Name,
Namespace: machinePoolBootstrap.Namespace,
},
},
},
},
ClusterName: cluster.Name,
},
}
// Ensure the machinePool gets a UID to be used by dependant objects for creating OwnerReferences.
setUID(machinePool)
objs := []runtime.Object{
machinePool,
machinePoolInfrastructure,
machinePoolBootstrap,
}
return objs
}
func NewFakeInfrastructureTemplate(name string) *fakeinfrastructure.DummyInfrastructureMachineTemplate {
return &fakeinfrastructure.DummyInfrastructureMachineTemplate{
TypeMeta: metav1.TypeMeta{
APIVersion: fakeinfrastructure.GroupVersion.String(),
Kind: "DummyInfrastructureMachineTemplate",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
// OwnerReference Added by the machine set controller -- RECONCILED
// Labels: MISSING
},
}
}
type FakeMachineDeployment struct {
name string
machineSets []*FakeMachineSet
sharedInfrastructureTemplate *fakeinfrastructure.DummyInfrastructureMachineTemplate
}
// NewFakeMachineDeployment return a FakeMachineDeployment that can generate a MachineDeployment object, all its own ancillary objects:
// - the machineDeploymentInfrastructure template object
// - the machineDeploymentBootstrap template object
// and all the objects for the defined FakeMachineSet
func NewFakeMachineDeployment(name string) *FakeMachineDeployment {
return &FakeMachineDeployment{
name: name,
}
}
func (f *FakeMachineDeployment) WithMachineSets(fakeMachineSet ...*FakeMachineSet) *FakeMachineDeployment {
f.machineSets = append(f.machineSets, fakeMachineSet...)
return f
}
func (f *FakeMachineDeployment) WithInfrastructureTemplate(infrastructureTemplate *fakeinfrastructure.DummyInfrastructureMachineTemplate) *FakeMachineDeployment {
f.sharedInfrastructureTemplate = infrastructureTemplate
return f
}
func (f *FakeMachineDeployment) Objs(cluster *clusterv1.Cluster) []runtime.Object {
// infra template can be either shared or specific to the machine deployment
machineDeploymentInfrastructure := f.sharedInfrastructureTemplate
if machineDeploymentInfrastructure == nil {
machineDeploymentInfrastructure = NewFakeInfrastructureTemplate(f.name)
}
machineDeploymentInfrastructure.Namespace = cluster.Namespace
machineDeploymentInfrastructure.OwnerReferences = append(machineDeploymentInfrastructure.OwnerReferences, // Added by the machine set controller -- RECONCILED
metav1.OwnerReference{
APIVersion: clusterv1.GroupVersion.String(),
Kind: "Cluster",
Name: cluster.Name,
UID: cluster.UID,
},
)
setUID(machineDeploymentInfrastructure)
machineDeploymentBootstrap := &fakebootstrap.DummyBootstrapConfigTemplate{
TypeMeta: metav1.TypeMeta{
APIVersion: fakebootstrap.GroupVersion.String(),
Kind: "DummyBootstrapConfigTemplate",
},
ObjectMeta: metav1.ObjectMeta{
Name: f.name,
Namespace: cluster.Namespace,
OwnerReferences: []metav1.OwnerReference{ // Added by the machine set controller -- RECONCILED
{
APIVersion: clusterv1.GroupVersion.String(),
Kind: "Cluster",
Name: cluster.Name,
UID: cluster.UID,
},
},
// Labels: MISSING
},
}
machineDeployment := &clusterv1.MachineDeployment{
TypeMeta: metav1.TypeMeta{
Kind: "MachineDeployment",
APIVersion: clusterv1.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: f.name,
Namespace: cluster.Namespace,
OwnerReferences: []metav1.OwnerReference{ // Added by the machineDeployment controller (mirrors machineDeployment.spec.ClusterName) -- RECONCILED
{
APIVersion: clusterv1.GroupVersion.String(),
Kind: "Cluster",
Name: cluster.Name,
UID: cluster.UID,
},
},
Labels: map[string]string{
clusterv1.ClusterLabelName: cluster.Name, // Added by the machineDeployment controller (mirrors machineDeployment.spec.ClusterName) -- RECONCILED
},
},
Spec: clusterv1.MachineDeploymentSpec{
Template: clusterv1.MachineTemplateSpec{
Spec: clusterv1.MachineSpec{
InfrastructureRef: corev1.ObjectReference{
APIVersion: machineDeploymentInfrastructure.APIVersion,
Kind: machineDeploymentInfrastructure.Kind,
Name: machineDeploymentInfrastructure.Name,
Namespace: machineDeploymentInfrastructure.Namespace,
},
Bootstrap: clusterv1.Bootstrap{
ConfigRef: &corev1.ObjectReference{
APIVersion: machineDeploymentBootstrap.APIVersion,
Kind: machineDeploymentBootstrap.Kind,
Name: machineDeploymentBootstrap.Name,
Namespace: machineDeploymentBootstrap.Namespace,
},
},
},
},
ClusterName: cluster.Name,
},
}
// Ensure the machineDeployment gets a UID to be used by dependant objects for creating OwnerReferences.
setUID(machineDeployment)
objs := []runtime.Object{
machineDeployment,
machineDeploymentBootstrap,
}
// if the infra template is specific to the machine deployment, add it to the object list
if f.sharedInfrastructureTemplate == nil {
objs = append(objs, machineDeploymentInfrastructure)
}
// Adds the objects for the machineSets
for _, machineSet := range f.machineSets {
objs = append(objs, machineSet.Objs(cluster, machineDeployment)...)
}
return objs
}
type FakeMachineSet struct {
name string
machines []*FakeMachine
sharedInfrastructureTemplate *fakeinfrastructure.DummyInfrastructureMachineTemplate
}
// NewFakeMachineSet return a FakeMachineSet that can generate a MachineSet object, all its own ancillary objects:
// - the machineSetInfrastructure template object (only if not controlled by a MachineDeployment)
// - the machineSetBootstrap template object (only if not controlled by a MachineDeployment)
// and all the objects for the defined FakeMachine
func NewFakeMachineSet(name string) *FakeMachineSet {
return &FakeMachineSet{
name: name,
}
}
func (f *FakeMachineSet) WithMachines(fakeMachine ...*FakeMachine) *FakeMachineSet {
f.machines = append(f.machines, fakeMachine...)
return f
}
func (f *FakeMachineSet) WithInfrastructureTemplate(infrastructureTemplate *fakeinfrastructure.DummyInfrastructureMachineTemplate) *FakeMachineSet {
f.sharedInfrastructureTemplate = infrastructureTemplate
return f
}
func (f *FakeMachineSet) Objs(cluster *clusterv1.Cluster, machineDeployment *clusterv1.MachineDeployment) []runtime.Object {
machineSet := &clusterv1.MachineSet{ // Created by machineDeployment controller
TypeMeta: metav1.TypeMeta{
Kind: "MachineSet",
APIVersion: clusterv1.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: f.name,
Namespace: cluster.Namespace,
// Owner reference set by machineSet controller or by machineDeployment controller (see below)
Labels: map[string]string{
clusterv1.ClusterLabelName: cluster.Name, // Added by the machineSet controller (mirrors machineSet.spec.ClusterName) -- RECONCILED
},
},
Spec: clusterv1.MachineSetSpec{
ClusterName: cluster.Name,
},
}
// Ensure the machineSet gets a UID to be used by dependant objects for creating OwnerReferences.
setUID(machineSet)
objs := make([]runtime.Object, 0)
if machineDeployment != nil {
// If this machineSet belong to a machineDeployment, it is controlled by it / ownership set by the machineDeployment controller -- ** NOT RECONCILED **
machineSet.SetOwnerReferences([]metav1.OwnerReference{*metav1.NewControllerRef(machineDeployment, machineDeployment.GroupVersionKind())})
// additionally the machine has ref to the same infra and bootstrap templates defined in the MachineDeployment
machineSet.Spec.Template.Spec.InfrastructureRef = *machineDeployment.Spec.Template.Spec.InfrastructureRef.DeepCopy()
machineSet.Spec.Template.Spec.Bootstrap.ConfigRef = machineDeployment.Spec.Template.Spec.Bootstrap.ConfigRef.DeepCopy()
objs = append(objs, machineSet)
} else {
// If this machineSet does not belong to a machineDeployment, it is owned by the cluster / ownership set by the machineSet controller -- RECONCILED
machineSet.SetOwnerReferences([]metav1.OwnerReference{{
APIVersion: cluster.APIVersion,
Kind: cluster.Kind,
Name: cluster.Name,
UID: cluster.UID,
}})
// additionally the machineSet has ref to dedicated infra and bootstrap templates
// infra template can be either shared or specific to the machine set
machineSetInfrastructure := f.sharedInfrastructureTemplate
if machineSetInfrastructure == nil {
machineSetInfrastructure = NewFakeInfrastructureTemplate(f.name)
}
machineSetInfrastructure.Namespace = cluster.Namespace
machineSetInfrastructure.OwnerReferences = append(machineSetInfrastructure.OwnerReferences, // Added by the machine set controller -- RECONCILED
metav1.OwnerReference{
APIVersion: clusterv1.GroupVersion.String(),
Kind: "Cluster",
Name: cluster.Name,
UID: cluster.UID,
},
)
setUID(machineSetInfrastructure)
machineSet.Spec.Template.Spec.InfrastructureRef = corev1.ObjectReference{
APIVersion: machineSetInfrastructure.APIVersion,
Kind: machineSetInfrastructure.Kind,
Name: machineSetInfrastructure.Name,
Namespace: machineSetInfrastructure.Namespace,
}
machineSetBootstrap := &fakebootstrap.DummyBootstrapConfigTemplate{
TypeMeta: metav1.TypeMeta{
APIVersion: fakebootstrap.GroupVersion.String(),
Kind: "DummyBootstrapConfigTemplate",
},
ObjectMeta: metav1.ObjectMeta{
Name: f.name,
Namespace: cluster.Namespace,
OwnerReferences: []metav1.OwnerReference{ // Added by the machine set controller -- RECONCILED
{
APIVersion: clusterv1.GroupVersion.String(),
Kind: "Cluster",
Name: cluster.Name,
UID: cluster.UID,
},
},
// Labels: MISSING
},
}
machineSet.Spec.Template.Spec.Bootstrap = clusterv1.Bootstrap{
ConfigRef: &corev1.ObjectReference{
APIVersion: machineSetBootstrap.APIVersion,
Kind: machineSetBootstrap.Kind,
Name: machineSetBootstrap.Name,
Namespace: machineSetBootstrap.Namespace,
},
}
objs = append(objs, machineSet, machineSetBootstrap)
// if the infra template is specific to the machine set, add it to the object list
if f.sharedInfrastructureTemplate == nil {
objs = append(objs, machineSetInfrastructure)
}
}
// Adds the objects for the machines controlled by the machineSet
for _, machine := range f.machines {
objs = append(objs, machine.Objs(cluster, false, machineSet, nil)...)
}
return objs
}
type FakeMachine struct {
name string
}
// NewFakeMachine return a FakeMachine that can generate a Machine object, all its own ancillary objects:
// - the machineInfrastructure object
// - the machineBootstrap object and the related bootstrapDataSecret
// If there is no a control plane object in the cluster, the first FakeMachine gets a generated sa secret
func NewFakeMachine(name string) *FakeMachine {
return &FakeMachine{
name: name,
}
}
func (f *FakeMachine) Objs(cluster *clusterv1.Cluster, generateCerts bool, machineSet *clusterv1.MachineSet, controlPlane *fakecontrolplane.DummyControlPlane) []runtime.Object {
machineInfrastructure := &fakeinfrastructure.DummyInfrastructureMachine{
TypeMeta: metav1.TypeMeta{
APIVersion: fakeinfrastructure.GroupVersion.String(),
Kind: "DummyInfrastructureMachine",
},
ObjectMeta: metav1.ObjectMeta{
Name: f.name,
Namespace: cluster.Namespace,
// OwnerReferences: machine, Added by the machine controller (see below) -- RECONCILED
// Labels: cluster.x-k8s.io/cluster-name=cluster, Added by the machine controller (see below) -- RECONCILED
},
}
bootstrapDataSecretName := f.name
machineBootstrap := &fakebootstrap.DummyBootstrapConfig{
TypeMeta: metav1.TypeMeta{
APIVersion: fakebootstrap.GroupVersion.String(),
Kind: "DummyBootstrapConfig",
},
ObjectMeta: metav1.ObjectMeta{
Name: f.name,
Namespace: cluster.Namespace,
// OwnerReferences: machine, Added by the machine controller (see below) -- RECONCILED
// Labels: cluster.x-k8s.io/cluster-name=cluster, Added by the machine controller (see below) -- RECONCILED
},
Status: fakebootstrap.DummyBootstrapConfigStatus{
DataSecretName: &bootstrapDataSecretName,
},
}
// Ensure the machineBootstrap gets a UID to be used by dependant objects for creating OwnerReferences.
setUID(machineBootstrap)
bootstrapDataSecret := &corev1.Secret{ // generated by the bootstrap controller -- ** NOT RECONCILED **
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: bootstrapDataSecretName,
Namespace: cluster.Namespace,
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(machineBootstrap, machineBootstrap.GroupVersionKind()),
},
Labels: map[string]string{
clusterv1.ClusterLabelName: cluster.Name, // derives from Config -(ownerRef)-> machine.spec.ClusterName
},
},
}
machine := &clusterv1.Machine{
TypeMeta: metav1.TypeMeta{
Kind: "Machine",
APIVersion: clusterv1.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: f.name,
Namespace: cluster.Namespace,
// Owner reference set by machine controller or by machineSet controller (see below)
Labels: map[string]string{
clusterv1.ClusterLabelName: cluster.Name, // Added by the machine controller (mirrors machine.spec.ClusterName) -- RECONCILED
},
},
Spec: clusterv1.MachineSpec{
InfrastructureRef: corev1.ObjectReference{
APIVersion: machineInfrastructure.APIVersion,
Kind: machineInfrastructure.Kind,
Name: machineInfrastructure.Name,
Namespace: cluster.Namespace,
},
Bootstrap: clusterv1.Bootstrap{
ConfigRef: &corev1.ObjectReference{
APIVersion: machineBootstrap.APIVersion,
Kind: machineBootstrap.Kind,
Name: machineBootstrap.Name,
Namespace: cluster.Namespace,
},
DataSecretName: &bootstrapDataSecretName,
},
ClusterName: cluster.Name,
},
}
// Ensure the machine gets a UID to be used by dependant objects for creating OwnerReferences.
setUID(machine)
var additionalObjs []runtime.Object
switch {
case machineSet != nil:
// If this machine belong to a machineSet, it is controlled by it / ownership set by the machineSet controller -- ** NOT RECONCILED ?? **
machine.SetOwnerReferences([]metav1.OwnerReference{*metav1.NewControllerRef(machineSet, machineSet.GroupVersionKind())})
case controlPlane != nil:
// If this machine belong to a controlPlane, it is controlled by it / ownership set by the controlPlane controller -- ** NOT RECONCILED ?? **
machine.SetOwnerReferences([]metav1.OwnerReference{*metav1.NewControllerRef(controlPlane, controlPlane.GroupVersionKind())})
// Sets the MachineControlPlane Label
machine.Labels[clusterv1.MachineControlPlaneLabelName] = ""
default:
// If this machine does not belong to a machineSet or to a control plane, it is owned by the cluster / ownership set by the machine controller -- RECONCILED
machine.SetOwnerReferences([]metav1.OwnerReference{{
APIVersion: cluster.APIVersion,
Kind: cluster.Kind,
Name: cluster.Name,
UID: cluster.UID,
}})
// Adds one of the certificate secret object generated by the bootstrap config controller -- ** NOT RECONCILED **
if generateCerts {
saSecret := &corev1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: cluster.GetName() + "-sa",
Namespace: cluster.GetNamespace(),
// Labels: cluster.x-k8s.io/cluster-name=cluster MISSING??
},
}
// Set controlled by the machineBootstrap / ownership set by the bootstrap config controller -- ** NOT RECONCILED ?? **
saSecret.SetOwnerReferences([]metav1.OwnerReference{*metav1.NewControllerRef(machineBootstrap, machineBootstrap.GroupVersionKind())})
additionalObjs = append(additionalObjs, saSecret)
}
}
machineInfrastructure.SetOwnerReferences([]metav1.OwnerReference{
{
APIVersion: machine.APIVersion,
Kind: machine.Kind,
Name: machine.Name,
UID: machine.UID,
},
})
machineInfrastructure.SetLabels(map[string]string{
clusterv1.ClusterLabelName: machine.Spec.ClusterName,
})
machineBootstrap.SetOwnerReferences([]metav1.OwnerReference{
{
APIVersion: machine.APIVersion,
Kind: machine.Kind,
Name: machine.Name,
UID: machine.UID,
},
})
machineBootstrap.SetLabels(map[string]string{
clusterv1.ClusterLabelName: machine.Spec.ClusterName,
})
objs := []runtime.Object{
machine,
machineInfrastructure,
machineBootstrap,
bootstrapDataSecret,
}
objs = append(objs, additionalObjs...)
return objs
}
// setUID assigns a UID to the object, so test objects are uniquely identified.
// NB. In order to make debugging easier we are using a human readable, deterministic string (instead of a random UID).
func setUID(obj runtime.Object) {
accessor, err := meta.Accessor(obj)
if err != nil {
panic(fmt.Sprintf("failde to get accessor for test object: %v", err))
}
uid := fmt.Sprintf("%s, %s/%s", obj.GetObjectKind().GroupVersionKind().String(), accessor.GetNamespace(), accessor.GetName())
accessor.SetUID(types.UID(uid))
}
// FakeCustomResourceDefinition returns a fake CRD object for the given group/versions/kind.
func FakeCustomResourceDefinition(group string, kind string, versions ...string) *apiextensionslv1.CustomResourceDefinition {
crd := &apiextensionslv1.CustomResourceDefinition{
TypeMeta: metav1.TypeMeta{
Kind: apiextensionslv1.SchemeGroupVersion.String(),
APIVersion: "CustomResourceDefinition",
},
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s.%s", strings.ToLower(kind), group), //NB. this technically should use plural(kind), but for the sake of test what really matters is to generate a unique name
Labels: map[string]string{
clusterctlv1.ClusterctlLabelName: "",
},
},
Spec: apiextensionslv1.CustomResourceDefinitionSpec{ //NB. the spec contains only what is strictly required by the move test
Group: group,
Names: apiextensionslv1.CustomResourceDefinitionNames{
Kind: kind,
},
},
}
for i, version := range versions {
// set the first version as a storage version
versionObj := apiextensionslv1.CustomResourceDefinitionVersion{Name: version}
if i == 0 {
versionObj.Storage = true
}
crd.Spec.Versions = append(crd.Spec.Versions, versionObj)
}
return crd
}
// FakeCRDList returns FakeCustomResourceDefinitions for all the Types used in the test object graph
func FakeCRDList() []*apiextensionslv1.CustomResourceDefinition {
version := "v1alpha3"
return []*apiextensionslv1.CustomResourceDefinition{
FakeCustomResourceDefinition(clusterv1.GroupVersion.Group, "Cluster", version),
FakeCustomResourceDefinition(clusterv1.GroupVersion.Group, "Machine", version),
FakeCustomResourceDefinition(clusterv1.GroupVersion.Group, "MachineDeployment", version),
FakeCustomResourceDefinition(clusterv1.GroupVersion.Group, "MachineSet", version),
FakeCustomResourceDefinition(expv1.GroupVersion.Group, "MachinePool", version),
FakeCustomResourceDefinition(fakecontrolplane.GroupVersion.Group, "DummyControlPlane", version),
FakeCustomResourceDefinition(fakeinfrastructure.GroupVersion.Group, "DummyInfrastructureCluster", version),
FakeCustomResourceDefinition(fakeinfrastructure.GroupVersion.Group, "DummyInfrastructureMachine", version),
FakeCustomResourceDefinition(fakeinfrastructure.GroupVersion.Group, "DummyInfrastructureMachineTemplate", version),
FakeCustomResourceDefinition(fakebootstrap.GroupVersion.Group, "DummyBootstrapConfig", version),
FakeCustomResourceDefinition(fakebootstrap.GroupVersion.Group, "DummyBootstrapConfigTemplate", version),
}
}