Skip to content

Commit 975a4d1

Browse files
authored
Fix test flake caused by race condition (#1522)
Signed-off-by: Felix Riegger <felix.riegger@sap.com>
1 parent 1bbae66 commit 975a4d1

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

internal/controllers/compute/machinepool_lifecycle_controller_test.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ func startReadyConditionRenewer(pool *computev1alpha1.MachinePool, status corev1
261261
done := make(chan struct{})
262262
go func() {
263263
defer close(done)
264+
// Use a private copy so that komega's UpdateStatus (which calls client.Get
265+
// into this object) does not race with the Consistently poller, which also
266+
// calls client.Get into the caller-owned *pool pointer.
267+
localPool := pool.DeepCopy()
264268
ticker := time.NewTicker(50 * time.Millisecond)
265269
defer ticker.Stop()
266270
counter := 0
@@ -271,8 +275,8 @@ func startReadyConditionRenewer(pool *computev1alpha1.MachinePool, status corev1
271275
case <-ticker.C:
272276
counter++
273277
c := counter
274-
_ = UpdateStatus(pool, func() {
275-
pool.Status.Conditions = computev1alpha1.SetMachinePoolCondition(pool.Status.Conditions, computev1alpha1.MachinePoolCondition{
278+
_ = UpdateStatus(localPool, func() {
279+
localPool.Status.Conditions = computev1alpha1.SetMachinePoolCondition(localPool.Status.Conditions, computev1alpha1.MachinePoolCondition{
276280
Type: computev1alpha1.MachinePoolReady,
277281
Status: status,
278282
Reason: "MachinePoolReadyChanged",
@@ -294,15 +298,19 @@ func startLeaseRenewer(lease *coordinationv1.Lease) func(SpecContext) {
294298
done := make(chan struct{})
295299
go func() {
296300
defer close(done)
301+
// Use a private copy so that komega's Update (which calls client.Get
302+
// into this object) does not race with any concurrent reader of the
303+
// caller-owned *lease pointer.
304+
localLease := lease.DeepCopy()
297305
ticker := time.NewTicker(50 * time.Millisecond)
298306
defer ticker.Stop()
299307
for {
300308
select {
301309
case <-stopCh:
302310
return
303311
case <-ticker.C:
304-
_ = Update(lease, func() {
305-
lease.Spec.RenewTime = ptr.To(metav1.NowMicro())
312+
_ = Update(localLease, func() {
313+
localLease.Spec.RenewTime = ptr.To(metav1.NowMicro())
306314
})()
307315
}
308316
}

0 commit comments

Comments
 (0)