Skip to content

Commit f9e624a

Browse files
authored
Merge pull request #7134 from sbueringer/pr-extend-clusterclasschanges-test
🌱 test/e2e: Extend ClusterClass changes test to cover InfrastructureMachineTemplate rotation
2 parents 0f0a598 + 111a268 commit f9e624a

File tree

2 files changed

+68
-9
lines changed

2 files changed

+68
-9
lines changed

test/e2e/clusterclass_changes.go

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"os"
2323
"path/filepath"
24+
"reflect"
2425
"strings"
2526

2627
. "github.com/onsi/ginkgo/v2"
@@ -71,6 +72,15 @@ type ClusterClassChangesSpecInput struct {
7172
// "spec.template.spec.path.to.field": <value>,
7273
// }
7374
ModifyMachineDeploymentBootstrapConfigTemplateFields map[string]interface{}
75+
76+
// ModifyMachineDeploymentInfrastructureMachineTemplateFields are the fields which will be set on the
77+
// InfrastructureMachineTemplate of all MachineDeploymentClasses of the ClusterClass after the initial Cluster creation.
78+
// The test verifies that these fields are rolled out to the MachineDeployments.
79+
// NOTE: The fields are configured in the following format:
80+
// map[string]interface{}{
81+
// "spec.template.spec.path.to.field": <value>,
82+
// }
83+
ModifyMachineDeploymentInfrastructureMachineTemplateFields map[string]interface{}
7484
}
7585

7686
// ClusterClassChangesSpec implements a test that verifies that ClusterClass changes are rolled out successfully.
@@ -81,6 +91,9 @@ type ClusterClassChangesSpecInput struct {
8191
// - Modify the BootstrapTemplate of all MachineDeploymentClasses of the ClusterClass by setting
8292
// ModifyMachineDeploymentBootstrapConfigTemplateFields and wait until the change has been rolled out
8393
// to the MachineDeployments of the Cluster.
94+
// - Modify the InfrastructureMachineTemplate of all MachineDeploymentClasses of the ClusterClass by setting
95+
// ModifyMachineDeploymentInfrastructureMachineTemplateFields and wait until the change has been rolled out
96+
// to the MachineDeployments of the Cluster.
8497
// - Rebase the Cluster to a copy of the ClusterClass which has an additional worker label set. Then wait
8598
// until the change has been rolled out to the MachineDeployments of the Cluster and verify the ControlPlane
8699
// has not been changed.
@@ -110,7 +123,6 @@ func ClusterClassChangesSpec(ctx context.Context, inputGetter func() ClusterClas
110123
Expect(input.E2EConfig.Variables).To(HaveKey(KubernetesVersion))
111124
Expect(input.E2EConfig.Variables).To(HaveValidVersion(input.E2EConfig.GetVariable(KubernetesVersion)))
112125
Expect(input.ModifyControlPlaneFields).ToNot(BeEmpty(), "Invalid argument. input.ModifyControlPlaneFields can't be empty when calling %s spec", specName)
113-
Expect(input.ModifyMachineDeploymentBootstrapConfigTemplateFields).ToNot(BeEmpty(), "Invalid argument. input.ModifyMachineDeploymentBootstrapConfigTemplateFields can't be empty when calling %s spec", specName)
114126

115127
// Set up a Namespace where to host objects for this spec and create a watcher for the namespace events.
116128
namespace, cancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder)
@@ -154,7 +166,8 @@ func ClusterClassChangesSpec(ctx context.Context, inputGetter func() ClusterClas
154166
ClusterClass: clusterResources.ClusterClass,
155167
Cluster: clusterResources.Cluster,
156168
ModifyBootstrapConfigTemplateFields: input.ModifyMachineDeploymentBootstrapConfigTemplateFields,
157-
WaitForMachineDeployments: input.E2EConfig.GetIntervals(specName, "wait-worker-nodes"),
169+
ModifyInfrastructureMachineTemplateFields: input.ModifyMachineDeploymentInfrastructureMachineTemplateFields,
170+
WaitForMachineDeployments: input.E2EConfig.GetIntervals(specName, "wait-worker-nodes"),
158171
})
159172

