From 35227ee9f7544f6ef31503cba7476b4f9c819bed Mon Sep 17 00:00:00 2001 From: Igor Velichkovich Date: Fri, 3 Oct 2025 15:19:21 -0700 Subject: [PATCH 1/2] flake use apireader to directly talk to apiserver --- .../machine/machine_controller_status_test.go | 16 ++++++++-------- internal/test/envtest/environment.go | 7 +++++++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/internal/controllers/machine/machine_controller_status_test.go b/internal/controllers/machine/machine_controller_status_test.go index ac1e7f2c2324..33b2fdddf854 100644 --- a/internal/controllers/machine/machine_controller_status_test.go +++ b/internal/controllers/machine/machine_controller_status_test.go @@ -2085,7 +2085,7 @@ func TestReconcileMachinePhases(t *testing.T) { // Wait until InfraMachine has the ownerReference. g.Eventually(func(g Gomega) bool { - if err := env.Get(ctx, client.ObjectKeyFromObject(infraMachine), infraMachine); err != nil { + if err := env.DirectApiServerGet(ctx, client.ObjectKeyFromObject(infraMachine), infraMachine); err != nil { return false } g.Expect(infraMachine.GetOwnerReferences()).To(HaveLen(1)) @@ -2127,7 +2127,7 @@ func TestReconcileMachinePhases(t *testing.T) { // Wait until Machine was reconciled. g.Eventually(func(g Gomega) bool { - if err := env.Get(ctx, client.ObjectKeyFromObject(machine), machine); err != nil { + if err := env.DirectApiServerGet(ctx, client.ObjectKeyFromObject(machine), machine); err != nil { return false } g.Expect(machine.Status.GetTypedPhase()).To(Equal(clusterv1.MachinePhasePending)) @@ -2182,7 +2182,7 @@ func TestReconcileMachinePhases(t *testing.T) { // Wait until Machine was reconciled. g.Eventually(func(g Gomega) bool { - if err := env.Get(ctx, client.ObjectKeyFromObject(machine), machine); err != nil { + if err := env.DirectApiServerGet(ctx, client.ObjectKeyFromObject(machine), machine); err != nil { return false } g.Expect(machine.Status.GetTypedPhase()).To(Equal(clusterv1.MachinePhaseProvisioning)) @@ -2270,7 +2270,7 @@ func TestReconcileMachinePhases(t *testing.T) { // Wait until Machine was reconciled. g.Eventually(func(g Gomega) bool { - if err := env.Get(ctx, client.ObjectKeyFromObject(machine), machine); err != nil { + if err := env.DirectApiServerGet(ctx, client.ObjectKeyFromObject(machine), machine); err != nil { return false } g.Expect(machine.Status.Addresses).To(HaveLen(2)) @@ -2349,7 +2349,7 @@ func TestReconcileMachinePhases(t *testing.T) { // Wait until Machine was reconciled. g.Eventually(func(g Gomega) bool { - if err := env.Get(ctx, client.ObjectKeyFromObject(machine), machine); err != nil { + if err := env.DirectApiServerGet(ctx, client.ObjectKeyFromObject(machine), machine); err != nil { return false } g.Expect(machine.Status.GetTypedPhase()).To(Equal(clusterv1.MachinePhaseRunning)) @@ -2427,7 +2427,7 @@ func TestReconcileMachinePhases(t *testing.T) { // Wait until Machine was reconciled. g.Eventually(func(g Gomega) bool { - if err := env.Get(ctx, client.ObjectKeyFromObject(machine), machine); err != nil { + if err := env.DirectApiServerGet(ctx, client.ObjectKeyFromObject(machine), machine); err != nil { return false } g.Expect(machine.Status.GetTypedPhase()).To(Equal(clusterv1.MachinePhaseRunning)) @@ -2489,7 +2489,7 @@ func TestReconcileMachinePhases(t *testing.T) { // Wait until Machine was reconciled. g.Eventually(func(g Gomega) bool { - if err := env.Get(ctx, client.ObjectKeyFromObject(machine), machine); err != nil { + if err := env.DirectApiServerGet(ctx, client.ObjectKeyFromObject(machine), machine); err != nil { return false } g.Expect(machine.Status.GetTypedPhase()).To(Equal(clusterv1.MachinePhaseProvisioned)) @@ -2582,7 +2582,7 @@ func TestReconcileMachinePhases(t *testing.T) { // Wait until Machine was reconciled. g.Eventually(func(g Gomega) bool { - if err := env.Get(ctx, client.ObjectKeyFromObject(machine), machine); err != nil { + if err := env.DirectApiServerGet(ctx, client.ObjectKeyFromObject(machine), machine); err != nil { return false } g.Expect(machine.Status.GetTypedPhase()).To(Equal(clusterv1.MachinePhaseDeleting)) diff --git a/internal/test/envtest/environment.go b/internal/test/envtest/environment.go index 48baf781b846..a1121f54df4a 100644 --- a/internal/test/envtest/environment.go +++ b/internal/test/envtest/environment.go @@ -565,6 +565,13 @@ func (e *Environment) PatchAndWait(ctx context.Context, obj client.Object, opts return nil } +// DirectApiServerGet gets an object directly from apiserver bypassing informer caches.. +// +// NOTE: Bypassing cache helps in preventing test flakes due to the cache sync delays but should only be used in validation steps of testing. +func (e *Environment) DirectApiServerGet(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { + return e.Manager.GetAPIReader().Get(ctx, key, obj, opts...) +} + // CreateNamespace creates a new namespace with a generated name. func (e *Environment) CreateNamespace(ctx context.Context, generateName string) (*corev1.Namespace, error) { ns := &corev1.Namespace{ From c5abbe503cfb77c7c4361dd1aff931140bbb8a04 Mon Sep 17 00:00:00 2001 From: Igor Velichkovich Date: Mon, 6 Oct 2025 15:45:23 -0700 Subject: [PATCH 2/2] lint --- .../machine/machine_controller_status_test.go | 16 ++++++++-------- internal/test/envtest/environment.go | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/internal/controllers/machine/machine_controller_status_test.go b/internal/controllers/machine/machine_controller_status_test.go index 33b2fdddf854..e0e68431c597 100644 --- a/internal/controllers/machine/machine_controller_status_test.go +++ b/internal/controllers/machine/machine_controller_status_test.go @@ -2085,7 +2085,7 @@ func TestReconcileMachinePhases(t *testing.T) { // Wait until InfraMachine has the ownerReference. g.Eventually(func(g Gomega) bool { - if err := env.DirectApiServerGet(ctx, client.ObjectKeyFromObject(infraMachine), infraMachine); err != nil { + if err := env.DirectAPIServerGet(ctx, client.ObjectKeyFromObject(infraMachine), infraMachine); err != nil { return false } g.Expect(infraMachine.GetOwnerReferences()).To(HaveLen(1)) @@ -2127,7 +2127,7 @@ func TestReconcileMachinePhases(t *testing.T) { // Wait until Machine was reconciled. g.Eventually(func(g Gomega) bool { - if err := env.DirectApiServerGet(ctx, client.ObjectKeyFromObject(machine), machine); err != nil { + if err := env.DirectAPIServerGet(ctx, client.ObjectKeyFromObject(machine), machine); err != nil { return false } g.Expect(machine.Status.GetTypedPhase()).To(Equal(clusterv1.MachinePhasePending)) @@ -2182,7 +2182,7 @@ func TestReconcileMachinePhases(t *testing.T) { // Wait until Machine was reconciled. g.Eventually(func(g Gomega) bool { - if err := env.DirectApiServerGet(ctx, client.ObjectKeyFromObject(machine), machine); err != nil { + if err := env.DirectAPIServerGet(ctx, client.ObjectKeyFromObject(machine), machine); err != nil { return false } g.Expect(machine.Status.GetTypedPhase()).To(Equal(clusterv1.MachinePhaseProvisioning)) @@ -2270,7 +2270,7 @@ func TestReconcileMachinePhases(t *testing.T) { // Wait until Machine was reconciled. g.Eventually(func(g Gomega) bool { - if err := env.DirectApiServerGet(ctx, client.ObjectKeyFromObject(machine), machine); err != nil { + if err := env.DirectAPIServerGet(ctx, client.ObjectKeyFromObject(machine), machine); err != nil { return false } g.Expect(machine.Status.Addresses).To(HaveLen(2)) @@ -2349,7 +2349,7 @@ func TestReconcileMachinePhases(t *testing.T) { // Wait until Machine was reconciled. g.Eventually(func(g Gomega) bool { - if err := env.DirectApiServerGet(ctx, client.ObjectKeyFromObject(machine), machine); err != nil { + if err := env.DirectAPIServerGet(ctx, client.ObjectKeyFromObject(machine), machine); err != nil { return false } g.Expect(machine.Status.GetTypedPhase()).To(Equal(clusterv1.MachinePhaseRunning)) @@ -2427,7 +2427,7 @@ func TestReconcileMachinePhases(t *testing.T) { // Wait until Machine was reconciled. g.Eventually(func(g Gomega) bool { - if err := env.DirectApiServerGet(ctx, client.ObjectKeyFromObject(machine), machine); err != nil { + if err := env.DirectAPIServerGet(ctx, client.ObjectKeyFromObject(machine), machine); err != nil { return false } g.Expect(machine.Status.GetTypedPhase()).To(Equal(clusterv1.MachinePhaseRunning)) @@ -2489,7 +2489,7 @@ func TestReconcileMachinePhases(t *testing.T) { // Wait until Machine was reconciled. g.Eventually(func(g Gomega) bool { - if err := env.DirectApiServerGet(ctx, client.ObjectKeyFromObject(machine), machine); err != nil { + if err := env.DirectAPIServerGet(ctx, client.ObjectKeyFromObject(machine), machine); err != nil { return false } g.Expect(machine.Status.GetTypedPhase()).To(Equal(clusterv1.MachinePhaseProvisioned)) @@ -2582,7 +2582,7 @@ func TestReconcileMachinePhases(t *testing.T) { // Wait until Machine was reconciled. g.Eventually(func(g Gomega) bool { - if err := env.DirectApiServerGet(ctx, client.ObjectKeyFromObject(machine), machine); err != nil { + if err := env.DirectAPIServerGet(ctx, client.ObjectKeyFromObject(machine), machine); err != nil { return false } g.Expect(machine.Status.GetTypedPhase()).To(Equal(clusterv1.MachinePhaseDeleting)) diff --git a/internal/test/envtest/environment.go b/internal/test/envtest/environment.go index a1121f54df4a..c01652d2a769 100644 --- a/internal/test/envtest/environment.go +++ b/internal/test/envtest/environment.go @@ -565,10 +565,10 @@ func (e *Environment) PatchAndWait(ctx context.Context, obj client.Object, opts return nil } -// DirectApiServerGet gets an object directly from apiserver bypassing informer caches.. +// DirectAPIServerGet gets an object directly from apiserver bypassing informer caches.. // // NOTE: Bypassing cache helps in preventing test flakes due to the cache sync delays but should only be used in validation steps of testing. -func (e *Environment) DirectApiServerGet(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { +func (e *Environment) DirectAPIServerGet(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { return e.Manager.GetAPIReader().Get(ctx, key, obj, opts...) }