Skip to content

Commit

Permalink
fix: fix KonnectAPIAuthConfiguration reconciler
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek committed Oct 21, 2024
1 parent f8b995f commit ce76b20
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions controller/konnect/reconciler_konnectapiauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand Down

0 comments on commit ce76b20

Please sign in to comment.