Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Filter managed KongPluginBinding in finalizer reconciler #802

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions controller/konnect/reconciler_generic_pluginbindingfinalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"fmt"

"github.com/samber/lo"
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"
Expand Down Expand Up @@ -164,10 +166,14 @@ func (r *KonnectEntityPluginBindingFinalizerReconciler[T, TEnt]) Reconcile(
return ctrl.Result{}, err
}

manangedPluginBindings := lo.Filter(kongPluginBindingList.Items, func(kpb configurationv1alpha1.KongPluginBinding, _ int) bool {
return isManagedKongPluginBinding(&kpb)
})

var finalizersChangedAction string
// if the entity is marked for deletion, we need to delete all the PluginBindings that reference it.
if !ent.GetDeletionTimestamp().IsZero() {
for _, kpb := range kongPluginBindingList.Items {
for _, kpb := range manangedPluginBindings {
if err := cl.Delete(ctx, &kpb); err != nil {
if k8serrors.IsNotFound(err) {
continue
Expand All @@ -183,7 +189,7 @@ func (r *KonnectEntityPluginBindingFinalizerReconciler[T, TEnt]) Reconcile(
}
} else {
// if the entity is not marked for deletion, update the finalizers accordingly.
switch len(kongPluginBindingList.Items) {
switch len(manangedPluginBindings) {
case 0:
// in case no KongPluginBindings are referencing the entity, but it has the finalizer,
// we need to remove the finalizer.
Expand Down Expand Up @@ -237,6 +243,14 @@ func (r *KonnectEntityPluginBindingFinalizerReconciler[T, TEnt]) getKongPluginBi
}
}

// isManagedKongPluginBinding returns true if the KongPluginBinding is managed by the controller.
// REVIEW: Here we judge whether KongPluginBinding is managed by checking its owner references and treat it as managed if it has a plugin as owner reference.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense to me.

Suggested change
// REVIEW: Here we judge whether KongPluginBinding is managed by checking its owner references and treat it as managed if it has a plugin as owner reference.
// We judge whether KongPluginBinding is managed by checking its owner references and treat it as managed if it has a plugin as owner reference.

func isManagedKongPluginBinding(kpb *configurationv1alpha1.KongPluginBinding) bool {
return lo.ContainsBy(kpb.OwnerReferences, func(o metav1.OwnerReference) bool {
return o.Kind == "KongPlugin" || o.Kind == "KongClusterPlugin"
})
}

func (r *KonnectEntityPluginBindingFinalizerReconciler[T, TEnt]) setControllerBuilderOptionsForKongPluginBinding(
b *builder.TypedBuilder[ctrl.Request],
) {
Expand Down
Loading