Skip to content

Commit 2d7d193

Browse files
authored
Merge pull request kubernetes-sigs#9373 from mercedes-benz/cc-mp-engine-tests
🌱 Add MachinePool test cases to engine tests
2 parents b98880e + 7a2295d commit 2d7d193

File tree

1 file changed

+238
-1
lines changed

1 file changed

+238
-1
lines changed

internal/controllers/topology/cluster/patches/engine_test.go

Lines changed: 238 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ func TestApply(t *testing.T) {
4949
controlPlaneInfrastructureMachineTemplate map[string]interface{}
5050
machineDeploymentBootstrapTemplate map[string]map[string]interface{}
5151
machineDeploymentInfrastructureMachineTemplate map[string]map[string]interface{}
52+
machinePoolBootstrapConfig map[string]map[string]interface{}
53+
machinePoolInfrastructureMachinePool map[string]map[string]interface{}
5254
}
5355

5456
tests := []struct {
@@ -134,7 +136,7 @@ func TestApply(t *testing.T) {
134136
},
135137
},
136138
{
137-
name: "Should apply JSON patches to MachineDeployment templates",
139+
name: "Should apply JSON patches to MachineDeployment and MachinePool templates",
138140
patches: []clusterv1.ClusterClassPatch{
139141
{
140142
Name: "fake-patch1",
@@ -175,6 +177,42 @@ func TestApply(t *testing.T) {
175177
},
176178
},
177179
},
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+
},
178216
},
179217
},
180218
},
@@ -187,6 +225,14 @@ func TestApply(t *testing.T) {
187225
"default-worker-topo1": {"spec.template.spec.resource": "default-worker-infra"},
188226
"default-worker-topo2": {"spec.template.spec.resource": "default-worker-infra"},
189227
},
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+
},
190236
},
191237
},
192238
{
@@ -580,6 +626,46 @@ func TestApply(t *testing.T) {
580626
},
581627
},
582628
},
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+
},
583669
},
584670
},
585671
},
@@ -601,6 +687,14 @@ func TestApply(t *testing.T) {
601687
"default-worker-topo1": {"spec.template.spec.machineDeploymentTopologyName": "default-worker-topo1"},
602688
"default-worker-topo2": {"spec.template.spec.machineDeploymentTopologyName": "default-worker-topo2"},
603689
},
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+
},
604698
},
605699
},
606700
{
@@ -614,6 +708,14 @@ func TestApply(t *testing.T) {
614708
},
615709
},
616710
},
711+
{
712+
Name: "default-mp-worker-infra",
713+
Definitions: []clusterv1.ClusterClassStatusVariableDefinition{
714+
{
715+
From: "inline",
716+
},
717+
},
718+
},
617719
{
618720
Name: "infraCluster",
619721
DefinitionsConflict: true,
@@ -689,6 +791,46 @@ func TestApply(t *testing.T) {
689791
},
690792
},
691793
},
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+
},
692834
},
693835
},
694836
},
@@ -704,6 +846,14 @@ func TestApply(t *testing.T) {
704846
"default-worker-topo1": {"spec.template.spec.resource": "value1"},
705847
"default-worker-topo2": {"spec.template.spec.resource": "default-worker-topo2"},
706848
},
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+
},
707857
},
708858
},
709859
}
@@ -716,10 +866,13 @@ func TestApply(t *testing.T) {
716866
// * A ClusterClass with its corresponding templates:
717867
// * ControlPlaneTemplate with a corresponding ControlPlane InfrastructureMachineTemplate.
718868
// * MachineDeploymentClass "default-worker" with corresponding BootstrapTemplate and InfrastructureMachineTemplate.
869+
// * MachinePoolClass "default-mp-worker" with corresponding BootstrapTemplate and InfrastructureMachineTemplate.
719870
// * The corresponding Cluster.spec.topology:
720871
// * with 3 ControlPlane replicas
721872
// * 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")
722874
// * 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")
723876
// * desired: essentially the corresponding desired objects.
724877
blueprint, desired := setupTestObjects()
725878

@@ -764,6 +917,12 @@ func TestApply(t *testing.T) {
764917
expectedBootstrapTemplates[mdTopology] = md.BootstrapTemplate.DeepCopy()
765918
expectedInfrastructureMachineTemplate[mdTopology] = md.InfrastructureMachineTemplate.DeepCopy()
766919
}
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+
}
767926

