@@ -49,6 +49,8 @@ func TestApply(t *testing.T) {
49
49
controlPlaneInfrastructureMachineTemplate map [string ]interface {}
50
50
machineDeploymentBootstrapTemplate map [string ]map [string ]interface {}
51
51
machineDeploymentInfrastructureMachineTemplate map [string ]map [string ]interface {}
52
+ machinePoolBootstrapConfig map [string ]map [string ]interface {}
53
+ machinePoolInfrastructureMachinePool map [string ]map [string ]interface {}
52
54
}
53
55
54
56
tests := []struct {
@@ -134,7 +136,7 @@ func TestApply(t *testing.T) {
134
136
},
135
137
},
136
138
{
137
- name : "Should apply JSON patches to MachineDeployment templates" ,
139
+ name : "Should apply JSON patches to MachineDeployment and MachinePool templates" ,
138
140
patches : []clusterv1.ClusterClassPatch {
139
141
{
140
142
Name : "fake-patch1" ,
@@ -175,6 +177,42 @@ func TestApply(t *testing.T) {
175
177
},
176
178
},
177
179
},
180
+ {
181
+ Selector : clusterv1.PatchSelector {
182
+ APIVersion : builder .InfrastructureGroupVersion .String (),
183
+ Kind : builder .GenericInfrastructureMachinePoolTemplateKind ,
184
+ MatchResources : clusterv1.PatchSelectorMatch {
185
+ MachinePoolClass : & clusterv1.PatchSelectorMatchMachinePoolClass {
186
+ Names : []string {"default-mp-worker" },
187
+ },
188
+ },
189
+ },
190
+ JSONPatches : []clusterv1.JSONPatch {
191
+ {
192
+ Op : "add" ,
193
+ Path : "/spec/template/spec/resource" ,
194
+ Value : & apiextensionsv1.JSON {Raw : []byte (`"default-mp-worker-infra"` )},
195
+ },
196
+ },
197
+ },
198
+ {
199
+ Selector : clusterv1.PatchSelector {
200
+ APIVersion : builder .BootstrapGroupVersion .String (),
201
+ Kind : builder .GenericBootstrapConfigTemplateKind ,
202
+ MatchResources : clusterv1.PatchSelectorMatch {
203
+ MachinePoolClass : & clusterv1.PatchSelectorMatchMachinePoolClass {
204
+ Names : []string {"default-mp-worker" },
205
+ },
206
+ },
207
+ },
208
+ JSONPatches : []clusterv1.JSONPatch {
209
+ {
210
+ Op : "add" ,
211
+ Path : "/spec/template/spec/resource" ,
212
+ Value : & apiextensionsv1.JSON {Raw : []byte (`"default-mp-worker-bootstrap"` )},
213
+ },
214
+ },
215
+ },
178
216
},
179
217
},
180
218
},
@@ -187,6 +225,14 @@ func TestApply(t *testing.T) {
187
225
"default-worker-topo1" : {"spec.template.spec.resource" : "default-worker-infra" },
188
226
"default-worker-topo2" : {"spec.template.spec.resource" : "default-worker-infra" },
189
227
},
228
+ machinePoolBootstrapConfig : map [string ]map [string ]interface {}{
229
+ "default-mp-worker-topo1" : {"spec.resource" : "default-mp-worker-bootstrap" },
230
+ "default-mp-worker-topo2" : {"spec.resource" : "default-mp-worker-bootstrap" },
231
+ },
232
+ machinePoolInfrastructureMachinePool : map [string ]map [string ]interface {}{
233
+ "default-mp-worker-topo1" : {"spec.resource" : "default-mp-worker-infra" },
234
+ "default-mp-worker-topo2" : {"spec.resource" : "default-mp-worker-infra" },
235
+ },
190
236
},
191
237
},
192
238
{
@@ -580,6 +626,46 @@ func TestApply(t *testing.T) {
580
626
},
581
627
},
582
628
},
629
+ {
630
+ Selector : clusterv1.PatchSelector {
631
+ APIVersion : builder .BootstrapGroupVersion .String (),
632
+ Kind : builder .GenericBootstrapConfigTemplateKind ,
633
+ MatchResources : clusterv1.PatchSelectorMatch {
634
+ MachinePoolClass : & clusterv1.PatchSelectorMatchMachinePoolClass {
635
+ Names : []string {"default-mp-worker" },
636
+ },
637
+ },
638
+ },
639
+ JSONPatches : []clusterv1.JSONPatch {
640
+ {
641
+ Op : "add" ,
642
+ Path : "/spec/template/spec/machinePoolTopologyName" ,
643
+ ValueFrom : & clusterv1.JSONPatchValue {
644
+ Variable : pointer .String ("builtin.machinePool.topologyName" ),
645
+ },
646
+ },
647
+ },
648
+ },
649
+ {
650
+ Selector : clusterv1.PatchSelector {
651
+ APIVersion : builder .InfrastructureGroupVersion .String (),
652
+ Kind : builder .GenericInfrastructureMachinePoolTemplateKind ,
653
+ MatchResources : clusterv1.PatchSelectorMatch {
654
+ MachinePoolClass : & clusterv1.PatchSelectorMatchMachinePoolClass {
655
+ Names : []string {"default-mp-worker" },
656
+ },
657
+ },
658
+ },
659
+ JSONPatches : []clusterv1.JSONPatch {
660
+ {
661
+ Op : "add" ,
662
+ Path : "/spec/template/spec/machinePoolTopologyName" ,
663
+ ValueFrom : & clusterv1.JSONPatchValue {
664
+ Variable : pointer .String ("builtin.machinePool.topologyName" ),
665
+ },
666
+ },
667
+ },
668
+ },
583
669
},
584
670
},
585
671
},
@@ -601,6 +687,14 @@ func TestApply(t *testing.T) {
601
687
"default-worker-topo1" : {"spec.template.spec.machineDeploymentTopologyName" : "default-worker-topo1" },
602
688
"default-worker-topo2" : {"spec.template.spec.machineDeploymentTopologyName" : "default-worker-topo2" },
603
689
},
690
+ machinePoolInfrastructureMachinePool : map [string ]map [string ]interface {}{
691
+ "default-mp-worker-topo1" : {"spec.machinePoolTopologyName" : "default-mp-worker-topo1" },
692
+ "default-mp-worker-topo2" : {"spec.machinePoolTopologyName" : "default-mp-worker-topo2" },
693
+ },
694
+ machinePoolBootstrapConfig : map [string ]map [string ]interface {}{
695
+ "default-mp-worker-topo1" : {"spec.machinePoolTopologyName" : "default-mp-worker-topo1" },
696
+ "default-mp-worker-topo2" : {"spec.machinePoolTopologyName" : "default-mp-worker-topo2" },
697
+ },
604
698
},
605
699
},
606
700
{
@@ -614,6 +708,14 @@ func TestApply(t *testing.T) {
614
708
},
615
709
},
616
710
},
711
+ {
712
+ Name : "default-mp-worker-infra" ,
713
+ Definitions : []clusterv1.ClusterClassStatusVariableDefinition {
714
+ {
715
+ From : "inline" ,
716
+ },
717
+ },
718
+ },
617
719
{
618
720
Name : "infraCluster" ,
619
721
DefinitionsConflict : true ,
@@ -689,6 +791,46 @@ func TestApply(t *testing.T) {
689
791
},
690
792
},
691
793
},
794
+ {
795
+ Selector : clusterv1.PatchSelector {
796
+ APIVersion : builder .BootstrapGroupVersion .String (),
797
+ Kind : builder .GenericBootstrapConfigTemplateKind ,
798
+ MatchResources : clusterv1.PatchSelectorMatch {
799
+ MachinePoolClass : & clusterv1.PatchSelectorMatchMachinePoolClass {
800
+ Names : []string {"default-mp-worker" },
801
+ },
802
+ },
803
+ },
804
+ JSONPatches : []clusterv1.JSONPatch {
805
+ {
806
+ Op : "add" ,
807
+ Path : "/spec/template/spec/resource" ,
808
+ ValueFrom : & clusterv1.JSONPatchValue {
809
+ Variable : pointer .String ("default-mp-worker-infra" ),
810
+ },
811
+ },
812
+ },
813
+ },
814
+ {
815
+ Selector : clusterv1.PatchSelector {
816
+ APIVersion : builder .InfrastructureGroupVersion .String (),
817
+ Kind : builder .GenericInfrastructureMachinePoolTemplateKind ,
818
+ MatchResources : clusterv1.PatchSelectorMatch {
819
+ MachinePoolClass : & clusterv1.PatchSelectorMatchMachinePoolClass {
820
+ Names : []string {"default-mp-worker" },
821
+ },
822
+ },
823
+ },
824
+ JSONPatches : []clusterv1.JSONPatch {
825
+ {
826
+ Op : "add" ,
827
+ Path : "/spec/template/spec/resource" ,
828
+ ValueFrom : & clusterv1.JSONPatchValue {
829
+ Variable : pointer .String ("default-mp-worker-infra" ),
830
+ },
831
+ },
832
+ },
833
+ },
692
834
},
693
835
},
694
836
},
@@ -704,6 +846,14 @@ func TestApply(t *testing.T) {
704
846
"default-worker-topo1" : {"spec.template.spec.resource" : "value1" },
705
847
"default-worker-topo2" : {"spec.template.spec.resource" : "default-worker-topo2" },
706
848
},
849
+ machinePoolInfrastructureMachinePool : map [string ]map [string ]interface {}{
850
+ "default-mp-worker-topo1" : {"spec.resource" : "value2" },
851
+ "default-mp-worker-topo2" : {"spec.resource" : "default-mp-worker-topo2" },
852
+ },
853
+ machinePoolBootstrapConfig : map [string ]map [string ]interface {}{
854
+ "default-mp-worker-topo1" : {"spec.resource" : "value2" },
855
+ "default-mp-worker-topo2" : {"spec.resource" : "default-mp-worker-topo2" },
856
+ },
707
857
},
708
858
},
709
859
}
@@ -716,10 +866,13 @@ func TestApply(t *testing.T) {
716
866
// * A ClusterClass with its corresponding templates:
717
867
// * ControlPlaneTemplate with a corresponding ControlPlane InfrastructureMachineTemplate.
718
868
// * MachineDeploymentClass "default-worker" with corresponding BootstrapTemplate and InfrastructureMachineTemplate.
869
+ // * MachinePoolClass "default-mp-worker" with corresponding BootstrapTemplate and InfrastructureMachineTemplate.
719
870
// * The corresponding Cluster.spec.topology:
720
871
// * with 3 ControlPlane replicas
721
872
// * with a "default-worker-topo1" MachineDeploymentTopology without replicas (based on "default-worker")
873
+ // * with a "default-mp-worker-topo1" MachinePoolTopology without replicas (based on "default-mp-worker")
722
874
// * with a "default-worker-topo2" MachineDeploymentTopology with 3 replicas (based on "default-worker")
875
+ // * with a "default-mp-worker-topo2" MachinePoolTopology with 3 replicas (based on "default-mp-worker")
723
876
// * desired: essentially the corresponding desired objects.
724
877
blueprint , desired := setupTestObjects ()
725
878
@@ -764,6 +917,12 @@ func TestApply(t *testing.T) {
764
917
expectedBootstrapTemplates [mdTopology ] = md .BootstrapTemplate .DeepCopy ()
765
918
expectedInfrastructureMachineTemplate [mdTopology ] = md .InfrastructureMachineTemplate .DeepCopy ()
766
919
}
920
+ expectedBootstrapConfig := map [string ]* unstructured.Unstructured {}
921
+ expectedInfrastructureMachinePool := map [string ]* unstructured.Unstructured {}
922
+ for mpTopology , mp := range desired .MachinePools {
923
+ expectedBootstrapConfig [mpTopology ] = mp .BootstrapObject .DeepCopy ()
924
+ expectedInfrastructureMachinePool [mpTopology ] = mp .InfrastructureMachinePoolObject .DeepCopy ()
925
+ }
767
926
768
927
// Set expected fields on the copy of the objects, so they can be used for comparison with the result of Apply.
769
928
if tt .expectedFields .infrastructureCluster != nil {
@@ -781,6 +940,12 @@ func TestApply(t *testing.T) {
781
940
for mdTopology , expectedFields := range tt .expectedFields .machineDeploymentInfrastructureMachineTemplate {
782
941
setSpecFields (expectedInfrastructureMachineTemplate [mdTopology ], expectedFields )
783
942
}
943
+ for mpTopology , expectedFields := range tt .expectedFields .machinePoolBootstrapConfig {
944
+ setSpecFields (expectedBootstrapConfig [mpTopology ], expectedFields )
945
+ }
946
+ for mpTopology , expectedFields := range tt .expectedFields .machinePoolInfrastructureMachinePool {
947
+ setSpecFields (expectedInfrastructureMachinePool [mpTopology ], expectedFields )
948
+ }
784
949
785
950
// Apply patches.
786
951
if err := patchEngine .Apply (context .Background (), blueprint , desired ); err != nil {
@@ -801,6 +966,12 @@ func TestApply(t *testing.T) {
801
966
for mdTopology , infrastructureMachineTemplate := range expectedInfrastructureMachineTemplate {
802
967
g .Expect (desired .MachineDeployments [mdTopology ].InfrastructureMachineTemplate ).To (EqualObject (infrastructureMachineTemplate ))
803
968
}
969
+ for mpTopology , bootstrapConfig := range expectedBootstrapConfig {
970
+ g .Expect (desired .MachinePools [mpTopology ].BootstrapObject ).To (EqualObject (bootstrapConfig ))
971
+ }
972
+ for mpTopology , infrastructureMachinePool := range expectedInfrastructureMachinePool {
973
+ g .Expect (desired .MachinePools [mpTopology ].InfrastructureMachinePoolObject ).To (EqualObject (infrastructureMachinePool ))
974
+ }
804
975
})
805
976
}
806
977
}
@@ -817,18 +988,29 @@ func setupTestObjects() (*scope.ClusterBlueprint, *scope.ClusterState) {
817
988
818
989
workerInfrastructureMachineTemplate := builder .InfrastructureMachineTemplate (metav1 .NamespaceDefault , "linux-worker-inframachinetemplate" ).
819
990
Build ()
991
+ workerInfrastructureMachinePoolTemplate := builder .InfrastructureMachinePoolTemplate (metav1 .NamespaceDefault , "linux-worker-inframachinetemplate" ).
992
+ Build ()
993
+ workerInfrastructureMachinePool := builder .InfrastructureMachinePool (metav1 .NamespaceDefault , "linux-worker-inframachinetemplate" ).
994
+ Build ()
820
995
workerBootstrapTemplate := builder .BootstrapTemplate (metav1 .NamespaceDefault , "linux-worker-bootstraptemplate" ).
821
996
Build ()
997
+ workerBootstrapConfig := builder .BootstrapConfig (metav1 .NamespaceDefault , "linux-worker-bootstraptemplate" ).
998
+ Build ()
822
999
mdClass1 := builder .MachineDeploymentClass ("default-worker" ).
823
1000
WithInfrastructureTemplate (workerInfrastructureMachineTemplate ).
824
1001
WithBootstrapTemplate (workerBootstrapTemplate ).
825
1002
Build ()
1003
+ mpClass1 := builder .MachinePoolClass ("default-mp-worker" ).
1004
+ WithInfrastructureTemplate (workerInfrastructureMachinePoolTemplate ).
1005
+ WithBootstrapTemplate (workerBootstrapTemplate ).
1006
+ Build ()
826
1007
827
1008
clusterClass := builder .ClusterClass (metav1 .NamespaceDefault , "clusterClass1" ).
828
1009
WithInfrastructureClusterTemplate (infrastructureClusterTemplate ).
829
1010
WithControlPlaneTemplate (controlPlaneTemplate ).
830
1011
WithControlPlaneInfrastructureMachineTemplate (controlPlaneInfrastructureMachineTemplate ).
831
1012
WithWorkerMachineDeploymentClasses (* mdClass1 ).
1013
+ WithWorkerMachinePoolClasses (* mpClass1 ).
832
1014
Build ()
833
1015
834
1016
// Note: we depend on TypeMeta being set to calculate HolderReferences correctly.
@@ -880,6 +1062,12 @@ func setupTestObjects() (*scope.ClusterBlueprint, *scope.ClusterState) {
880
1062
Value : apiextensionsv1.JSON {Raw : []byte (`"default-worker-topo2"` )},
881
1063
DefinitionFrom : "inline" ,
882
1064
},
1065
+ {
1066
+ Name : "default-mp-worker-infra" ,
1067
+ // This value should be overwritten for the default-mp-worker-topo1 MachineDeployment.
1068
+ Value : apiextensionsv1.JSON {Raw : []byte (`"default-mp-worker-topo2"` )},
1069
+ DefinitionFrom : "inline" ,
1070
+ },
883
1071
},
884
1072
Workers : & clusterv1.WorkersTopology {
885
1073
MachineDeployments : []clusterv1.MachineDeploymentTopology {
@@ -904,6 +1092,28 @@ func setupTestObjects() (*scope.ClusterBlueprint, *scope.ClusterState) {
904
1092
Replicas : pointer .Int32 (5 ),
905
1093
},
906
1094
},
1095
+ MachinePools : []clusterv1.MachinePoolTopology {
1096
+ {
1097
+ Metadata : clusterv1.ObjectMeta {},
1098
+ Class : "default-mp-worker" ,
1099
+ Name : "default-mp-worker-topo1" ,
1100
+ Variables : & clusterv1.MachinePoolVariables {
1101
+ Overrides : []clusterv1.ClusterVariable {
1102
+ {
1103
+ Name : "default-mp-worker-infra" ,
1104
+ DefinitionFrom : "inline" ,
1105
+ Value : apiextensionsv1.JSON {Raw : []byte (`"value2"` )},
1106
+ },
1107
+ },
1108
+ },
1109
+ },
1110
+ {
1111
+ Metadata : clusterv1.ObjectMeta {},
1112
+ Class : "default-mp-worker" ,
1113
+ Name : "default-mp-worker-topo2" ,
1114
+ Replicas : pointer .Int32 (5 ),
1115
+ },
1116
+ },
907
1117
},
908
1118
},
909
1119
},
@@ -924,6 +1134,12 @@ func setupTestObjects() (*scope.ClusterBlueprint, *scope.ClusterState) {
924
1134
BootstrapTemplate : workerBootstrapTemplate ,
925
1135
},
926
1136
},
1137
+ MachinePools : map [string ]* scope.MachinePoolBlueprint {
1138
+ "default-mp-worker" : {
1139
+ InfrastructureMachinePoolTemplate : workerInfrastructureMachinePoolTemplate ,
1140
+ BootstrapTemplate : workerBootstrapTemplate ,
1141
+ },
1142
+ },
927
1143
}
928
1144
929
1145
// Create a Cluster using the ClusterClass from above with multiple MachineDeployments
@@ -974,6 +1190,27 @@ func setupTestObjects() (*scope.ClusterBlueprint, *scope.ClusterState) {
974
1190
BootstrapTemplate : workerBootstrapTemplate .DeepCopy (),
975
1191
},
976
1192
},
1193
+ MachinePools : map [string ]* scope.MachinePoolState {
1194
+ "default-mp-worker-topo1" : {
1195
+ Object : builder .MachinePool (metav1 .NamespaceDefault , "mp1" ).
1196
+ WithLabels (map [string ]string {clusterv1 .ClusterTopologyMachinePoolNameLabel : "default-mp-worker-topo1" }).
1197
+ WithVersion ("v1.21.2" ).
1198
+ Build (),
1199
+ // Make sure we're using an independent instance of the template.
1200
+ InfrastructureMachinePoolObject : workerInfrastructureMachinePool .DeepCopy (),
1201
+ BootstrapObject : workerBootstrapConfig .DeepCopy (),
1202
+ },
1203
+ "default-mp-worker-topo2" : {
1204
+ Object : builder .MachinePool (metav1 .NamespaceDefault , "mp2" ).
1205
+ WithLabels (map [string ]string {clusterv1 .ClusterTopologyMachinePoolNameLabel : "default-mp-worker-topo2" }).
1206
+ WithVersion ("v1.20.6" ).
1207
+ WithReplicas (5 ).
1208
+ Build (),
1209
+ // Make sure we're using an independent instance of the template.
1210
+ InfrastructureMachinePoolObject : workerInfrastructureMachinePool .DeepCopy (),
1211
+ BootstrapObject : workerBootstrapConfig .DeepCopy (),
1212
+ },
1213
+ },
977
1214
}
978
1215
return blueprint , desired
979
1216
}
0 commit comments