Skip to content

Commit 7eb3669

Browse files
authored
Merge pull request #12613 from sbueringer/pr-fix-autoscaler-test-flake
🐛 Fix autoscaler e2e test flake
2 parents c0bd847 + 5ffee12 commit 7eb3669

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

test/e2e/autoscaler.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func AutoscalerSpec(ctx context.Context, inputGetter func() AutoscalerSpecInput)
203203
By("Checking the MachineDeployment is scaled up")
204204
mdScaledUpReplicas := mdOriginalReplicas + 1
205205
framework.AssertMachineDeploymentReplicas(ctx, framework.AssertMachineDeploymentReplicasInput{
206-
Getter: input.BootstrapClusterProxy.GetClient(),
206+
GetLister: input.BootstrapClusterProxy.GetClient(),
207207
MachineDeployment: clusterResources.MachineDeployments[0],
208208
Replicas: mdScaledUpReplicas,
209209
WaitForMachineDeployment: input.E2EConfig.GetIntervals(specName, "wait-autoscaler"),
@@ -240,7 +240,7 @@ func AutoscalerSpec(ctx context.Context, inputGetter func() AutoscalerSpecInput)
240240
// Since we scaled up the MachineDeployment manually and the workload has not changed auto scaler
241241
// should detect that there are unneeded nodes and scale down the MachineDeployment.
242242
framework.AssertMachineDeploymentReplicas(ctx, framework.AssertMachineDeploymentReplicasInput{
243-
Getter: input.BootstrapClusterProxy.GetClient(),
243+
GetLister: input.BootstrapClusterProxy.GetClient(),
244244
MachineDeployment: clusterResources.MachineDeployments[0],
245245
Replicas: mdScaledUpReplicas,
246246
WaitForMachineDeployment: input.E2EConfig.GetIntervals(specName, "wait-controllers"),
@@ -257,21 +257,21 @@ func AutoscalerSpec(ctx context.Context, inputGetter func() AutoscalerSpecInput)
257257
WaitForAnnotationsToBeAdded: input.E2EConfig.GetIntervals(specName, "wait-autoscaler"),
258258
})
259259

260-
By("Scaling the MachineDeployment scale up deployment to zero")
260+
By("Scaling the MachineDeployment scale up deployment to 0")
261261
framework.ScaleScaleUpDeploymentAndWait(ctx, framework.ScaleScaleUpDeploymentAndWaitInput{
262262
ClusterProxy: workloadClusterProxy,
263263
Replicas: 0,
264264
}, input.E2EConfig.GetIntervals(specName, "wait-autoscaler")...)
265265

266266
By("Checking the MachineDeployment finished scaling down to zero")
267267
framework.AssertMachineDeploymentReplicas(ctx, framework.AssertMachineDeploymentReplicasInput{
268-
Getter: input.BootstrapClusterProxy.GetClient(),
268+
GetLister: input.BootstrapClusterProxy.GetClient(),
269269
MachineDeployment: clusterResources.MachineDeployments[0],
270270
Replicas: 0,
271271
WaitForMachineDeployment: input.E2EConfig.GetIntervals(specName, "wait-controllers"),
272272
})
273273

274-
By("Scaling the MachineDeployment scale up deployment to 1")
274+
Byf("Scaling the MachineDeployment scale up deployment to %d", mpOriginalReplicas+1)
275275
framework.ScaleScaleUpDeploymentAndWait(ctx, framework.ScaleScaleUpDeploymentAndWaitInput{
276276
ClusterProxy: workloadClusterProxy,
277277
// We need to sum up the expected number of MachineDeployment replicas and the current
@@ -281,7 +281,7 @@ func AutoscalerSpec(ctx context.Context, inputGetter func() AutoscalerSpecInput)
281281

282282
By("Checking the MachineDeployment finished scaling up")
283283
framework.AssertMachineDeploymentReplicas(ctx, framework.AssertMachineDeploymentReplicasInput{
284-
Getter: input.BootstrapClusterProxy.GetClient(),
284+
GetLister: input.BootstrapClusterProxy.GetClient(),
285285
MachineDeployment: clusterResources.MachineDeployments[0],
286286
Replicas: 1,
287287
WaitForMachineDeployment: input.E2EConfig.GetIntervals(specName, "wait-controllers"),

test/framework/autoscaler_helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func AddScaleUpDeploymentAndWait(ctx context.Context, input AddScaleUpDeployment
221221
},
222222
}
223223

224-
By("Create scale up deployment")
224+
byf("Create scale up deployment (%d replicas)", replicas)
225225
Expect(input.ClusterProxy.GetClient().Create(ctx, scaleUpDeployment)).To(Succeed(), "failed to create the scale up pod")
226226

227227
By("Wait for the scale up deployment to become ready (this implies machines to be created)")

test/framework/machinedeployment_helpers.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -777,15 +777,15 @@ func ScaleAndWaitMachineDeploymentTopologyV1Beta1(ctx context.Context, input Sca
777777
}
778778

779779
type AssertMachineDeploymentReplicasInput struct {
780-
Getter Getter
780+
GetLister GetLister
781781
MachineDeployment *clusterv1.MachineDeployment
782782
Replicas int32
783783
WaitForMachineDeployment []interface{}
784784
}
785785

786786
func AssertMachineDeploymentReplicas(ctx context.Context, input AssertMachineDeploymentReplicasInput) {
787787
Expect(ctx).NotTo(BeNil(), "ctx is required for AssertMachineDeploymentReplicas")
788-
Expect(input.Getter).ToNot(BeNil(), "Invalid argument. input.Getter can't be nil when calling AssertMachineDeploymentReplicas")
788+
Expect(input.GetLister).ToNot(BeNil(), "Invalid argument. input.GetLister can't be nil when calling AssertMachineDeploymentReplicas")
789789
Expect(input.MachineDeployment).ToNot(BeNil(), "Invalid argument. input.MachineDeployment can't be nil when calling AssertMachineDeploymentReplicas")
790790

791791
Eventually(func(g Gomega) {
@@ -795,9 +795,23 @@ func AssertMachineDeploymentReplicas(ctx context.Context, input AssertMachineDep
795795
Namespace: input.MachineDeployment.Namespace,
796796
Name: input.MachineDeployment.Name,
797797
}
798-
g.Expect(input.Getter.Get(ctx, key, md)).To(Succeed(), fmt.Sprintf("failed to get MachineDeployment %s", klog.KObj(input.MachineDeployment)))
798+
g.Expect(input.GetLister.Get(ctx, key, md)).To(Succeed(), fmt.Sprintf("failed to get MachineDeployment %s", klog.KObj(input.MachineDeployment)))
799799
g.Expect(md.Spec.Replicas).Should(Not(BeNil()), fmt.Sprintf("MachineDeployment %s replicas should not be nil", klog.KObj(md)))
800800
g.Expect(*md.Spec.Replicas).Should(Equal(input.Replicas), fmt.Sprintf("MachineDeployment %s replicas should match expected replicas", klog.KObj(md)))
801801
g.Expect(ptr.Deref(md.Status.Replicas, 0)).Should(Equal(input.Replicas), fmt.Sprintf("MachineDeployment %s status.replicas should match expected replicas", klog.KObj(md)))
802+
803+
// Check all replicas have a NodeRef (i.e. they have a corresponding Node).
804+
selectorMap, err := metav1.LabelSelectorAsMap(&input.MachineDeployment.Spec.Selector)
805+
g.Expect(err).ToNot(HaveOccurred())
806+
machines := &clusterv1.MachineList{}
807+
err = input.GetLister.List(ctx, machines, client.InNamespace(input.MachineDeployment.Namespace), client.MatchingLabels(selectorMap))
808+
g.Expect(err).ToNot(HaveOccurred())
809+
count := 0
810+
for _, machine := range machines.Items {
811+
if machine.Status.NodeRef.IsDefined() {
812+
count++
813+
}
814+
}
815+
g.Expect(count).To(Equal(int(input.Replicas)))
802816
}, input.WaitForMachineDeployment...).Should(Succeed())
803817
}

0 commit comments

Comments
 (0)