From b8a4a51dbff0854eea941a56f68bb222ee5f64c2 Mon Sep 17 00:00:00 2001 From: Burak Sekili Date: Tue, 18 Jul 2023 09:37:37 +0300 Subject: [PATCH] [TT-9420] Move status update out of CreateOrUpdate to prevent conflicts (#642) * Fetch the resource before updating it Signed-off-by: Burak Sekili * copy object onto new one Signed-off-by: Burak Sekili * Use Patch instead of update requests Signed-off-by: Burak Sekili * Bring Status check to back Signed-off-by: Burak Sekili * Move status update out of CreateOrUpdate Signed-off-by: Burak Sekili --------- Signed-off-by: Burak Sekili --- controllers/apidefinition_controller.go | 65 ++++++++++--------------- 1 file changed, 27 insertions(+), 38 deletions(-) diff --git a/controllers/apidefinition_controller.go b/controllers/apidefinition_controller.go index e22fdd506..590ac14a1 100644 --- a/controllers/apidefinition_controller.go +++ b/controllers/apidefinition_controller.go @@ -222,11 +222,36 @@ func (r *ApiDefinitionReconciler) Reconcile(ctx context.Context, req ctrl.Reques // Reconciler must return the error observed by CreateOrUpdate() function since the mutator given to CreateOrUpdate // returns special custom error such as ErrMultipleLinkSubGraph. errK8s := retry.RetryOnConflict(retry.DefaultRetry, func() error { + namespace := upstreamRequestStruct.Namespace + target := model.Target{Namespace: &namespace, Name: upstreamRequestStruct.Name} + + if desired.Status.ApiID == "" { + apiId := "" + if upstreamRequestStruct.Spec.APIID != nil { + apiId = *upstreamRequestStruct.Spec.APIID + } + + apiOnTyk, _ := klient.Universal.Api().Get(ctx, apiId) //nolint:errcheck + + return r.updateStatus( + ctx, + desired.Namespace, + target, + true, + func(status *tykv1alpha1.ApiDefinitionStatus) { + status.ApiID = apiId + status.LatestTykSpecHash = calculateHash(apiOnTyk) + status.LatestCRDSpecHash = calculateHash(upstreamRequestStruct.Spec) + status.LatestTransaction = *transactionInfo + }, + ) + } + return r.updateStatus( ctx, desired.Namespace, - model.Target{Namespace: &desired.Namespace, Name: desired.Name}, - false, + target, + true, func(status *tykv1alpha1.ApiDefinitionStatus) { status.LatestTransaction = *transactionInfo }, ) }) @@ -447,37 +472,6 @@ func (r *ApiDefinitionReconciler) create(ctx context.Context, desired *tykv1alph return err } - apiOnTyk, _ := klient.Universal.Api().Get(ctx, *desired.Spec.APIID) //nolint:errcheck - - namespace := desired.Namespace - target := model.Target{Namespace: &namespace, Name: desired.Name} - - err = r.updateStatus( - ctx, - desired.Namespace, - target, - false, - func(status *tykv1alpha1.ApiDefinitionStatus) { - status.ApiID = *desired.Spec.APIID - status.LatestTykSpecHash = calculateHash(apiOnTyk) - status.LatestCRDSpecHash = calculateHash(desired.Spec) - status.LatestTransaction = tykv1alpha1.TransactionInfo{ - Time: metav1.Now(), - Status: tykv1alpha1.Successful, - Error: "", - } - }, - ) - if err != nil { - r.Log.Error( - err, - "Failed to update Status ID", - "ApiDefinition", client.ObjectKeyFromObject(desired).String(), - ) - - return err - } - return nil } @@ -539,11 +533,6 @@ func (r *ApiDefinitionReconciler) update(ctx context.Context, desired *tykv1alph func(status *tykv1alpha1.ApiDefinitionStatus) { status.LatestTykSpecHash = calculateHash(apiOnTyk) status.LatestCRDSpecHash = calculateHash(desired.Spec) - status.LatestTransaction = tykv1alpha1.TransactionInfo{ - Time: metav1.Now(), - Status: tykv1alpha1.Successful, - Error: "", - } }, ) if err != nil {