From ce76b2042848b874a862ffbb5495069e19068718 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Ma=C5=82ek?= Date: Mon, 21 Oct 2024 18:34:47 +0200 Subject: [PATCH] fix: fix KonnectAPIAuthConfiguration reconciler --- .../konnect/reconciler_konnectapiauth.go | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/controller/konnect/reconciler_konnectapiauth.go b/controller/konnect/reconciler_konnectapiauth.go index c1241f75f..389242113 100644 --- a/controller/konnect/reconciler_konnectapiauth.go +++ b/controller/konnect/reconciler_konnectapiauth.go @@ -7,12 +7,14 @@ import ( sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations" corev1 "k8s.io/api/core/v1" + k8serrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/handler" + ctrllog "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/predicate" "github.com/kong/gateway-operator/controller/konnect/ops" @@ -153,19 +155,25 @@ func (r *KonnectAPIAuthConfigurationReconciler) Reconcile( apiAuth.Status.OrganizationID != "" || apiAuth.Status.ServerURL != serverURL.String() { + old := apiAuth.DeepCopy() apiAuth.Status.OrganizationID = "" apiAuth.Status.ServerURL = serverURL.String() - res, errUpdate := patch.StatusWithCondition( - ctx, r.client, &apiAuth, + _ = patch.SetStatusWithConditionIfDifferent(&apiAuth, konnectv1alpha1.KonnectEntityAPIAuthConfigurationValidConditionType, metav1.ConditionFalse, konnectv1alpha1.KonnectEntityAPIAuthConfigurationReasonInvalid, err.Error(), ) - if errUpdate != nil || !res.IsZero() { - return res, errUpdate + + _, errUpdate := patch.ApplyStatusPatchIfNotEmpty(ctx, r.client, ctrllog.FromContext(ctx), &apiAuth, old) + if errUpdate != nil { + if k8serrors.IsConflict(errUpdate) { + return ctrl.Result{Requeue: true}, nil + } + return ctrl.Result{}, errUpdate } + return ctrl.Result{}, nil } return ctrl.Result{}, nil @@ -191,19 +199,26 @@ func (r *KonnectAPIAuthConfigurationReconciler) Reconcile( apiAuth.Status.OrganizationID != *respOrg.MeOrganization.ID || apiAuth.Status.ServerURL != serverURL.String() { + old := apiAuth.DeepCopy() + apiAuth.Status.OrganizationID = *respOrg.MeOrganization.ID apiAuth.Status.ServerURL = serverURL.String() - res, err := patch.StatusWithCondition( - ctx, r.client, &apiAuth, + _ = patch.SetStatusWithConditionIfDifferent(&apiAuth, konnectv1alpha1.KonnectEntityAPIAuthConfigurationValidConditionType, metav1.ConditionTrue, konnectv1alpha1.KonnectEntityAPIAuthConfigurationReasonValid, condMessage, ) - if err != nil || !res.IsZero() { - return res, err + + _, errUpdate := patch.ApplyStatusPatchIfNotEmpty(ctx, r.client, ctrllog.FromContext(ctx), &apiAuth, old) + if errUpdate != nil { + if k8serrors.IsConflict(errUpdate) { + return ctrl.Result{Requeue: true}, nil + } + return ctrl.Result{}, errUpdate } + return ctrl.Result{}, nil }