Skip to content

Commit 6ac7085

Browse files
authored
Merge pull request kubernetes-sigs#10498 from willie-yao/mp-upgrade
🌱 Support MachinePools without MachinePoolMachines in clusterctl upgrade test
2 parents 99866da + bd1fb5a commit 6ac7085

File tree

1 file changed

+117
-30
lines changed

1 file changed

+117
-30
lines changed

test/e2e/clusterctl_upgrade.go

Lines changed: 117 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package e2e
1818

1919
import (
2020
"context"
21+
"errors"
2122
"fmt"
2223
"io"
2324
"net/http"
@@ -45,6 +46,8 @@ import (
4546

4647
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
4748
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
49+
"sigs.k8s.io/cluster-api/controllers/external"
50+
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
4851
"sigs.k8s.io/cluster-api/test/e2e/internal/log"
4952
"sigs.k8s.io/cluster-api/test/framework"
5053
"sigs.k8s.io/cluster-api/test/framework/bootstrap"
@@ -428,8 +431,14 @@ func ClusterctlUpgradeSpec(ctx context.Context, inputGetter func() ClusterctlUpg
428431
Name: workloadClusterName,
429432
}, input.E2EConfig.GetIntervals(specName, "wait-cluster")...)
430433

431-
expectedMachineCount := *controlPlaneMachineCount + calculateExpectedWorkerCount(ctx, managementClusterProxy.GetClient(), workloadClusterUnstructured, coreCAPIStorageVersion)
434+
By("Calculating expected MachineDeployment and MachinePool Machine and Node counts")
435+
expectedMachineDeploymentMachineCount := calculateExpectedMachineDeploymentMachineCount(ctx, managementClusterProxy.GetClient(), workloadClusterUnstructured, coreCAPIStorageVersion)
436+
expectedMachinePoolNodeCount := calculateExpectedMachinePoolNodeCount(ctx, managementClusterProxy.GetClient(), workloadClusterUnstructured, coreCAPIStorageVersion)
437+
expectedMachinePoolMachineCount, err := calculateExpectedMachinePoolMachineCount(ctx, managementClusterProxy.GetClient(), workloadClusterNamespace, workloadClusterName)
438+
Expect(err).ToNot(HaveOccurred())
439+
expectedMachineCount := *controlPlaneMachineCount + expectedMachineDeploymentMachineCount + expectedMachinePoolMachineCount
432440

441+
Byf("Expect %d Machines and %d MachinePool replicas to exist", expectedMachineCount, expectedMachinePoolNodeCount)
433442
By("Waiting for the machines to exist")
434443
Eventually(func() (int64, error) {
435444
var n int64
@@ -455,6 +464,26 @@ func ClusterctlUpgradeSpec(ctx context.Context, inputGetter func() ClusterctlUpg
455464
return n, nil
456465
}, input.E2EConfig.GetIntervals(specName, "wait-worker-nodes")...).Should(Equal(expectedMachineCount), "Timed out waiting for all Machines to exist")
457466

467+
By("Waiting for MachinePool to be ready with correct number of replicas")
468+
Eventually(func() (int64, error) {
469+
var n int64
470+
machinePoolList := &expv1.MachinePoolList{}
471+
if err := managementClusterProxy.GetClient().List(
472+
ctx,
473+
machinePoolList,
474+
client.InNamespace(workloadClusterNamespace),
475+
client.MatchingLabels{clusterv1.ClusterNameLabel: workloadClusterName},
476+
); err == nil {
477+
for _, mp := range machinePoolList.Items {
478+
if mp.Status.Phase == string(expv1.MachinePoolPhaseRunning) {
479+
n += int64(mp.Status.ReadyReplicas)
480+
}
481+
}
482+
}
483+
484+
return n, nil
485+
}, input.E2EConfig.GetIntervals(specName, "wait-worker-nodes")...).Should(Equal(expectedMachinePoolNodeCount), "Timed out waiting for all MachinePool replicas to be ready")
486+
458487
By("THE MANAGEMENT CLUSTER WITH OLDER VERSION OF PROVIDERS WORKS!")
459488

460489
for i, upgrade := range input.Upgrades {
@@ -609,20 +638,24 @@ func ClusterctlUpgradeSpec(ctx context.Context, inputGetter func() ClusterctlUpg
609638
ClusterName: workloadClusterName,
610639
Namespace: workloadClusterNamespace,
611640
})
612-
framework.ScaleAndWaitMachineDeployment(ctx, framework.ScaleAndWaitMachineDeploymentInput{
613-
ClusterProxy: managementClusterProxy,
614-
Cluster: workloadCluster,
615-
MachineDeployment: testMachineDeployments[0],
616-
Replicas: 2,
617-
WaitForMachineDeployments: input.E2EConfig.GetIntervals(specName, "wait-worker-nodes"),
618-
})
619-
framework.ScaleAndWaitMachineDeployment(ctx, framework.ScaleAndWaitMachineDeploymentInput{
620-
ClusterProxy: managementClusterProxy,
621-
Cluster: workloadCluster,
622-
MachineDeployment: testMachineDeployments[0],
623-
Replicas: 1,
624-
WaitForMachineDeployments: input.E2EConfig.GetIntervals(specName, "wait-worker-nodes"),
625-
})
641+
if len(testMachineDeployments) > 0 {
642+
framework.ScaleAndWaitMachineDeployment(ctx, framework.ScaleAndWaitMachineDeploymentInput{
643+
ClusterProxy: managementClusterProxy,
644+
Cluster: workloadCluster,
645+
MachineDeployment: testMachineDeployments[0],
646+
Replicas: 2,
647+
WaitForMachineDeployments: input.E2EConfig.GetIntervals(specName, "wait-worker-nodes"),
648+
})
649+
framework.ScaleAndWaitMachineDeployment(ctx, framework.ScaleAndWaitMachineDeploymentInput{
650+
ClusterProxy: managementClusterProxy,
651+
Cluster: workloadCluster,
652+
MachineDeployment: testMachineDeployments[0],
653+
Replicas: 1,
654+
WaitForMachineDeployments: input.E2EConfig.GetIntervals(specName, "wait-worker-nodes"),
655+
})
656+
} else {
657+
Byf("[%d] No MachineDeployments found to scale", i)
658+
}
626659
}
627660

628661
Byf("[%d] Verify client-side SSA still works", i)
@@ -763,8 +796,8 @@ func discoveryAndWaitForCluster(ctx context.Context, input discoveryAndWaitForCl
763796
return cluster
764797
}
765798

766-
func calculateExpectedWorkerCount(ctx context.Context, c client.Client, unstructuredCluster *unstructured.Unstructured, coreCAPIStorageVersion string) int64 {
767-
var expectedWorkerCount int64
799+
func calculateExpectedMachineDeploymentMachineCount(ctx context.Context, c client.Client, unstructuredCluster *unstructured.Unstructured, coreCAPIStorageVersion string) int64 {
800+
var expectedMachineDeploymentWorkerCount int64
768801

769802
// Convert v1beta1 unstructured Cluster to clusterv1.Cluster
770803
// Only v1beta1 Cluster support ClusterClass (i.e. have cluster.spec.topology).
@@ -778,16 +811,10 @@ func calculateExpectedWorkerCount(ctx context.Context, c client.Client, unstruct
778811
if md.Replicas == nil {
779812
continue
780813
}
781-
expectedWorkerCount += int64(*md.Replicas)
782-
}
783-
for _, mp := range cluster.Spec.Topology.Workers.MachinePools {
784-
if mp.Replicas == nil {
785-
continue
786-
}
787-
expectedWorkerCount += int64(*mp.Replicas)
814+
expectedMachineDeploymentWorkerCount += int64(*md.Replicas)
788815
}
789816
}
790-
return expectedWorkerCount
817+
return expectedMachineDeploymentWorkerCount
791818
}
792819
}
793820

