diff --git a/controller/konnect/watch.go b/controller/konnect/watch.go index e030c11f..6c30f52e 100644 --- a/controller/konnect/watch.go +++ b/controller/konnect/watch.go @@ -125,6 +125,10 @@ func objectListToReconcileRequests[ items []T, filters ...func(TPtr) bool, ) []ctrl.Request { + if len(items) == 0 { + return nil + } + ret := make([]ctrl.Request, 0, len(items)) for _, item := range items { var e TPtr = &item @@ -151,6 +155,10 @@ func objectListToReconcileRequests[ // as the object. func enqueueObjectForKonnectGatewayControlPlane[ TList interface { + GetItems() []T + }, + TListPtr interface { + *TList client.ObjectList GetItems() []T }, @@ -165,8 +173,11 @@ func enqueueObjectForKonnectGatewayControlPlane[ if !ok { return nil } - var l TList - if err := cl.List(ctx, l, + var ( + l TList + lPtr TListPtr = &l + ) + if err := cl.List(ctx, lPtr, // TODO: change this when cross namespace refs are allowed. client.InNamespace(cp.GetNamespace()), client.MatchingFields{ diff --git a/controller/konnect/watch_kongcacertificate.go b/controller/konnect/watch_kongcacertificate.go index a50cf726..d5456fdd 100644 --- a/controller/konnect/watch_kongcacertificate.go +++ b/controller/konnect/watch_kongcacertificate.go @@ -41,7 +41,7 @@ func KongCACertificateReconciliationWatchOptions(cl client.Client) []func(*ctrl. return b.Watches( &konnectv1alpha1.KonnectGatewayControlPlane{}, handler.EnqueueRequestsFromMapFunc( - enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongCACertificateList]( + enqueueObjectForKonnectGatewayControlPlane[configurationv1alpha1.KongCACertificateList]( cl, IndexFieldKongCACertificateOnKonnectGatewayControlPlane, ), ), diff --git a/controller/konnect/watch_kongcertificate.go b/controller/konnect/watch_kongcertificate.go index 7d6db850..4d58a343 100644 --- a/controller/konnect/watch_kongcertificate.go +++ b/controller/konnect/watch_kongcertificate.go @@ -41,7 +41,7 @@ func KongCertificateReconciliationWatchOptions(cl client.Client) []func(*ctrl.Bu return b.Watches( &konnectv1alpha1.KonnectGatewayControlPlane{}, handler.EnqueueRequestsFromMapFunc( - enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongCertificateList]( + enqueueObjectForKonnectGatewayControlPlane[configurationv1alpha1.KongCertificateList]( cl, IndexFieldKongCertificateOnKonnectGatewayControlPlane, ), ), diff --git a/controller/konnect/watch_kongconsumer.go b/controller/konnect/watch_kongconsumer.go index 86278bfc..385c9666 100644 --- a/controller/konnect/watch_kongconsumer.go +++ b/controller/konnect/watch_kongconsumer.go @@ -54,7 +54,7 @@ func KongConsumerReconciliationWatchOptions( return b.Watches( &konnectv1alpha1.KonnectGatewayControlPlane{}, handler.EnqueueRequestsFromMapFunc( - enqueueObjectForKonnectGatewayControlPlane[*configurationv1.KongConsumerList]( + enqueueObjectForKonnectGatewayControlPlane[configurationv1.KongConsumerList]( cl, IndexFieldKongConsumerOnKonnectGatewayControlPlane, ), ), diff --git a/controller/konnect/watch_kongconsumergroup.go b/controller/konnect/watch_kongconsumergroup.go index 1b124b76..c8f8d0a5 100644 --- a/controller/konnect/watch_kongconsumergroup.go +++ b/controller/konnect/watch_kongconsumergroup.go @@ -53,7 +53,7 @@ func KongConsumerGroupReconciliationWatchOptions( return b.Watches( &konnectv1alpha1.KonnectGatewayControlPlane{}, handler.EnqueueRequestsFromMapFunc( - enqueueObjectForKonnectGatewayControlPlane[*configurationv1beta1.KongConsumerGroupList]( + enqueueObjectForKonnectGatewayControlPlane[configurationv1beta1.KongConsumerGroupList]( cl, IndexFieldKongConsumerGroupOnKonnectGatewayControlPlane, ), ), diff --git a/controller/konnect/watch_kongdataplanecertificate.go b/controller/konnect/watch_kongdataplanecertificate.go index 9224b387..01a3b05b 100644 --- a/controller/konnect/watch_kongdataplanecertificate.go +++ b/controller/konnect/watch_kongdataplanecertificate.go @@ -41,7 +41,7 @@ func KongDataPlaneClientCertificateReconciliationWatchOptions(cl client.Client) return b.Watches( &konnectv1alpha1.KonnectGatewayControlPlane{}, handler.EnqueueRequestsFromMapFunc( - enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongDataPlaneClientCertificateList]( + enqueueObjectForKonnectGatewayControlPlane[configurationv1alpha1.KongDataPlaneClientCertificateList]( cl, IndexFieldKongDataPlaneClientCertificateOnKonnectGatewayControlPlane, ), ), diff --git a/controller/konnect/watch_kongkey.go b/controller/konnect/watch_kongkey.go index 2abda208..c90f8d92 100644 --- a/controller/konnect/watch_kongkey.go +++ b/controller/konnect/watch_kongkey.go @@ -49,7 +49,7 @@ func KongKeyReconciliationWatchOptions(cl client.Client) []func(*ctrl.Builder) * return b.Watches( &konnectv1alpha1.KonnectGatewayControlPlane{}, handler.EnqueueRequestsFromMapFunc( - enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongKeyList]( + enqueueObjectForKonnectGatewayControlPlane[configurationv1alpha1.KongKeyList]( cl, IndexFieldKongKeyOnKonnectGatewayControlPlane, ), ), diff --git a/controller/konnect/watch_kongkeyset.go b/controller/konnect/watch_kongkeyset.go index 423731f6..b784d01d 100644 --- a/controller/konnect/watch_kongkeyset.go +++ b/controller/konnect/watch_kongkeyset.go @@ -41,7 +41,7 @@ func KongKeySetReconciliationWatchOptions(cl client.Client) []func(*ctrl.Builder return b.Watches( &konnectv1alpha1.KonnectGatewayControlPlane{}, handler.EnqueueRequestsFromMapFunc( - enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongKeySetList]( + enqueueObjectForKonnectGatewayControlPlane[configurationv1alpha1.KongKeySetList]( cl, IndexFieldKongKeySetOnKonnectGatewayControlPlane, ), ), diff --git a/controller/konnect/watch_kongpluginbinding.go b/controller/konnect/watch_kongpluginbinding.go index 7676d6de..9795aa46 100644 --- a/controller/konnect/watch_kongpluginbinding.go +++ b/controller/konnect/watch_kongpluginbinding.go @@ -60,7 +60,7 @@ func KongPluginBindingReconciliationWatchOptions( return b.Watches( &konnectv1alpha1.KonnectGatewayControlPlane{}, handler.EnqueueRequestsFromMapFunc( - enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongPluginBindingList]( + enqueueObjectForKonnectGatewayControlPlane[configurationv1alpha1.KongPluginBindingList]( cl, IndexFieldKongPluginBindingKonnectGatewayControlPlane, ), ), diff --git a/controller/konnect/watch_kongservice.go b/controller/konnect/watch_kongservice.go index 73c24133..f8089ae4 100644 --- a/controller/konnect/watch_kongservice.go +++ b/controller/konnect/watch_kongservice.go @@ -52,7 +52,7 @@ func KongServiceReconciliationWatchOptions( return b.Watches( &konnectv1alpha1.KonnectGatewayControlPlane{}, handler.EnqueueRequestsFromMapFunc( - enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongServiceList]( + enqueueObjectForKonnectGatewayControlPlane[configurationv1alpha1.KongServiceList]( cl, IndexFieldKongServiceOnKonnectGatewayControlPlane, ), ), diff --git a/controller/konnect/watch_kongupstream.go b/controller/konnect/watch_kongupstream.go index 1514a1ec..5b5b7d15 100644 --- a/controller/konnect/watch_kongupstream.go +++ b/controller/konnect/watch_kongupstream.go @@ -52,7 +52,7 @@ func KongUpstreamReconciliationWatchOptions( return b.Watches( &konnectv1alpha1.KonnectGatewayControlPlane{}, handler.EnqueueRequestsFromMapFunc( - enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongUpstreamList]( + enqueueObjectForKonnectGatewayControlPlane[configurationv1alpha1.KongUpstreamList]( cl, IndexFieldKongUpstreamOnKonnectGatewayControlPlane, ), ), diff --git a/controller/konnect/watch_kongvault.go b/controller/konnect/watch_kongvault.go index b7976e74..3be125a9 100644 --- a/controller/konnect/watch_kongvault.go +++ b/controller/konnect/watch_kongvault.go @@ -41,7 +41,7 @@ func KongVaultReconciliationWatchOptions(cl client.Client) []func(*ctrl.Builder) return b.Watches( &konnectv1alpha1.KonnectGatewayControlPlane{}, handler.EnqueueRequestsFromMapFunc( - enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongVaultList]( + enqueueObjectForKonnectGatewayControlPlane[configurationv1alpha1.KongVaultList]( cl, IndexFieldKongVaultOnKonnectGatewayControlPlane, ), ), diff --git a/controller/konnect/watch_test.go b/controller/konnect/watch_test.go index 1eceb323..e8f50b9d 100644 --- a/controller/konnect/watch_test.go +++ b/controller/konnect/watch_test.go @@ -1,13 +1,18 @@ package konnect import ( + "context" "testing" "github.com/stretchr/testify/require" 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/client" fakectrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client/fake" "github.com/kong/gateway-operator/controller/konnect/constraints" + "github.com/kong/gateway-operator/modules/manager/scheme" configurationv1 "github.com/kong/kubernetes-configuration/api/configuration/v1" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" @@ -80,3 +85,118 @@ func TestObjectListToReconcileRequests(t *testing.T) { } }) } + +func TestEnqueueObjectForKonnectGatewayControlPlane(t *testing.T) { + cp := &konnectv1alpha1.KonnectGatewayControlPlane{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test1", + Namespace: "default", + }, + } + t.Run("KongConsumer", func(t *testing.T) { + tests := []struct { + name string + index string + list []client.Object + extractFunc client.IndexerFunc + expected []ctrl.Request + }{ + { + name: "no ControlPlane reference", + index: IndexFieldKongConsumerOnKonnectGatewayControlPlane, + extractFunc: kongConsumerReferencesKonnectGatewayControlPlane, + list: []client.Object{ + &configurationv1.KongConsumer{ + ObjectMeta: metav1.ObjectMeta{ + Name: "consumer1", + Namespace: "default", + }, + }, + &configurationv1.KongConsumer{ + ObjectMeta: metav1.ObjectMeta{ + Name: "consumer2", + Namespace: "default", + }, + }, + }, + }, + { + name: "1 KongConumser refers to KonnectGatewayControlPlane", + index: IndexFieldKongConsumerOnKonnectGatewayControlPlane, + extractFunc: kongConsumerReferencesKonnectGatewayControlPlane, + list: []client.Object{ + &configurationv1.KongConsumer{ + ObjectMeta: metav1.ObjectMeta{ + Name: "consumer1", + Namespace: "default", + }, + Spec: configurationv1.KongConsumerSpec{ + ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{ + Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef, + KonnectNamespacedRef: &configurationv1alpha1.KonnectNamespacedRef{ + Name: cp.Name, + }, + }, + }, + }, + &configurationv1.KongConsumer{ + ObjectMeta: metav1.ObjectMeta{ + Name: "consumer2", + Namespace: "default", + }, + }, + }, + expected: []ctrl.Request{ + { + NamespacedName: types.NamespacedName{ + Name: "consumer1", + Namespace: "default", + }, + }, + }, + }, + { + name: "1 KongConumser refers to a different KonnectGatewayControlPlane", + index: IndexFieldKongConsumerOnKonnectGatewayControlPlane, + extractFunc: kongConsumerReferencesKonnectGatewayControlPlane, + list: []client.Object{ + &configurationv1.KongConsumer{ + ObjectMeta: metav1.ObjectMeta{ + Name: "consumer1", + Namespace: "default", + }, + Spec: configurationv1.KongConsumerSpec{ + ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{ + Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef, + KonnectNamespacedRef: &configurationv1alpha1.KonnectNamespacedRef{ + Name: "different-cp", + }, + }, + }, + }, + &configurationv1.KongConsumer{ + ObjectMeta: metav1.ObjectMeta{ + Name: "consumer2", + Namespace: "default", + }, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cl := fakectrlruntimeclient.NewClientBuilder(). + WithScheme(scheme.Get()). + WithObjects(tt.list...). + WithIndex(&configurationv1.KongConsumer{}, tt.index, tt.extractFunc). + Build() + require.NotNil(t, cl) + + f := enqueueObjectForKonnectGatewayControlPlane[configurationv1.KongConsumerList](cl, tt.index) + requests := f(context.Background(), cp) + require.Len(t, requests, len(tt.expected)) + require.Equal(t, tt.expected, requests) + }) + } + }) +}