Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
[TT-9420] Move status update out of CreateOrUpdate to prevent conflic…
Browse files Browse the repository at this point in the history
…ts (#642)

* Fetch the resource before updating it

Signed-off-by: Burak Sekili <[email protected]>

* copy object onto new one

Signed-off-by: Burak Sekili <[email protected]>

* Use Patch instead of update requests

Signed-off-by: Burak Sekili <[email protected]>

* Bring Status check to back

Signed-off-by: Burak Sekili <[email protected]>

* Move status update out of CreateOrUpdate

Signed-off-by: Burak Sekili <[email protected]>

---------

Signed-off-by: Burak Sekili <[email protected]>
  • Loading branch information
buraksekili authored Jul 18, 2023
1 parent 2692ac2 commit 273601b
Showing 1 changed file with 27 additions and 38 deletions.
65 changes: 27 additions & 38 deletions controllers/apidefinition_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
)
})
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 273601b

Please sign in to comment.