@@ -811,7 +838,67 @@ func calculateExpectedWorkerCount(ctx context.Context, c client.Client, unstruct
811838
if !ok {
812839
continue
813840
}
814-
expectedWorkerCount += replicas
841+
expectedMachineDeploymentWorkerCount += replicas
842+
}
843+
844+
return expectedMachineDeploymentWorkerCount
845+
}
846+
847+
func calculateExpectedMachinePoolMachineCount(ctx context.Context, c client.Client, workloadClusterNamespace, workloadClusterName string) (int64, error) {
848+
expectedMachinePoolMachineCount := int64(0)
849+
850+
machinePoolList := &expv1.MachinePoolList{}
851+
if err := c.List(
852+
ctx,
853+
machinePoolList,
854+
client.InNamespace(workloadClusterNamespace),
855+
client.MatchingLabels{clusterv1.ClusterNameLabel: workloadClusterName},
856+
); err == nil {
857+
for _, mp := range machinePoolList.Items {
858+
mp := mp
859+
infraMachinePool, err := external.Get(ctx, c, &mp.Spec.Template.Spec.InfrastructureRef, workloadClusterNamespace)
860+
if err != nil {
861+
return 0, err
862+
}
863+
// Check if the InfraMachinePool has an infrastructureMachineKind field. If it does not, we should skip checking for MachinePool machines.
864+
err = util.UnstructuredUnmarshalField(infraMachinePool, ptr.To(""), "status", "infrastructureMachineKind")
865+
if err != nil && !errors.Is(err, util.ErrUnstructuredFieldNotFound) {
866+
return 0, err
867+
}
868+
if err == nil {
869+
expectedMachinePoolMachineCount += int64(*mp.Spec.Replicas)
870+
}
871+
}
872+
}
873+
874+
return expectedMachinePoolMachineCount, nil
875+
}
876+
877+
func calculateExpectedMachinePoolNodeCount(ctx context.Context, c client.Client, unstructuredCluster *unstructured.Unstructured, coreCAPIStorageVersion string) int64 {
878+
var expectedMachinePoolWorkerCount int64
879+
880+
// Convert v1beta1 unstructured Cluster to clusterv1.Cluster
881+
// Only v1beta1 Cluster support ClusterClass (i.e. have cluster.spec.topology).
882+
if unstructuredCluster.GroupVersionKind().Version == clusterv1.GroupVersion.Version {
883+
cluster := &clusterv1.Cluster{}
884+
Expect(apiruntime.DefaultUnstructuredConverter.FromUnstructured(unstructuredCluster.Object, cluster)).To(Succeed())
885+
886+
if cluster.Spec.Topology != nil {
887+
if cluster.Spec.Topology.Workers != nil {
888+
for _, mp := range cluster.Spec.Topology.Workers.MachinePools {
889+
if mp.Replicas == nil {
890+
continue
891+
}
892+
expectedMachinePoolWorkerCount += int64(*mp.Replicas)
893+
}
894+
}
895+
return expectedMachinePoolWorkerCount
896+
}
897+
}
898+
899+
byClusterOptions := []client.ListOption{
900+
client.InNamespace(unstructuredCluster.GetNamespace()),
901+
client.MatchingLabels{clusterv1.ClusterNameLabel: unstructuredCluster.GetName()},
815902
}
816903

817904
machinePoolList := &unstructured.UnstructuredList{}
@@ -827,16 +914,16 @@ func calculateExpectedWorkerCount(ctx context.Context, c client.Client, unstruct
827914
Eventually(func() error {
828915
return c.List(ctx, machinePoolList, byClusterOptions...)
829916
}, 3*time.Minute, 3*time.Second).Should(Succeed(), "Failed to list MachinePool object for Cluster %s", klog.KObj(unstructuredCluster))
830-
for _, md := range machinePoolList.Items {
831-
replicas, ok, err := unstructured.NestedInt64(md.Object, "spec", "replicas")
917+
for _, mp := range machinePoolList.Items {
918+
replicas, ok, err := unstructured.NestedInt64(mp.Object, "spec", "replicas")
832919
Expect(err).ToNot(HaveOccurred())
833920
if !ok {
834921
continue
835922
}
836-
expectedWorkerCount += replicas
923+
expectedMachinePoolWorkerCount += replicas
837924
}
838925

839-
return expectedWorkerCount
926+
return expectedMachinePoolWorkerCount
840927
}
841928

842929
// deleteAllClustersAndWaitInput is the input type for deleteAllClustersAndWait.

0 commit comments

Comments
 (0)