diff --git a/test/e2e/clusterclass_changes.go b/test/e2e/clusterclass_changes.go index de02b673ce0a..206bfd834a21 100644 --- a/test/e2e/clusterclass_changes.go +++ b/test/e2e/clusterclass_changes.go @@ -177,6 +177,12 @@ func ClusterClassChangesSpec(ctx context.Context, inputGetter func() ClusterClas WaitForMachineDeployments: input.E2EConfig.GetIntervals(specName, "wait-worker-nodes"), }) + By("Deleting a MachineDeploymentTopology in the Cluster Topology and wait for associated MachineDeployment to be deleted") + deleteMachineDeploymentTopologyAndWait(ctx, deleteMachineDeploymentTopologyAndWaitInput{ + ClusterProxy: input.BootstrapClusterProxy, + Cluster: clusterResources.Cluster, + WaitForMachineDeployments: input.E2EConfig.GetIntervals(specName, "wait-worker-nodes"), + }) By("PASSED!") }) @@ -458,3 +464,42 @@ func rebaseClusterClassAndWait(ctx context.Context, input rebaseClusterClassAndW Expect(afterControlPlane.GetGeneration()).To(Equal(beforeControlPlane.GetGeneration()), "ControlPlane generation should not be incremented during the rebase because ControlPlane should not be affected.") } + +// deleteMachineDeploymentTopologyAndWaitInput is the input type for deleteMachineDeploymentTopologyAndWaitInput. +type deleteMachineDeploymentTopologyAndWaitInput struct { + ClusterProxy framework.ClusterProxy + Cluster *clusterv1.Cluster + WaitForMachineDeployments []interface{} +} + +// deleteMachineDeploymentTopologyAndWait deletes a MachineDeploymentTopology from the Cluster and waits until the changes +// are rolled out by ensuring the associated MachineDeployment is correctly deleted. +func deleteMachineDeploymentTopologyAndWait(ctx context.Context, input deleteMachineDeploymentTopologyAndWaitInput) { + Expect(ctx).NotTo(BeNil(), "ctx is required for deleteMachineDeploymentTopologyAndWait") + Expect(input.ClusterProxy).ToNot(BeNil(), "Invalid argument. input.ClusterProxy can't be nil when calling deleteMachineDeploymentTopologyAndWait") + Expect(input.Cluster).ToNot(BeNil(), "Invalid argument. input.Cluster can't be nil when calling deleteMachineDeploymentTopologyAndWait") + Expect(len(input.Cluster.Spec.Topology.Workers.MachineDeployments)).To(BeNumerically(">", 0), + "Invalid Cluster. deleteMachineDeploymentTopologyAndWait requires at least one MachineDeploymentTopology to be defined in the Cluster topology") + + log.Logf("Removing MachineDeploymentTopology from the Cluster Topology.") + patchHelper, err := patch.NewHelper(input.Cluster, input.ClusterProxy.GetClient()) + Expect(err).ToNot(HaveOccurred()) + + // Remove the first MachineDeploymentTopology under input.Cluster.Spec.Topology.Workers.MachineDeployments + mdTopologyToDelete := input.Cluster.Spec.Topology.Workers.MachineDeployments[0] + input.Cluster.Spec.Topology.Workers.MachineDeployments = input.Cluster.Spec.Topology.Workers.MachineDeployments[1:] + Expect(patchHelper.Patch(ctx, input.Cluster)).To(Succeed()) + + log.Logf("Waiting for MachineDeployment to be deleted.") + Eventually(func() error { + // Get MachineDeployment for the current MachineDeploymentTopology. + mdList := &clusterv1.MachineDeploymentList{} + Expect(input.ClusterProxy.GetClient().List(ctx, mdList, client.InNamespace(input.Cluster.Namespace), client.MatchingLabels{ + clusterv1.ClusterTopologyMachineDeploymentLabelName: mdTopologyToDelete.Name, + })).To(Succeed()) + if len(mdList.Items) != 0 { + return errors.Errorf("expected no MachineDeployment for topology %q, but got %d", mdTopologyToDelete.Name, len(mdList.Items)) + } + return nil + }, input.WaitForMachineDeployments...).Should(BeNil()) +}