From 3a4ad19cbde812617692d133634af6bef5060554 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Ma=C5=82ek?= Date: Tue, 29 Oct 2024 13:18:15 +0100 Subject: [PATCH] fix: fix enqueueObjectForKonnectGatewayControlPlane type parameters (#825) --- controller/konnect/watch.go | 15 ++- controller/konnect/watch_kongcacertificate.go | 2 +- controller/konnect/watch_kongcertificate.go | 2 +- controller/konnect/watch_kongconsumer.go | 2 +- controller/konnect/watch_kongconsumergroup.go | 2 +- .../konnect/watch_kongdataplanecertificate.go | 2 +- controller/konnect/watch_kongkey.go | 2 +- controller/konnect/watch_kongkeyset.go | 2 +- controller/konnect/watch_kongpluginbinding.go | 2 +- controller/konnect/watch_kongservice.go | 2 +- controller/konnect/watch_kongupstream.go | 2 +- controller/konnect/watch_kongvault.go | 2 +- controller/konnect/watch_test.go | 120 ++++++++++++++++++ 13 files changed, 144 insertions(+), 13 deletions(-) diff --git a/controller/konnect/watch.go b/controller/konnect/watch.go index e030c11f6..6c30f52e7 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 a50cf7262..d5456fdd7 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 7d6db8502..4d58a343b 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 86278bfc9..385c96660 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 1b124b762..c8f8d0a59 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 9224b3876..01a3b05b5 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 2abda2084..c90f8d92f 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 423731f6a..b784d01dc 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 7676d6dee..9795aa467 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 73c24133f..f8089ae47 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 1514a1ec8..5b5b7d158 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 b7976e749..3be125a92 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 1eceb3231..e8f50b9de 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) + }) + } + }) +}