From adbbbc28410f073be7f54b6ab77cbf303024cce7 Mon Sep 17 00:00:00 2001 From: Enrique Llorente Pastora Date: Tue, 1 Mar 2022 09:02:03 +0100 Subject: [PATCH] [release-0.64] capture: Get currentState from cli (#1000) Calculating the captured state with the NNS is problematic since there are some latencies between nmstatectl set and NNS being updated, so it could happend that next NNCP is calculated with inaccurate network configuration. This change read the currentState from nmstatectl to get accurate data. Cherry picked from (#998) Signed-off-by: Quique Llorente --- ...nodenetworkconfigurationpolicy_controller.go | 17 +++++++++-------- ...etworkconfigurationpolicy_controller_test.go | 1 + 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/controllers/handler/nodenetworkconfigurationpolicy_controller.go b/controllers/handler/nodenetworkconfigurationpolicy_controller.go index e8948df286..e752b94c53 100644 --- a/controllers/handler/nodenetworkconfigurationpolicy_controller.go +++ b/controllers/handler/nodenetworkconfigurationpolicy_controller.go @@ -52,6 +52,7 @@ import ( "github.com/nmstate/kubernetes-nmstate/pkg/environment" nmstate "github.com/nmstate/kubernetes-nmstate/pkg/helper" "github.com/nmstate/kubernetes-nmstate/pkg/nmpolicy" + "github.com/nmstate/kubernetes-nmstate/pkg/nmstatectl" "github.com/nmstate/kubernetes-nmstate/pkg/node" "github.com/nmstate/kubernetes-nmstate/pkg/policyconditions" "github.com/nmstate/kubernetes-nmstate/pkg/probe" @@ -90,6 +91,7 @@ var ( return false }, } + nmstatectlShowFn = nmstatectl.Show ) // NodeNetworkConfigurationPolicyReconciler reconciles a NodeNetworkConfigurationPolicy object @@ -165,12 +167,7 @@ func (r *NodeNetworkConfigurationPolicyReconciler) Reconcile(ctx context.Context enactmentConditions := enactmentconditions.New(r.APIClient, nmstateapi.EnactmentKey(nodeName, instance.Name)) - nns, err := r.readNNS(nodeName) - if err != nil { - return ctrl.Result{}, err - } - - err = r.fillInEnactmentStatus(nns, *instance, *enactmentInstance, enactmentConditions) + err = r.fillInEnactmentStatus(*instance, *enactmentInstance, enactmentConditions) if err != nil { log.Error(err, "failed filling in the NNCE status") if apierrors.IsNotFound(err) { @@ -308,11 +305,15 @@ func (r *NodeNetworkConfigurationPolicyReconciler) initializeEnactment(policy nm return &enactment, nil } -func (r *NodeNetworkConfigurationPolicyReconciler) fillInEnactmentStatus(nns *nmstatev1beta1.NodeNetworkState, +func (r *NodeNetworkConfigurationPolicyReconciler) fillInEnactmentStatus( policy nmstatev1.NodeNetworkConfigurationPolicy, enactment nmstatev1beta1.NodeNetworkConfigurationEnactment, enactmentConditions enactmentconditions.EnactmentConditions) error { - capturedStates, desiredStateMetaInfo, generatedDesiredState, err := nmpolicy.GenerateState(policy.Spec.DesiredState, policy.Spec, nns.Status.CurrentState, enactment.Status.CapturedStates) + currentState, err := nmstatectlShowFn() + if err != nil { + return err + } + capturedStates, desiredStateMetaInfo, generatedDesiredState, err := nmpolicy.GenerateState(policy.Spec.DesiredState, policy.Spec, nmstateapi.NewState(currentState), enactment.Status.CapturedStates) if err != nil { err2 := enactmentstatus.Update(r.APIClient, nmstateapi.EnactmentKey(nodeName, policy.Name), func(status *nmstateapi.NodeNetworkConfigurationEnactmentStatus) { status.PolicyGeneration = policy.Generation diff --git a/controllers/handler/nodenetworkconfigurationpolicy_controller_test.go b/controllers/handler/nodenetworkconfigurationpolicy_controller_test.go index 48a792e2c0..5249d79ef7 100644 --- a/controllers/handler/nodenetworkconfigurationpolicy_controller_test.go +++ b/controllers/handler/nodenetworkconfigurationpolicy_controller_test.go @@ -96,6 +96,7 @@ var _ = Describe("NodeNetworkConfigurationPolicy controller predicates", func() } DescribeTable("when claimNodeRunningUpdate is called and", func(c incrementUnavailableNodeCountCase) { + nmstatectlShowFn = func() (string, error) { return "", nil } reconciler := NodeNetworkConfigurationPolicyReconciler{} s := scheme.Scheme s.AddKnownTypes(nmstatev1beta1.GroupVersion,