From 3519d077cae18056dc51662acf61e7fbfa57acbf Mon Sep 17 00:00:00 2001 From: Daniel Fan Date: Thu, 20 Jul 2023 17:14:29 -0400 Subject: [PATCH] Compare updated opreq status with latest opreq (#950) Signed-off-by: Daniel Fan --- .../operandrequest/operandrequest_controller.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/controllers/operandrequest/operandrequest_controller.go b/controllers/operandrequest/operandrequest_controller.go index 6c5957eb..bc8c0657 100644 --- a/controllers/operandrequest/operandrequest_controller.go +++ b/controllers/operandrequest/operandrequest_controller.go @@ -77,10 +77,17 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re // Always attempt to patch the status after each reconciliation. defer func() { - if reflect.DeepEqual(originalInstance.Status, requestInstance.Status) { + // get the latest instance from the server and check if the status has changed + existingInstance := &operatorv1alpha1.OperandRequest{} + if err := r.Client.Get(ctx, req.NamespacedName, existingInstance); err != nil { + // Error reading the latest object - requeue the request. + reconcileErr = utilerrors.NewAggregate([]error{reconcileErr, fmt.Errorf("error while get latest OperandRequest.Status from server: %v", err)}) + } + + if reflect.DeepEqual(existingInstance.Status, requestInstance.Status) { return } - if err := r.Client.Status().Patch(ctx, requestInstance, client.MergeFrom(originalInstance)); err != nil && !apierrors.IsNotFound(err) { + if err := r.Client.Status().Patch(ctx, requestInstance, client.MergeFrom(existingInstance)); err != nil && !apierrors.IsNotFound(err) { reconcileErr = utilerrors.NewAggregate([]error{reconcileErr, fmt.Errorf("error while patching OperandRequest.Status: %v", err)}) } }()