-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
builders.go
1165 lines (1026 loc) · 39.7 KB
/
builders.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 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 builder
import (
"fmt"
"strings"
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/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
)
// ClusterBuilder holds the variables and objects required to build a clusterv1.Cluster.
type ClusterBuilder struct {
namespace string
name string
labels map[string]string
topology *clusterv1.Topology
infrastructureCluster *unstructured.Unstructured
controlPlane *unstructured.Unstructured
}
// Cluster returns a ClusterBuilder with the given name and namespace.
func Cluster(namespace, name string) *ClusterBuilder {
return &ClusterBuilder{
namespace: namespace,
name: name,
}
}
// WithLabels sets the labels for the ClusterBuilder.
func (c *ClusterBuilder) WithLabels(labels map[string]string) *ClusterBuilder {
c.labels = labels
return c
}
// WithInfrastructureCluster adds the passed InfrastructureCluster to the ClusterBuilder.
func (c *ClusterBuilder) WithInfrastructureCluster(t *unstructured.Unstructured) *ClusterBuilder {
c.infrastructureCluster = t
return c
}
// WithControlPlane adds the passed ControlPlane to the ClusterBuilder.
func (c *ClusterBuilder) WithControlPlane(t *unstructured.Unstructured) *ClusterBuilder {
c.controlPlane = t
return c
}
// WithTopology adds the passed Topology object to the ClusterBuilder.
func (c *ClusterBuilder) WithTopology(topology *clusterv1.Topology) *ClusterBuilder {
c.topology = topology
return c
}
// Build returns a Cluster with the attributes added to the ClusterBuilder.
func (c *ClusterBuilder) Build() *clusterv1.Cluster {
obj := &clusterv1.Cluster{
TypeMeta: metav1.TypeMeta{
Kind: "Cluster",
APIVersion: clusterv1.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: c.name,
Namespace: c.namespace,
Labels: c.labels,
},
Spec: clusterv1.ClusterSpec{
Topology: c.topology,
},
}
if c.infrastructureCluster != nil {
obj.Spec.InfrastructureRef = objToRef(c.infrastructureCluster)
}
if c.controlPlane != nil {
obj.Spec.ControlPlaneRef = objToRef(c.controlPlane)
}
return obj
}
// ClusterTopologyBuilder contains the fields needed to build a testable ClusterTopology.
type ClusterTopologyBuilder struct {
class string
workers *clusterv1.WorkersTopology
version string
controlPlaneReplicas int32
variables []clusterv1.ClusterVariable
}
// ClusterTopology returns a ClusterTopologyBuilder.
func ClusterTopology() *ClusterTopologyBuilder {
return &ClusterTopologyBuilder{
workers: &clusterv1.WorkersTopology{},
}
}
// WithClass adds the passed ClusterClass name to the ClusterTopologyBuilder.
func (c *ClusterTopologyBuilder) WithClass(class string) *ClusterTopologyBuilder {
c.class = class
return c
}
// WithVersion adds the passed version to the ClusterTopologyBuilder.
func (c *ClusterTopologyBuilder) WithVersion(version string) *ClusterTopologyBuilder {
c.version = version
return c
}
// WithControlPlaneReplicas adds the passed replicas value to the ClusterTopologyBuilder.
func (c *ClusterTopologyBuilder) WithControlPlaneReplicas(replicas int32) *ClusterTopologyBuilder {
c.controlPlaneReplicas = replicas
return c
}
// WithMachineDeployment passes the full MachineDeploymentTopology and adds it to an existing list in the ClusterTopologyBuilder.
func (c *ClusterTopologyBuilder) WithMachineDeployment(mdc clusterv1.MachineDeploymentTopology) *ClusterTopologyBuilder {
c.workers.MachineDeployments = append(c.workers.MachineDeployments, mdc)
return c
}
// WithVariables adds the passed variables to the ClusterTopologyBuilder.
func (c *ClusterTopologyBuilder) WithVariables(vars ...clusterv1.ClusterVariable) *ClusterTopologyBuilder {
c.variables = vars
return c
}
// Build returns a testable cluster Topology object with any values passed to the builder.
func (c *ClusterTopologyBuilder) Build() *clusterv1.Topology {
return &clusterv1.Topology{
Class: c.class,
Workers: c.workers,
Version: c.version,
ControlPlane: clusterv1.ControlPlaneTopology{
Replicas: &c.controlPlaneReplicas,
},
Variables: c.variables,
}
}
// MachineDeploymentTopologyBuilder holds the values needed to create a testable MachineDeploymentTopology.
type MachineDeploymentTopologyBuilder struct {
class string
name string
replicas *int32
variables []clusterv1.ClusterVariable
}
// MachineDeploymentTopology returns a builder used to create a testable MachineDeploymentTopology.
func MachineDeploymentTopology(name string) *MachineDeploymentTopologyBuilder {
return &MachineDeploymentTopologyBuilder{
name: name,
}
}
// WithClass adds a class string used as the MachineDeploymentTopology class.
func (m *MachineDeploymentTopologyBuilder) WithClass(class string) *MachineDeploymentTopologyBuilder {
m.class = class
return m
}
// WithReplicas adds a replicas value used as the MachineDeploymentTopology replicas value.
func (m *MachineDeploymentTopologyBuilder) WithReplicas(replicas int32) *MachineDeploymentTopologyBuilder {
m.replicas = &replicas
return m
}
// WithVariables adds variables used as the MachineDeploymentTopology variables value.
func (m *MachineDeploymentTopologyBuilder) WithVariables(variables ...clusterv1.ClusterVariable) *MachineDeploymentTopologyBuilder {
m.variables = variables
return m
}
// Build returns a testable MachineDeploymentTopology with any values passed to the builder.
func (m *MachineDeploymentTopologyBuilder) Build() clusterv1.MachineDeploymentTopology {
md := clusterv1.MachineDeploymentTopology{
Class: m.class,
Name: m.name,
Replicas: m.replicas,
}
if len(m.variables) > 0 {
md.Variables = &clusterv1.MachineDeploymentVariables{
Overrides: m.variables,
}
}
return md
}
// ClusterClassBuilder holds the variables and objects required to build a clusterv1.ClusterClass.
type ClusterClassBuilder struct {
namespace string
name string
infrastructureClusterTemplate *unstructured.Unstructured
controlPlaneMetadata *clusterv1.ObjectMeta
controlPlaneTemplate *unstructured.Unstructured
controlPlaneInfrastructureMachineTemplate *unstructured.Unstructured
controlPlaneMHC *clusterv1.MachineHealthCheckClass
machineDeploymentClasses []clusterv1.MachineDeploymentClass
variables []clusterv1.ClusterClassVariable
patches []clusterv1.ClusterClassPatch
}
// ClusterClass returns a ClusterClassBuilder with the given name and namespace.
func ClusterClass(namespace, name string) *ClusterClassBuilder {
return &ClusterClassBuilder{
namespace: namespace,
name: name,
}
}
// WithInfrastructureClusterTemplate adds the passed InfrastructureClusterTemplate to the ClusterClassBuilder.
func (c *ClusterClassBuilder) WithInfrastructureClusterTemplate(t *unstructured.Unstructured) *ClusterClassBuilder {
c.infrastructureClusterTemplate = t
return c
}
// WithControlPlaneTemplate adds the passed ControlPlaneTemplate to the ClusterClassBuilder.
func (c *ClusterClassBuilder) WithControlPlaneTemplate(t *unstructured.Unstructured) *ClusterClassBuilder {
c.controlPlaneTemplate = t
return c
}
// WithControlPlaneMetadata adds the given labels and annotations for use with the ControlPlane to the ClusterClassBuilder.
func (c *ClusterClassBuilder) WithControlPlaneMetadata(labels, annotations map[string]string) *ClusterClassBuilder {
c.controlPlaneMetadata = &clusterv1.ObjectMeta{
Labels: labels,
Annotations: annotations,
}
return c
}
// WithControlPlaneInfrastructureMachineTemplate adds the ControlPlane's InfrastructureMachineTemplate to the ClusterClassBuilder.
func (c *ClusterClassBuilder) WithControlPlaneInfrastructureMachineTemplate(t *unstructured.Unstructured) *ClusterClassBuilder {
c.controlPlaneInfrastructureMachineTemplate = t
return c
}
// WithControlPlaneMachineHealthCheck adds a MachineHealthCheck for the ControlPlane to the ClusterClassBuilder.
func (c *ClusterClassBuilder) WithControlPlaneMachineHealthCheck(mhc *clusterv1.MachineHealthCheckClass) *ClusterClassBuilder {
c.controlPlaneMHC = mhc
return c
}
// WithVariables adds the Variables the ClusterClassBuilder.
func (c *ClusterClassBuilder) WithVariables(vars ...clusterv1.ClusterClassVariable) *ClusterClassBuilder {
c.variables = vars
return c
}
// WithPatches adds the patches to the ClusterClassBuilder.
func (c *ClusterClassBuilder) WithPatches(patches []clusterv1.ClusterClassPatch) *ClusterClassBuilder {
c.patches = patches
return c
}
// WithWorkerMachineDeploymentClasses adds the variables and objects needed to create MachineDeploymentTemplates for a ClusterClassBuilder.
func (c *ClusterClassBuilder) WithWorkerMachineDeploymentClasses(mdcs ...clusterv1.MachineDeploymentClass) *ClusterClassBuilder {
if c.machineDeploymentClasses == nil {
c.machineDeploymentClasses = make([]clusterv1.MachineDeploymentClass, 0)
}
c.machineDeploymentClasses = append(c.machineDeploymentClasses, mdcs...)
return c
}
// Build takes the objects and variables in the ClusterClass builder and uses them to create a ClusterClass object.
func (c *ClusterClassBuilder) Build() *clusterv1.ClusterClass {
obj := &clusterv1.ClusterClass{
TypeMeta: metav1.TypeMeta{
Kind: "ClusterClass",
APIVersion: clusterv1.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: c.name,
Namespace: c.namespace,
},
Spec: clusterv1.ClusterClassSpec{
Variables: c.variables,
Patches: c.patches,
},
}
if c.infrastructureClusterTemplate != nil {
obj.Spec.Infrastructure = clusterv1.LocalObjectTemplate{
Ref: objToRef(c.infrastructureClusterTemplate),
}
}
if c.controlPlaneMetadata != nil {
obj.Spec.ControlPlane.Metadata = *c.controlPlaneMetadata
}
if c.controlPlaneTemplate != nil {
obj.Spec.ControlPlane.LocalObjectTemplate = clusterv1.LocalObjectTemplate{
Ref: objToRef(c.controlPlaneTemplate),
}
}
if c.controlPlaneMHC != nil {
obj.Spec.ControlPlane.MachineHealthCheck = c.controlPlaneMHC
}
if c.controlPlaneInfrastructureMachineTemplate != nil {
obj.Spec.ControlPlane.MachineInfrastructure = &clusterv1.LocalObjectTemplate{
Ref: objToRef(c.controlPlaneInfrastructureMachineTemplate),
}
}
obj.Spec.Workers.MachineDeployments = c.machineDeploymentClasses
return obj
}
// MachineDeploymentClassBuilder holds the variables and objects required to build a clusterv1.MachineDeploymentClass.
type MachineDeploymentClassBuilder struct {
class string
infrastructureMachineTemplate *unstructured.Unstructured
bootstrapTemplate *unstructured.Unstructured
labels map[string]string
annotations map[string]string
machineHealthCheckClass *clusterv1.MachineHealthCheckClass
}
// MachineDeploymentClass returns a MachineDeploymentClassBuilder with the given name and namespace.
func MachineDeploymentClass(class string) *MachineDeploymentClassBuilder {
return &MachineDeploymentClassBuilder{
class: class,
}
}
// WithInfrastructureTemplate registers the passed Unstructured object as the InfrastructureMachineTemplate for the MachineDeploymentClassBuilder.
func (m *MachineDeploymentClassBuilder) WithInfrastructureTemplate(t *unstructured.Unstructured) *MachineDeploymentClassBuilder {
m.infrastructureMachineTemplate = t
return m
}
// WithBootstrapTemplate registers the passed Unstructured object as the BootstrapTemplate for the MachineDeploymentClassBuilder.
func (m *MachineDeploymentClassBuilder) WithBootstrapTemplate(t *unstructured.Unstructured) *MachineDeploymentClassBuilder {
m.bootstrapTemplate = t
return m
}
// WithLabels sets the labels for the MachineDeploymentClassBuilder.
func (m *MachineDeploymentClassBuilder) WithLabels(labels map[string]string) *MachineDeploymentClassBuilder {
m.labels = labels
return m
}
// WithAnnotations sets the annotations for the MachineDeploymentClassBuilder.
func (m *MachineDeploymentClassBuilder) WithAnnotations(annotations map[string]string) *MachineDeploymentClassBuilder {
m.annotations = annotations
return m
}
// WithMachineHealthCheckClass sets the MachineHealthCheckClass for the MachineDeploymentClassBuilder.
func (m *MachineDeploymentClassBuilder) WithMachineHealthCheckClass(mhc *clusterv1.MachineHealthCheckClass) *MachineDeploymentClassBuilder {
m.machineHealthCheckClass = mhc
return m
}
// Build creates a full MachineDeploymentClass object with the variables passed to the MachineDeploymentClassBuilder.
func (m *MachineDeploymentClassBuilder) Build() *clusterv1.MachineDeploymentClass {
obj := &clusterv1.MachineDeploymentClass{
Class: m.class,
Template: clusterv1.MachineDeploymentClassTemplate{
Metadata: clusterv1.ObjectMeta{
Labels: m.labels,
Annotations: m.annotations,
},
},
}
if m.bootstrapTemplate != nil {
obj.Template.Bootstrap.Ref = objToRef(m.bootstrapTemplate)
}
if m.infrastructureMachineTemplate != nil {
obj.Template.Infrastructure.Ref = objToRef(m.infrastructureMachineTemplate)
}
if m.machineHealthCheckClass != nil {
obj.MachineHealthCheck = m.machineHealthCheckClass
}
return obj
}
// InfrastructureMachineTemplateBuilder holds the variables and objects needed to build an InfrastructureMachineTemplate.
// +kubebuilder:object:generate=false
type InfrastructureMachineTemplateBuilder struct {
namespace string
name string
specFields map[string]interface{}
}
// InfrastructureMachineTemplate creates an InfrastructureMachineTemplateBuilder with the given name and namespace.
func InfrastructureMachineTemplate(namespace, name string) *InfrastructureMachineTemplateBuilder {
return &InfrastructureMachineTemplateBuilder{
namespace: namespace,
name: name,
}
}
// WithSpecFields sets a map of spec fields on the unstructured object. The keys in the map represent the path and the value corresponds
// to the value of the spec field.
//
// Note: all the paths should start with "spec."
//
// Example map: map[string]interface{}{
// "spec.version": "v1.2.3",
// }.
func (i *InfrastructureMachineTemplateBuilder) WithSpecFields(fields map[string]interface{}) *InfrastructureMachineTemplateBuilder {
i.specFields = fields
return i
}
// Build takes the objects and variables in the InfrastructureMachineTemplateBuilder and generates an unstructured object.
func (i *InfrastructureMachineTemplateBuilder) Build() *unstructured.Unstructured {
obj := &unstructured.Unstructured{}
obj.SetAPIVersion(InfrastructureGroupVersion.String())
obj.SetKind(GenericInfrastructureMachineTemplateKind)
obj.SetNamespace(i.namespace)
obj.SetName(i.name)
// Initialize the spec.template.spec to make the object valid in reconciliation.
setSpecFields(obj, map[string]interface{}{"spec.template.spec": map[string]interface{}{}})
setSpecFields(obj, i.specFields)
return obj
}
// BootstrapTemplateBuilder holds the variables needed to build a generic BootstrapTemplate.
// +kubebuilder:object:generate=false
type BootstrapTemplateBuilder struct {
namespace string
name string
specFields map[string]interface{}
}
// BootstrapTemplate creates a BootstrapTemplateBuilder with the given name and namespace.
func BootstrapTemplate(namespace, name string) *BootstrapTemplateBuilder {
return &BootstrapTemplateBuilder{
namespace: namespace,
name: name,
}
}
// WithSpecFields will add fields of any type to the object spec. It takes an argument, fields, which is of the form path: object.
func (b *BootstrapTemplateBuilder) WithSpecFields(fields map[string]interface{}) *BootstrapTemplateBuilder {
b.specFields = fields
return b
}
// Build creates a new Unstructured object with the information passed to the BootstrapTemplateBuilder.
func (b *BootstrapTemplateBuilder) Build() *unstructured.Unstructured {
obj := &unstructured.Unstructured{}
obj.SetAPIVersion(BootstrapGroupVersion.String())
obj.SetKind(GenericBootstrapConfigTemplateKind)
obj.SetNamespace(b.namespace)
obj.SetName(b.name)
setSpecFields(obj, b.specFields)
// Initialize the spec.template.spec to make the object valid in reconciliation.
setSpecFields(obj, map[string]interface{}{"spec.template.spec": map[string]interface{}{}})
return obj
}
// InfrastructureClusterTemplateBuilder holds the variables needed to build a generic InfrastructureClusterTemplate.
// +kubebuilder:object:generate=false
type InfrastructureClusterTemplateBuilder struct {
namespace string
name string
specFields map[string]interface{}
}
// InfrastructureClusterTemplate returns an InfrastructureClusterTemplateBuilder with the given name and namespace.
func InfrastructureClusterTemplate(namespace, name string) *InfrastructureClusterTemplateBuilder {
return &InfrastructureClusterTemplateBuilder{
namespace: namespace,
name: name,
}
}
// WithSpecFields sets a map of spec fields on the unstructured object. The keys in the map represent the path and the value corresponds
// to the value of the spec field.
//
// Note: all the paths should start with "spec."
//
// Example map: map[string]interface{}{
// "spec.version": "v1.2.3",
// }.
func (i *InfrastructureClusterTemplateBuilder) WithSpecFields(fields map[string]interface{}) *InfrastructureClusterTemplateBuilder {
i.specFields = fields
return i
}
// Build creates a new Unstructured object with the variables passed to the InfrastructureClusterTemplateBuilder.
func (i *InfrastructureClusterTemplateBuilder) Build() *unstructured.Unstructured {
obj := &unstructured.Unstructured{}
obj.SetAPIVersion(InfrastructureGroupVersion.String())
obj.SetKind(GenericInfrastructureClusterTemplateKind)
obj.SetNamespace(i.namespace)
obj.SetName(i.name)
// Initialize the spec.template.spec to make the object valid in reconciliation.
setSpecFields(obj, map[string]interface{}{"spec.template.spec": map[string]interface{}{}})
setSpecFields(obj, i.specFields)
return obj
}
// ControlPlaneTemplateBuilder holds the variables and objects needed to build a generic ControlPlane template.
// +kubebuilder:object:generate=false
type ControlPlaneTemplateBuilder struct {
namespace string
name string
infrastructureMachineTemplate *unstructured.Unstructured
specFields map[string]interface{}
}
// ControlPlaneTemplate creates a NewControlPlaneTemplate builder with the given name and namespace.
func ControlPlaneTemplate(namespace, name string) *ControlPlaneTemplateBuilder {
return &ControlPlaneTemplateBuilder{
namespace: namespace,
name: name,
}
}
// WithSpecFields sets a map of spec fields on the unstructured object. The keys in the map represent the path and the value corresponds
// to the value of the spec field.
//
// Note: all the paths should start with "spec."
//
// Example map: map[string]interface{}{
// "spec.version": "v1.2.3",
// }.
func (c *ControlPlaneTemplateBuilder) WithSpecFields(fields map[string]interface{}) *ControlPlaneTemplateBuilder {
c.specFields = fields
return c
}
// WithInfrastructureMachineTemplate adds the given Unstructured object to the ControlPlaneTemplateBuilder as its InfrastructureMachineTemplate.
func (c *ControlPlaneTemplateBuilder) WithInfrastructureMachineTemplate(t *unstructured.Unstructured) *ControlPlaneTemplateBuilder {
c.infrastructureMachineTemplate = t
return c
}
// Build creates an Unstructured object from the variables passed to the ControlPlaneTemplateBuilder.
func (c *ControlPlaneTemplateBuilder) Build() *unstructured.Unstructured {
obj := &unstructured.Unstructured{}
obj.SetAPIVersion(ControlPlaneGroupVersion.String())
obj.SetKind(GenericControlPlaneTemplateKind)
obj.SetNamespace(c.namespace)
obj.SetName(c.name)
// Initialize the spec.template.spec to make the object valid in reconciliation.
setSpecFields(obj, map[string]interface{}{"spec.template.spec": map[string]interface{}{}})
setSpecFields(obj, c.specFields)
if c.infrastructureMachineTemplate != nil {
if err := setNestedRef(obj, c.infrastructureMachineTemplate, "spec", "template", "spec", "machineTemplate", "infrastructureRef"); err != nil {
panic(err)
}
}
return obj
}
// InfrastructureClusterBuilder holds the variables and objects needed to build a generic InfrastructureCluster.
// +kubebuilder:object:generate=false
type InfrastructureClusterBuilder struct {
namespace string
name string
specFields map[string]interface{}
}
// WithSpecFields sets a map of spec fields on the unstructured object. The keys in the map represent the path and the value corresponds
// to the value of the spec field.
//
// Note: all the paths should start with "spec."
//
// Example map: map[string]interface{}{
// "spec.version": "v1.2.3",
// }.
func (i *InfrastructureClusterBuilder) WithSpecFields(fields map[string]interface{}) *InfrastructureClusterBuilder {
i.specFields = fields
return i
}
// InfrastructureCluster returns and InfrastructureClusterBuilder with the given name and namespace.
func InfrastructureCluster(namespace, name string) *InfrastructureClusterBuilder {
return &InfrastructureClusterBuilder{
namespace: namespace,
name: name,
}
}
// Build returns an Unstructured object with the information passed to the InfrastructureClusterBuilder.
func (i *InfrastructureClusterBuilder) Build() *unstructured.Unstructured {
obj := &unstructured.Unstructured{}
obj.SetAPIVersion(InfrastructureGroupVersion.String())
obj.SetKind(GenericInfrastructureClusterKind)
obj.SetNamespace(i.namespace)
obj.SetName(i.name)
setSpecFields(obj, i.specFields)
return obj
}
// ControlPlaneBuilder holds the variables and objects needed to build a generic object for cluster.spec.controlPlaneRef.
// +kubebuilder:object:generate=false
type ControlPlaneBuilder struct {
namespace string
name string
infrastructureMachineTemplate *unstructured.Unstructured
replicas *int64
version *string
specFields map[string]interface{}
statusFields map[string]interface{}
}
// ControlPlane returns a ControlPlaneBuilder with the given name and Namespace.
func ControlPlane(namespace, name string) *ControlPlaneBuilder {
return &ControlPlaneBuilder{
namespace: namespace,
name: name,
}
}
// WithInfrastructureMachineTemplate adds the given unstructured object to the ControlPlaneBuilder as its InfrastructureMachineTemplate.
func (f *ControlPlaneBuilder) WithInfrastructureMachineTemplate(t *unstructured.Unstructured) *ControlPlaneBuilder {
f.infrastructureMachineTemplate = t
return f
}
// WithReplicas sets the number of replicas for the ControlPlaneBuilder.
func (f *ControlPlaneBuilder) WithReplicas(replicas int64) *ControlPlaneBuilder {
f.replicas = &replicas
return f
}
// WithVersion adds the passed version to the ControlPlaneBuilder.
func (f *ControlPlaneBuilder) WithVersion(version string) *ControlPlaneBuilder {
f.version = &version
return f
}
// WithSpecFields sets a map of spec fields on the unstructured object. The keys in the map represent the path and the value corresponds
// to the value of the spec field.
//
// Note: all the paths should start with "spec."
//
// Example map: map[string]interface{}{
// "spec.version": "v1.2.3",
// }.
func (f *ControlPlaneBuilder) WithSpecFields(m map[string]interface{}) *ControlPlaneBuilder {
f.specFields = m
return f
}
// WithStatusFields sets a map of status fields on the unstructured object. The keys in the map represent the path and the value corresponds
// to the value of the status field.
//
// Note: all the paths should start with "status."
//
// Example map: map[string]interface{}{
// "status.version": "v1.2.3",
// }.
func (f *ControlPlaneBuilder) WithStatusFields(m map[string]interface{}) *ControlPlaneBuilder {
f.statusFields = m
return f
}
// Build generates an Unstructured object from the information passed to the ControlPlaneBuilder.
func (f *ControlPlaneBuilder) Build() *unstructured.Unstructured {
obj := &unstructured.Unstructured{}
obj.SetAPIVersion(ControlPlaneGroupVersion.String())
obj.SetKind(GenericControlPlaneKind)
obj.SetNamespace(f.namespace)
obj.SetName(f.name)
setSpecFields(obj, f.specFields)
setStatusFields(obj, f.statusFields)
// TODO(killianmuldoon): Update to use the internal/contract package, when it is importable from here
if f.infrastructureMachineTemplate != nil {
if err := setNestedRef(obj, f.infrastructureMachineTemplate, "spec", "machineTemplate", "infrastructureRef"); err != nil {
panic(err)
}
}
if f.replicas != nil {
if err := unstructured.SetNestedField(obj.UnstructuredContent(), *f.replicas, "spec", "replicas"); err != nil {
panic(err)
}
}
if f.version != nil {
if err := unstructured.SetNestedField(obj.UnstructuredContent(), *f.version, "spec", "version"); err != nil {
panic(err)
}
}
return obj
}
// MachinePoolBuilder holds the variables and objects needed to build a generic MachinePool.
type MachinePoolBuilder struct {
namespace string
name string
bootstrapTemplate *unstructured.Unstructured
infrastructureTemplate *unstructured.Unstructured
version *string
clusterName string
replicas *int32
labels map[string]string
status *expv1.MachinePoolStatus
}
// MachinePool creates a MachinePoolBuilder with the given name and namespace.
func MachinePool(namespace, name string) *MachinePoolBuilder {
return &MachinePoolBuilder{
name: name,
namespace: namespace,
}
}
// WithBootstrapTemplate adds the passed Unstructured object to the MachinePoolBuilder as a bootstrapTemplate.
func (m *MachinePoolBuilder) WithBootstrapTemplate(ref *unstructured.Unstructured) *MachinePoolBuilder {
m.bootstrapTemplate = ref
return m
}
// WithInfrastructureTemplate adds the passed unstructured object to the MachinePool builder as an infrastructureMachineTemplate.
func (m *MachinePoolBuilder) WithInfrastructureTemplate(ref *unstructured.Unstructured) *MachinePoolBuilder {
m.infrastructureTemplate = ref
return m
}
// WithLabels adds the given labels to the MachinePoolBuilder.
func (m *MachinePoolBuilder) WithLabels(labels map[string]string) *MachinePoolBuilder {
m.labels = labels
return m
}
// WithVersion sets the passed version on the MachinePool spec.
func (m *MachinePoolBuilder) WithVersion(version string) *MachinePoolBuilder {
m.version = &version
return m
}
// WithClusterName sets the passed clusterName on the MachinePool spec.
func (m *MachinePoolBuilder) WithClusterName(clusterName string) *MachinePoolBuilder {
m.clusterName = clusterName
return m
}
// WithReplicas sets the number of replicas for the MachinePoolBuilder.
func (m *MachinePoolBuilder) WithReplicas(replicas int32) *MachinePoolBuilder {
m.replicas = &replicas
return m
}
// WithStatus sets the passed status object as the status of the MachinePool object.
func (m *MachinePoolBuilder) WithStatus(status expv1.MachinePoolStatus) *MachinePoolBuilder {
m.status = &status
return m
}
// Build creates a new MachinePool with the variables and objects passed to the MachinePoolBuilder.
func (m *MachinePoolBuilder) Build() *expv1.MachinePool {
obj := &expv1.MachinePool{
TypeMeta: metav1.TypeMeta{
Kind: "MachinePool",
APIVersion: expv1.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: m.name,
Namespace: m.namespace,
Labels: m.labels,
},
Spec: expv1.MachinePoolSpec{
ClusterName: m.clusterName,
Replicas: m.replicas,
},
}
if m.bootstrapTemplate != nil {
obj.Spec.Template.Spec.Bootstrap.ConfigRef = objToRef(m.bootstrapTemplate)
}
if m.infrastructureTemplate != nil {
obj.Spec.Template.Spec.InfrastructureRef = *objToRef(m.infrastructureTemplate)
}
if m.version != nil {
obj.Spec.Template.Spec.Version = m.version
}
if m.status != nil {
obj.Status = *m.status
}
return obj
}
// MachineDeploymentBuilder holds the variables and objects needed to build a generic MachineDeployment.
type MachineDeploymentBuilder struct {
namespace string
name string
bootstrapTemplate *unstructured.Unstructured
infrastructureTemplate *unstructured.Unstructured
version *string
replicas *int32
generation *int64
labels map[string]string
status *clusterv1.MachineDeploymentStatus
}
// MachineDeployment creates a MachineDeploymentBuilder with the given name and namespace.
func MachineDeployment(namespace, name string) *MachineDeploymentBuilder {
return &MachineDeploymentBuilder{
name: name,
namespace: namespace,
}
}
// WithBootstrapTemplate adds the passed Unstructured object to the MachineDeploymentBuilder as a bootstrapTemplate.
func (m *MachineDeploymentBuilder) WithBootstrapTemplate(ref *unstructured.Unstructured) *MachineDeploymentBuilder {
m.bootstrapTemplate = ref
return m
}
// WithInfrastructureTemplate adds the passed unstructured object to the MachineDeployment builder as an infrastructureMachineTemplate.
func (m *MachineDeploymentBuilder) WithInfrastructureTemplate(ref *unstructured.Unstructured) *MachineDeploymentBuilder {
m.infrastructureTemplate = ref
return m
}
// WithLabels adds the given labels to the MachineDeploymentBuilder.
func (m *MachineDeploymentBuilder) WithLabels(labels map[string]string) *MachineDeploymentBuilder {
m.labels = labels
return m
}
// WithVersion sets the passed version on the machine deployment spec.
func (m *MachineDeploymentBuilder) WithVersion(version string) *MachineDeploymentBuilder {
m.version = &version
return m
}
// WithReplicas sets the number of replicas for the MachineDeploymentClassBuilder.
func (m *MachineDeploymentBuilder) WithReplicas(replicas int32) *MachineDeploymentBuilder {
m.replicas = &replicas
return m
}
// WithGeneration sets the passed value on the machine deployments object metadata.
func (m *MachineDeploymentBuilder) WithGeneration(generation int64) *MachineDeploymentBuilder {
m.generation = &generation
return m
}
// WithStatus sets the passed status object as the status of the machine deployment object.
func (m *MachineDeploymentBuilder) WithStatus(status clusterv1.MachineDeploymentStatus) *MachineDeploymentBuilder {
m.status = &status
return m
}
// Build creates a new MachineDeployment with the variables and objects passed to the MachineDeploymentBuilder.
func (m *MachineDeploymentBuilder) Build() *clusterv1.MachineDeployment {
obj := &clusterv1.MachineDeployment{
TypeMeta: metav1.TypeMeta{
Kind: "MachineDeployment",
APIVersion: clusterv1.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: m.name,
Namespace: m.namespace,
Labels: m.labels,
},
}
if m.generation != nil {
obj.Generation = *m.generation
}
if m.version != nil {
obj.Spec.Template.Spec.Version = m.version
}
obj.Spec.Replicas = m.replicas
if m.bootstrapTemplate != nil {
obj.Spec.Template.Spec.Bootstrap.ConfigRef = objToRef(m.bootstrapTemplate)
}
if m.infrastructureTemplate != nil {
obj.Spec.Template.Spec.InfrastructureRef = *objToRef(m.infrastructureTemplate)
}
if m.status != nil {
obj.Status = *m.status
}
return obj
}
// MachineSetBuilder holds the variables and objects needed to build a generic MachineSet.
type MachineSetBuilder struct {
namespace string
name string
bootstrapTemplate *unstructured.Unstructured
infrastructureTemplate *unstructured.Unstructured
replicas *int32
labels map[string]string
}
// MachineSet creates a MachineSetBuilder with the given name and namespace.
func MachineSet(namespace, name string) *MachineSetBuilder {
return &MachineSetBuilder{
name: name,
namespace: namespace,
}
}
// WithBootstrapTemplate adds the passed Unstructured object to the MachineSetBuilder as a bootstrapTemplate.
func (m *MachineSetBuilder) WithBootstrapTemplate(ref *unstructured.Unstructured) *MachineSetBuilder {
m.bootstrapTemplate = ref
return m
}
// WithInfrastructureTemplate adds the passed unstructured object to the MachineSet builder as an infrastructureMachineTemplate.
func (m *MachineSetBuilder) WithInfrastructureTemplate(ref *unstructured.Unstructured) *MachineSetBuilder {
m.infrastructureTemplate = ref
return m
}
// WithLabels adds the given labels to the MachineSetBuilder.
func (m *MachineSetBuilder) WithLabels(labels map[string]string) *MachineSetBuilder {
m.labels = labels
return m
}
// WithReplicas sets the number of replicas for the MachineSetClassBuilder.
func (m *MachineSetBuilder) WithReplicas(replicas *int32) *MachineSetBuilder {
m.replicas = replicas
return m
}
// Build creates a new MachineSet with the variables and objects passed to the MachineSetBuilder.
func (m *MachineSetBuilder) Build() *clusterv1.MachineSet {
obj := &clusterv1.MachineSet{
TypeMeta: metav1.TypeMeta{
Kind: "MachineSet",
APIVersion: clusterv1.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: m.name,
Namespace: m.namespace,
Labels: m.labels,
},
}
obj.Spec.Replicas = m.replicas
if m.bootstrapTemplate != nil {
obj.Spec.Template.Spec.Bootstrap.ConfigRef = objToRef(m.bootstrapTemplate)
}
if m.infrastructureTemplate != nil {
obj.Spec.Template.Spec.InfrastructureRef = *objToRef(m.infrastructureTemplate)
}
return obj
}
// MachineBuilder holds the variables required to build a Machine.
type MachineBuilder struct {
name string
namespace string
version *string
clusterName string
bootstrap *unstructured.Unstructured
labels map[string]string
}
// Machine returns a MachineBuilder.
func Machine(namespace, name string) *MachineBuilder {
return &MachineBuilder{
name: name,
namespace: namespace,
}
}
// WithVersion adds a version to the MachineBuilder.
func (m *MachineBuilder) WithVersion(version string) *MachineBuilder {
m.version = &version
return m
}
// WithBootstrapTemplate adds a bootstrap template to the MachineBuilder.
func (m *MachineBuilder) WithBootstrapTemplate(bootstrap *unstructured.Unstructured) *MachineBuilder {
m.bootstrap = bootstrap
return m
}