@@ -18,6 +18,7 @@ package e2e
18
18
19
19
import (
20
20
"context"
21
+ "errors"
21
22
"fmt"
22
23
"io"
23
24
"net/http"
@@ -45,6 +46,8 @@ import (
45
46
46
47
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
47
48
"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"
48
51
"sigs.k8s.io/cluster-api/test/e2e/internal/log"
49
52
"sigs.k8s.io/cluster-api/test/framework"
50
53
"sigs.k8s.io/cluster-api/test/framework/bootstrap"
@@ -428,8 +431,14 @@ func ClusterctlUpgradeSpec(ctx context.Context, inputGetter func() ClusterctlUpg
428
431
Name : workloadClusterName ,
429
432
}, input .E2EConfig .GetIntervals (specName , "wait-cluster" )... )
430
433
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
432
440
441
+ Byf ("Expect %d Machines and %d MachinePool replicas to exist" , expectedMachineCount , expectedMachinePoolNodeCount )
433
442
By ("Waiting for the machines to exist" )
434
443
Eventually (func () (int64 , error ) {
435
444
var n int64
@@ -455,6 +464,26 @@ func ClusterctlUpgradeSpec(ctx context.Context, inputGetter func() ClusterctlUpg
455
464
return n , nil
456
465
}, input .E2EConfig .GetIntervals (specName , "wait-worker-nodes" )... ).Should (Equal (expectedMachineCount ), "Timed out waiting for all Machines to exist" )
457
466
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
+
458
487
By ("THE MANAGEMENT CLUSTER WITH OLDER VERSION OF PROVIDERS WORKS!" )
459
488
460
489
for i , upgrade := range input .Upgrades {
@@ -609,20 +638,24 @@ func ClusterctlUpgradeSpec(ctx context.Context, inputGetter func() ClusterctlUpg
609
638
ClusterName : workloadClusterName ,
610
639
Namespace : workloadClusterNamespace ,
611
640
})
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
+ }
626
659
}
627
660
628
661
Byf ("[%d] Verify client-side SSA still works" , i )
@@ -763,8 +796,8 @@ func discoveryAndWaitForCluster(ctx context.Context, input discoveryAndWaitForCl
763
796
return cluster
764
797
}
765
798
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
768
801
769
802
// Convert v1beta1 unstructured Cluster to clusterv1.Cluster
770
803
// Only v1beta1 Cluster support ClusterClass (i.e. have cluster.spec.topology).
@@ -778,16 +811,10 @@ func calculateExpectedWorkerCount(ctx context.Context, c client.Client, unstruct
778
811
if md .Replicas == nil {
779
812
continue
780
813
}
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 )
788
815
}
789
816
}
790
- return expectedWorkerCount
817
+ return expectedMachineDeploymentWorkerCount
791
818
}
792
819
}
793
820
@@ -811,7 +838,67 @@ func calculateExpectedWorkerCount(ctx context.Context, c client.Client, unstruct
811
838
if ! ok {
812
839
continue
813
840
}
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 ()},
815
902
}
816
903
817
904
machinePoolList := & unstructured.UnstructuredList {}
@@ -827,16 +914,16 @@ func calculateExpectedWorkerCount(ctx context.Context, c client.Client, unstruct
827
914
Eventually (func () error {
828
915
return c .List (ctx , machinePoolList , byClusterOptions ... )
829
916
}, 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" )
832
919
Expect (err ).ToNot (HaveOccurred ())
833
920
if ! ok {
834
921
continue
835
922
}
836
- expectedWorkerCount += replicas
923
+ expectedMachinePoolWorkerCount += replicas
837
924
}
838
925
839
- return expectedWorkerCount
926
+ return expectedMachinePoolWorkerCount
840
927
}
841
928
842
929
// deleteAllClustersAndWaitInput is the input type for deleteAllClustersAndWait.
0 commit comments