Skip to content

Commit

Permalink
fix(konnect): do not manage finalizers and lifecycle for unmanaged Ko…
Browse files Browse the repository at this point in the history
…ngPluginBindings (#805)
  • Loading branch information
pmalek authored Oct 25, 2024
1 parent ae5c9c0 commit f63673e
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 251 deletions.
13 changes: 13 additions & 0 deletions controller/konnect/plugins.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package konnect

import (
"github.com/samber/lo"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
Expand All @@ -25,3 +27,14 @@ var kongPluginsAnnotationChangedPredicate = predicate.Funcs{
return ok
},
}

func ownerRefIsAnyKongPlugin(obj client.Object) bool {
return lo.ContainsBy(
obj.GetOwnerReferences(),
func(ownerRef metav1.OwnerReference) bool {
return ownerRef.Kind == "KongPlugin" ||
// NOTE: We currently do not support KongClusterPlugin, but we keep this here for future use.
ownerRef.Kind == "KongClusterPlugin"
},
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ func (r *KonnectEntityPluginBindingFinalizerReconciler[T, TEnt]) enqueueObjectRe
return nil
}

// If the KongPluginBinding is unmanaged (created not using an annotation,
// and thus not having KongPlugin as an owner), skip it, do not delete it.
if !ownerRefIsAnyKongPlugin(kpb) {
return nil
}

var (
name string
e T
Expand Down Expand Up @@ -125,6 +131,9 @@ func (r *KonnectEntityPluginBindingFinalizerReconciler[T, TEnt]) enqueueObjectRe
}

// Reconcile reconciles the Konnect entity that can be set as KongPluginBinding target.
// It reconciles only entities that are referenced by managed KongPluginBindings,
// i.e. those that are created by the controller out of konghq.com/plugins annotations.
//
// Its purpose is to:
// - check if the entity is marked for deletion and mark KongPluginBindings
// that reference it.
Expand Down
6 changes: 6 additions & 0 deletions controller/konnect/watch_kongplugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ func (r *KongPluginReconciler) mapKongPluginBindings(ctx context.Context, obj cl
return []ctrl.Request{}
}

// If the KongPluginBinding is unmanaged (created not using an annotation,
// and thus not having KongPlugin as an owner), do not enqueue it.
if !ownerRefIsAnyKongPlugin(kongPluginBinding) {
return nil
}

return []ctrl.Request{
{
NamespacedName: types.NamespacedName{
Expand Down
5 changes: 5 additions & 0 deletions test/envtest/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ func assertCollectObjectExistsAndHasKonnectID(
obj interface {
client.Object
GetKonnectID() string
GetTypeName() string
},
konnectID string,
) func(c *assert.CollectT) {
t.Helper()

t.Logf("wait for the %s %s to get Konnect ID (%s) assigned",
obj.GetTypeName(), client.ObjectKeyFromObject(obj), konnectID,
)

return func(c *assert.CollectT) {
nn := client.ObjectKeyFromObject(obj)
if !assert.NoError(c, clientNamespaced.Get(ctx, nn, obj)) {
Expand Down
2 changes: 1 addition & 1 deletion test/envtest/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ const (
waitTime = 10 * time.Second

// tickTime is a generic tick time for the tests' eventual conditions.
tickTime = 500 * time.Millisecond
tickTime = 250 * time.Millisecond
)
Loading

0 comments on commit f63673e

Please sign in to comment.