From 35d01e526b25a60fe6a72125e3ad7b3c52363416 Mon Sep 17 00:00:00 2001 From: ushabelgur Date: Tue, 19 Nov 2024 15:19:55 +0530 Subject: [PATCH] fix flapping IPs in NetworkInterface status --- .../controllers/machine_controller.go | 16 ++++++++++------ .../controllers/machine_controller_test.go | 7 ++++--- 2 files changed, 14 insertions(+), 9 deletions(-) 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..5121fc28e 100644 --- a/poollet/machinepoollet/controllers/machine_controller_test.go +++ b/poollet/machinepoollet/controllers/machine_controller_test.go @@ -301,13 +301,14 @@ 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")), + Eventually(Object(nic)).Should(HaveField("Spec.ProviderID", Equal("primary-handle"))) + + 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{