@@ -64,8 +64,8 @@ type AutoscalingFromZeroSpecInput struct {
6464}
6565
6666// AutoscalingFromZeroSpec implements a test that verifies autoscaling from zero functionality.
67- // It creates a MachineDeployment scaled to 0 , deploys Cluster Autoscaler, triggers scale-up
68- // with a workload, and verifies machines can scale from 0 to 1+.
67+ // It adds autoscaler annotations to an existing MachineDeployment , deploys Cluster Autoscaler,
68+ // triggers scale-up with a workload, and verifies machines can scale from 0 to 1+.
6969func AutoscalingFromZeroSpec (ctx context.Context , inputGetter func () AutoscalingFromZeroSpecInput ) {
7070 input := inputGetter ()
7171 Expect (input .BootstrapClusterProxy ).NotTo (BeNil (), "Invalid argument. input.BootstrapClusterProxy can't be nil" )
@@ -91,7 +91,7 @@ func AutoscalingFromZeroSpec(ctx context.Context, inputGetter func() Autoscaling
9191
9292 // Step 1: Add autoscaler annotations to existing MachineDeployment
9393 By ("Adding autoscaler annotations to existing MachineDeployment" )
94- autoscaleMD = addAutoscalerAnnotationsToMachineDeployment (ctx , mgmtClient , input .Namespace .Name , input .ClusterName )
94+ autoscaleMD = addAutoscalerAnnotationsToMachineDeployment (ctx , mgmtClient , input .Namespace .Name , input .ClusterName , input . WaitIntervals )
9595
9696 // Step 2: Wait for MachineDeployment to stabilize at 0 replicas
9797 By ("Waiting for MachineDeployment to stabilize at 0 replicas" )
@@ -135,12 +135,22 @@ func AutoscalingFromZeroSpec(ctx context.Context, inputGetter func() Autoscaling
135135
136136 // Step 6: Create nginx deployment to trigger scale-up in workload cluster
137137 By ("Creating nginx deployment to trigger scale-up" )
138- _ , err = deploymentBuilder .Create ("nginx:1.21" , "autoscale-trigger" , corev1 .NamespaceDefault ).
138+ deployment , err : = deploymentBuilder .Create ("nginx:1.21" , "autoscale-trigger" , corev1 .NamespaceDefault ).
139139 SetReplicas (2 ).
140140 SetResourceRequests ("100m" , "128Mi" ).
141141 Deploy (ctx , workloadClientset )
142142 Expect (err ).NotTo (HaveOccurred ())
143143
144+ // Clean up the deployment after the test
145+ defer func () {
146+ By ("Cleaning up nginx deployment" )
147+ err := workloadClientset .AppsV1 ().Deployments (corev1 .NamespaceDefault ).
148+ Delete (ctx , deployment .Name , metav1.DeleteOptions {})
149+ if err != nil && ! apierrors .IsNotFound (err ) {
150+ Logf ("Failed to delete deployment %s: %v" , deployment .Name , err )
151+ }
152+ }()
153+
144154 // Step 7: Wait for CA to scale MachineDeployment from 0 to 1+
145155 By ("Waiting for Cluster Autoscaler to scale MachineDeployment to 1+" )
146156 Eventually (func (g Gomega ) {
@@ -149,13 +159,26 @@ func AutoscalingFromZeroSpec(ctx context.Context, inputGetter func() Autoscaling
149159 g .Expect (err ).NotTo (HaveOccurred ())
150160 g .Expect (md .Spec .Replicas ).NotTo (BeNil ())
151161 g .Expect (* md .Spec .Replicas ).To (BeNumerically (">=" , 1 ))
162+ g .Expect (md .Status .Replicas ).To (HaveValue (BeNumerically (">=" , 1 )))
163+ g .Expect (md .Status .ReadyReplicas ).To (HaveValue (BeNumerically (">=" , 1 )))
152164 }, input .WaitIntervals ... ).Should (Succeed ())
153165
154- Byf ("Cluster Autoscaler scaled MachineDeployment from 0 to 1+ - scale-up from zero test PASSED!" )
166+ Byf ("Cluster Autoscaler scaled MachineDeployment from 0 to 1+" )
167+
168+ // Step 8: Verify nginx deployment is ready
169+ By ("Verifying nginx deployment is ready in workload cluster" )
170+ Eventually (func (g Gomega ) {
171+ nginxDeployment , err := workloadClientset .AppsV1 ().Deployments (corev1 .NamespaceDefault ).
172+ Get (ctx , deployment .Name , metav1.GetOptions {})
173+ g .Expect (err ).NotTo (HaveOccurred ())
174+ g .Expect (nginxDeployment .Status .ReadyReplicas ).To (Equal (int32 (2 )))
175+ }, input .WaitIntervals ... ).Should (Succeed ())
176+
177+ Byf ("Nginx deployment is ready - scale-up from zero test PASSED!" )
155178}
156179
157180// addAutoscalerAnnotationsToMachineDeployment adds autoscaler annotations to the existing MachineDeployment.
158- func addAutoscalerAnnotationsToMachineDeployment (ctx context.Context , mgmtClient client.Client , namespace , clusterName string ) * clusterv1.MachineDeployment {
181+ func addAutoscalerAnnotationsToMachineDeployment (ctx context.Context , mgmtClient client.Client , namespace , clusterName string , waitIntervals [] interface {} ) * clusterv1.MachineDeployment {
159182 mdList := & clusterv1.MachineDeploymentList {}
160183 err := mgmtClient .List (ctx , mdList ,
161184 client .InNamespace (namespace ),
@@ -164,20 +187,34 @@ func addAutoscalerAnnotationsToMachineDeployment(ctx context.Context, mgmtClient
164187 })
165188 Expect (err ).NotTo (HaveOccurred ())
166189 Expect (mdList .Items ).To (HaveLen (1 ), "Expected exactly one MachineDeployment for cluster %s, found %d" , clusterName , len (mdList .Items ))
167- md := & mdList .Items [0 ]
168190
169- // Add autoscaler annotations
170- if md . Annotations == nil {
171- md . Annotations = make ( map [ string ] string )
191+ mdKey := client. ObjectKey {
192+ Namespace : mdList . Items [ 0 ]. Namespace ,
193+ Name : mdList . Items [ 0 ]. Name ,
172194 }
173- md .Annotations ["cluster.x-k8s.io/cluster-api-autoscaler-node-group-min-size" ] = "0"
174- md .Annotations ["cluster.x-k8s.io/cluster-api-autoscaler-node-group-max-size" ] = "3"
175195
176- // Update the MachineDeployment
177- err = mgmtClient .Update (ctx , md )
178- Expect (err ).NotTo (HaveOccurred ())
196+ var updatedMD * clusterv1.MachineDeployment
197+
198+ Eventually (func (g Gomega ) {
199+ md := & clusterv1.MachineDeployment {}
200+ err := mgmtClient .Get (ctx , mdKey , md )
201+ g .Expect (err ).NotTo (HaveOccurred ())
202+
203+ // Add autoscaler annotations
204+ if md .Annotations == nil {
205+ md .Annotations = make (map [string ]string )
206+ }
207+ md .Annotations ["cluster.x-k8s.io/cluster-api-autoscaler-node-group-min-size" ] = "0"
208+ md .Annotations ["cluster.x-k8s.io/cluster-api-autoscaler-node-group-max-size" ] = "3"
209+
210+ // Update the MachineDeployment
211+ err = mgmtClient .Update (ctx , md )
212+ g .Expect (err ).NotTo (HaveOccurred ())
213+
214+ updatedMD = md
215+ }, waitIntervals ... ).Should (Succeed ())
179216
180- return md
217+ return updatedMD
181218}
182219
183220// deployClusterAutoscalerRBAC deploys the RBAC resources for Cluster Autoscaler to management cluster.
0 commit comments