768927
// Set expected fields on the copy of the objects, so they can be used for comparison with the result of Apply.
769928
if tt.expectedFields.infrastructureCluster != nil {
@@ -781,6 +940,12 @@ func TestApply(t *testing.T) {
781940
for mdTopology, expectedFields := range tt.expectedFields.machineDeploymentInfrastructureMachineTemplate {
782941
setSpecFields(expectedInfrastructureMachineTemplate[mdTopology], expectedFields)
783942
}
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+
}
784949

785950
// Apply patches.
786951
if err := patchEngine.Apply(context.Background(), blueprint, desired); err != nil {
@@ -801,6 +966,12 @@ func TestApply(t *testing.T) {
801966
for mdTopology, infrastructureMachineTemplate := range expectedInfrastructureMachineTemplate {
802967
g.Expect(desired.MachineDeployments[mdTopology].InfrastructureMachineTemplate).To(EqualObject(infrastructureMachineTemplate))
803968
}
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+
}
804975
})
805976
}
806977
}
@@ -817,18 +988,29 @@ func setupTestObjects() (*scope.ClusterBlueprint, *scope.ClusterState) {
817988

818989
workerInfrastructureMachineTemplate := builder.InfrastructureMachineTemplate(metav1.NamespaceDefault, "linux-worker-inframachinetemplate").
819990
Build()
991+
workerInfrastructureMachinePoolTemplate := builder.InfrastructureMachinePoolTemplate(metav1.NamespaceDefault, "linux-worker-inframachinetemplate").
992+
Build()
993+
workerInfrastructureMachinePool := builder.InfrastructureMachinePool(metav1.NamespaceDefault, "linux-worker-inframachinetemplate").
994+
Build()
820995
workerBootstrapTemplate := builder.BootstrapTemplate(metav1.NamespaceDefault, "linux-worker-bootstraptemplate").
821996
Build()
997+
workerBootstrapConfig := builder.BootstrapConfig(metav1.NamespaceDefault, "linux-worker-bootstraptemplate").
998+
Build()
822999
mdClass1 := builder.MachineDeploymentClass("default-worker").
8231000
WithInfrastructureTemplate(workerInfrastructureMachineTemplate).
8241001
WithBootstrapTemplate(workerBootstrapTemplate).
8251002
Build()
1003+
mpClass1 := builder.MachinePoolClass("default-mp-worker").
1004+
WithInfrastructureTemplate(workerInfrastructureMachinePoolTemplate).
1005+
WithBootstrapTemplate(workerBootstrapTemplate).
1006+
Build()
8261007

8271008
clusterClass := builder.ClusterClass(metav1.NamespaceDefault, "clusterClass1").
8281009
WithInfrastructureClusterTemplate(infrastructureClusterTemplate).
8291010
WithControlPlaneTemplate(controlPlaneTemplate).
8301011
WithControlPlaneInfrastructureMachineTemplate(controlPlaneInfrastructureMachineTemplate).
8311012
WithWorkerMachineDeploymentClasses(*mdClass1).
1013+
WithWorkerMachinePoolClasses(*mpClass1).
8321014
Build()
8331015

8341016
// Note: we depend on TypeMeta being set to calculate HolderReferences correctly.
@@ -880,6 +1062,12 @@ func setupTestObjects() (*scope.ClusterBlueprint, *scope.ClusterState) {
8801062
Value: apiextensionsv1.JSON{Raw: []byte(`"default-worker-topo2"`)},
8811063
DefinitionFrom: "inline",
8821064
},
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+
},
8831071
},
8841072
Workers: &clusterv1.WorkersTopology{
8851073
MachineDeployments: []clusterv1.MachineDeploymentTopology{
@@ -904,6 +1092,28 @@ func setupTestObjects() (*scope.ClusterBlueprint, *scope.ClusterState) {
9041092
Replicas: pointer.Int32(5),
9051093
},
9061094
},
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+
},
9071117
},
9081118
},
9091119
},
@@ -924,6 +1134,12 @@ func setupTestObjects() (*scope.ClusterBlueprint, *scope.ClusterState) {
9241134
BootstrapTemplate: workerBootstrapTemplate,
9251135
},
9261136
},
1137+
MachinePools: map[string]*scope.MachinePoolBlueprint{
1138+
"default-mp-worker": {
1139+
InfrastructureMachinePoolTemplate: workerInfrastructureMachinePoolTemplate,
1140+
BootstrapTemplate: workerBootstrapTemplate,
1141+
},
1142+
},
9271143
}
9281144

9291145
// Create a Cluster using the ClusterClass from above with multiple MachineDeployments
@@ -974,6 +1190,27 @@ func setupTestObjects() (*scope.ClusterBlueprint, *scope.ClusterState) {
9741190
BootstrapTemplate: workerBootstrapTemplate.DeepCopy(),
9751191
},
9761192
},
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+
},
9771214
}
9781215
return blueprint, desired
9791216
}

0 commit comments

Comments
 (0)