From 9c991b535b722c937675b8ae3f873976e1036fd6 Mon Sep 17 00:00:00 2001 From: ushabelgur Date: Fri, 22 Nov 2024 16:09:29 +0530 Subject: [PATCH] Fix flapping IPs in NetworkInterface status (#1163) * fix flapping IPs in NetworkInterface status * incorporating review comments * incorporating review comments --- .../controllers/controllers_suite_test.go | 2 +- .../controllers/machine_controller.go | 16 ++++++++++------ .../controllers/machine_controller_test.go | 8 +++++++- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/poollet/machinepoollet/controllers/controllers_suite_test.go b/poollet/machinepoollet/controllers/controllers_suite_test.go index e50f67396..2a521a13f 100644 --- a/poollet/machinepoollet/controllers/controllers_suite_test.go +++ b/poollet/machinepoollet/controllers/controllers_suite_test.go @@ -54,7 +54,7 @@ var ( const ( eventuallyTimeout = 3 * time.Second pollingInterval = 50 * time.Millisecond - consistentlyDuration = 1 * time.Second + consistentlyDuration = 3 * time.Second apiServiceTimeout = 5 * time.Minute controllerManagerService = "controller-manager" diff --git a/poollet/machinepoollet/controllers/machine_controller.go b/poollet/machinepoollet/controllers/machine_controller.go index ee6c778cc..fbb9a8f0c 100644 --- a/poollet/machinepoollet/controllers/machine_controller.go +++ b/poollet/machinepoollet/controllers/machine_controller.go @@ -7,6 +7,7 @@ import ( "context" "errors" "fmt" + "slices" "strconv" "time" @@ -542,17 +543,20 @@ func (r *MachineReconciler) updateNetworkInterfaceStatusFromIRIStatus( return nil // nothing to do } - nicBase := nic.DeepCopy() - nic.Status.IPs = commonv1alpha1.MustParseIPs(iriNicStatus.Ips...) - nic.Status.LastStateTransitionTime = &metav1.Time{Time: time.Now()} nicState, ok := iriNetworkInterfaceStateToNetworkInterfaceState[iriNicStatus.State] if !ok { return fmt.Errorf("encountered unknown network interface state %s", iriNicStatus.State) } - nic.Status.State = nicState - if err := r.Status().Patch(ctx, nic, client.MergeFrom(nicBase)); err != nil { - return fmt.Errorf("failed to patch network interface status: %w", err) + nicBase := nic.DeepCopy() + if nicBase.Status.State != nicState || !slices.Equal(nic.Status.IPs, commonv1alpha1.MustParseIPs(iriNicStatus.Ips...)) { + nic.Status.LastStateTransitionTime = &metav1.Time{Time: time.Now()} + nic.Status.IPs = commonv1alpha1.MustParseIPs(iriNicStatus.Ips...) + nic.Status.State = nicState + + if err := r.Status().Patch(ctx, nic, client.MergeFrom(nicBase)); err != nil { + return fmt.Errorf("failed to patch network interface status: %w", err) + } } return nil diff --git a/poollet/machinepoollet/controllers/machine_controller_test.go b/poollet/machinepoollet/controllers/machine_controller_test.go index ce3439161..faa87cdaf 100644 --- a/poollet/machinepoollet/controllers/machine_controller_test.go +++ b/poollet/machinepoollet/controllers/machine_controller_test.go @@ -300,7 +300,6 @@ var _ = Describe("MachineController", func() { } srv.SetMachines([]*testingmachine.FakeMachine{iriMachine}) - By("waiting for the ironcore network interface to have a provider id set") Eventually(Object(nic)).Should(And( HaveField("Spec.ProviderID", Equal("primary-handle")), HaveField("Status", MatchFields(IgnoreExtras, Fields{ @@ -309,6 +308,13 @@ var _ = Describe("MachineController", func() { })), )) + Consistently(Object(nic)).Should( + HaveField("Status", MatchFields(IgnoreExtras, Fields{ + "State": Equal(networkingv1alpha1.NetworkInterfaceStateAvailable), + "IPs": ContainElement(commonv1alpha1.MustParseIP("10.0.0.1")), + })), + ) + By("ensuring the ironcore machine status networkInterfaces to have correct NetworkInterfaceRef") Eventually(Object(machine)).Should(HaveField("Status.NetworkInterfaces", ConsistOf(MatchFields(IgnoreExtras, Fields{ "Name": Equal("primary"),