160173
By("Rebasing the Cluster to a ClusterClass with a modified label for MachineDeployments and wait for changes to be applied to the MachineDeployment objects")
@@ -243,11 +256,12 @@ func modifyControlPlaneViaClusterClassAndWait(ctx context.Context, input modifyC
243256

244257
// modifyMachineDeploymentViaClusterClassAndWaitInput is the input type for modifyMachineDeploymentViaClusterClassAndWait.
245258
type modifyMachineDeploymentViaClusterClassAndWaitInput struct {
246-
ClusterProxy framework.ClusterProxy
247-
ClusterClass *clusterv1.ClusterClass
248-
Cluster *clusterv1.Cluster
249-
ModifyBootstrapConfigTemplateFields map[string]interface{}
250-
WaitForMachineDeployments []interface{}
259+
ClusterProxy framework.ClusterProxy
260+
ClusterClass *clusterv1.ClusterClass
261+
Cluster *clusterv1.Cluster
262+
ModifyBootstrapConfigTemplateFields map[string]interface{}
263+
ModifyInfrastructureMachineTemplateFields map[string]interface{}
264+
WaitForMachineDeployments []interface{}
251265
}
252266

253267
// modifyMachineDeploymentViaClusterClassAndWait modifies the BootstrapConfigTemplate of MachineDeploymentClasses of a ClusterClass
@@ -273,7 +287,6 @@ func modifyMachineDeploymentViaClusterClassAndWait(ctx context.Context, input mo
273287
bootstrapConfigTemplateRef := mdClass.Template.Bootstrap.Ref
274288
bootstrapConfigTemplate, err := external.Get(ctx, mgmtClient, bootstrapConfigTemplateRef, input.Cluster.Namespace)
275289
Expect(err).ToNot(HaveOccurred())
276-
277290
// Create a new BootstrapConfigTemplate object with a new name and ModifyBootstrapConfigTemplateFields set.
278291
newBootstrapConfigTemplate := bootstrapConfigTemplate.DeepCopy()
279292
newBootstrapConfigTemplateName := fmt.Sprintf("%s-%s", bootstrapConfigTemplateRef.Name, util.RandomString(6))
@@ -284,10 +297,25 @@ func modifyMachineDeploymentViaClusterClassAndWait(ctx context.Context, input mo
284297
}
285298
Expect(mgmtClient.Create(ctx, newBootstrapConfigTemplate)).To(Succeed())
286299

300+
// Retrieve InfrastructureMachineTemplate object.
301+
infrastructureMachineTemplateRef := mdClass.Template.Infrastructure.Ref
302+
infrastructureMachineTemplate, err := external.Get(ctx, mgmtClient, infrastructureMachineTemplateRef, input.Cluster.Namespace)
303+
Expect(err).ToNot(HaveOccurred())
304+
// Create a new InfrastructureMachineTemplate object with a new name and ModifyInfrastructureMachineTemplateFields set.
305+
newInfrastructureMachineTemplate := infrastructureMachineTemplate.DeepCopy()
306+
newInfrastructureMachineTemplateName := fmt.Sprintf("%s-%s", infrastructureMachineTemplateRef.Name, util.RandomString(6))
307+
newInfrastructureMachineTemplate.SetName(newInfrastructureMachineTemplateName)
308+
newInfrastructureMachineTemplate.SetResourceVersion("")
309+
for fieldPath, value := range input.ModifyInfrastructureMachineTemplateFields {
310+
Expect(unstructured.SetNestedField(newInfrastructureMachineTemplate.Object, value, strings.Split(fieldPath, ".")...)).To(Succeed())
311+
}
312+
Expect(mgmtClient.Create(ctx, newInfrastructureMachineTemplate)).To(Succeed())
313+
287314
// Patch the BootstrapConfigTemplate ref of the MachineDeploymentClass to reference the new BootstrapConfigTemplate.
288315
patchHelper, err := patch.NewHelper(input.ClusterClass, mgmtClient)
289316
Expect(err).ToNot(HaveOccurred())
290317
bootstrapConfigTemplateRef.Name = newBootstrapConfigTemplateName
318+
infrastructureMachineTemplateRef.Name = newInfrastructureMachineTemplateName
291319
Expect(patchHelper.Patch(ctx, input.ClusterClass)).To(Succeed())
292320

293321
log.Logf("Waiting for MachineDeployment rollout for MachineDeploymentClass %q to complete.", mdClass.Class)
@@ -322,7 +350,23 @@ func modifyMachineDeploymentViaClusterClassAndWait(ctx context.Context, input mo
322350
if err != nil || !ok {
323351
return errors.Errorf("failed to get field %q", fieldPath)
324352
}
325-
if currentValue != expectedValue {
353+
if !reflect.DeepEqual(currentValue, expectedValue) {
354+
return errors.Errorf("field %q should be equal to %q, but is %q", fieldPath, expectedValue, currentValue)
355+
}
356+
}
357+
358+
// Get the corresponding InfrastructureMachineTemplate.
359+
infrastructureMachineTemplateRef := md.Spec.Template.Spec.InfrastructureRef
360+
infrastructureMachineTemplate, err := external.Get(ctx, mgmtClient, &infrastructureMachineTemplateRef, input.Cluster.Namespace)
361+
Expect(err).ToNot(HaveOccurred())
362+
363+
// Verify that ModifyInfrastructureMachineTemplateFields have been set.
364+
for fieldPath, expectedValue := range input.ModifyInfrastructureMachineTemplateFields {
365+
currentValue, ok, err := unstructured.NestedFieldNoCopy(infrastructureMachineTemplate.Object, strings.Split(fieldPath, ".")...)
366+
if err != nil || !ok {
367+
return errors.Errorf("failed to get field %q", fieldPath)
368+
}
369+
if !reflect.DeepEqual(currentValue, expectedValue) {
326370
return errors.Errorf("field %q should be equal to %q, but is %q", fieldPath, expectedValue, currentValue)
327371
}
328372
}

test/e2e/clusterclass_changes_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ var _ = Describe("When testing ClusterClass changes [ClusterClass]", func() {
4444
ModifyMachineDeploymentBootstrapConfigTemplateFields: map[string]interface{}{
4545
"spec.template.spec.verbosity": int64(4),
4646
},
47+
// ModifyMachineDeploymentInfrastructureMachineTemplateFields are the fields which will be set on the
48+
// InfrastructureMachineTemplate of all MachineDeploymentClasses of the ClusterClass after the initial Cluster creation.
49+
// The test verifies that these fields are rolled out to the MachineDeployments.
50+
ModifyMachineDeploymentInfrastructureMachineTemplateFields: map[string]interface{}{
51+
"spec.template.spec.extraMounts": []interface{}{
52+
map[string]interface{}{
53+
"containerPath": "/var/run/docker.sock",
54+
"hostPath": "/var/run/docker.sock",
55+
},
56+
map[string]interface{}{
57+
"containerPath": "/tmp",
58+
"hostPath": "/tmp",
59+
},
60+
},
61+
},
4762
}
4863
})
4964
})

0 commit comments

Comments
 (0)