diff --git a/controller/konnect/watch.go b/controller/konnect/watch.go index 3b7bc985..e030c11f 100644 --- a/controller/konnect/watch.go +++ b/controller/konnect/watch.go @@ -9,6 +9,7 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" ctrllog "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/reconcile" "github.com/kong/gateway-operator/controller/konnect/constraints" operatorerrors "github.com/kong/gateway-operator/internal/errors" @@ -118,11 +119,8 @@ func controlPlaneRefIsKonnectNamespacedRef[ // objectListToReconcileRequests converts a list of objects to a list of reconcile requests. func objectListToReconcileRequests[ - T any, - TPtr interface { - *T - client.Object - }, + T constraints.SupportedKonnectEntityType, + TPtr constraints.EntityType[T], ]( items []T, filters ...func(TPtr) bool, @@ -145,3 +143,39 @@ func objectListToReconcileRequests[ return ret } + +// enqueueObjectForKonnectGatewayControlPlane returns a function that enqueues +// reconcile requests for objects matching the provided list type, so for example +// providing KongConsumerList, this function will enqueue reconcile requests for +// KongConsumers that refer to the KonnectGatewayControlPlane that was provided +// as the object. +func enqueueObjectForKonnectGatewayControlPlane[ + TList interface { + client.ObjectList + GetItems() []T + }, + T constraints.SupportedKonnectEntityType, + TT constraints.EntityType[T], +]( + cl client.Client, + index string, +) func(context.Context, client.Object) []reconcile.Request { + return func(ctx context.Context, obj client.Object) []reconcile.Request { + cp, ok := obj.(*konnectv1alpha1.KonnectGatewayControlPlane) + if !ok { + return nil + } + var l TList + if err := cl.List(ctx, l, + // TODO: change this when cross namespace refs are allowed. + client.InNamespace(cp.GetNamespace()), + client.MatchingFields{ + index: cp.GetNamespace() + "/" + cp.GetName(), + }, + ); err != nil { + return nil + } + + return objectListToReconcileRequests[T, TT](l.GetItems()) + } +} diff --git a/controller/konnect/watch_kongcacertificate.go b/controller/konnect/watch_kongcacertificate.go index bfd835af..a50cf726 100644 --- a/controller/konnect/watch_kongcacertificate.go +++ b/controller/konnect/watch_kongcacertificate.go @@ -41,7 +41,9 @@ func KongCACertificateReconciliationWatchOptions(cl client.Client) []func(*ctrl. return b.Watches( &konnectv1alpha1.KonnectGatewayControlPlane{}, handler.EnqueueRequestsFromMapFunc( - enqueueKongCACertificateForKonnectControlPlane(cl), + enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongCACertificateList]( + cl, IndexFieldKongCACertificateOnKonnectGatewayControlPlane, + ), ), ) }, @@ -120,26 +122,3 @@ func enqueueKongCACertificateForKonnectAPIAuthConfiguration(cl client.Client) ha return ret } } - -func enqueueKongCACertificateForKonnectControlPlane( - cl client.Client, -) func(ctx context.Context, obj client.Object) []reconcile.Request { - return func(ctx context.Context, obj client.Object) []reconcile.Request { - cp, ok := obj.(*konnectv1alpha1.KonnectGatewayControlPlane) - if !ok { - return nil - } - var l configurationv1alpha1.KongCACertificateList - if err := cl.List(ctx, &l, - // TODO: change this when cross namespace refs are allowed. - client.InNamespace(cp.GetNamespace()), - client.MatchingFields{ - IndexFieldKongCACertificateOnKonnectGatewayControlPlane: cp.Namespace + "/" + cp.Name, - }, - ); err != nil { - return nil - } - - return objectListToReconcileRequests(l.Items) - } -} diff --git a/controller/konnect/watch_kongcertificate.go b/controller/konnect/watch_kongcertificate.go index b71115e6..7d6db850 100644 --- a/controller/konnect/watch_kongcertificate.go +++ b/controller/konnect/watch_kongcertificate.go @@ -41,7 +41,9 @@ func KongCertificateReconciliationWatchOptions(cl client.Client) []func(*ctrl.Bu return b.Watches( &konnectv1alpha1.KonnectGatewayControlPlane{}, handler.EnqueueRequestsFromMapFunc( - enqueueKongCertificateForKonnectControlPlane(cl), + enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongCertificateList]( + cl, IndexFieldKongCertificateOnKonnectGatewayControlPlane, + ), ), ) }, @@ -120,26 +122,3 @@ func enqueueKongCertificateForKonnectAPIAuthConfiguration(cl client.Client) hand return ret } } - -func enqueueKongCertificateForKonnectControlPlane( - cl client.Client, -) func(ctx context.Context, obj client.Object) []reconcile.Request { - return func(ctx context.Context, obj client.Object) []reconcile.Request { - cp, ok := obj.(*konnectv1alpha1.KonnectGatewayControlPlane) - if !ok { - return nil - } - var l configurationv1alpha1.KongCertificateList - if err := cl.List(ctx, &l, - // TODO: change this when cross namespace refs are allowed. - client.InNamespace(cp.GetNamespace()), - client.MatchingFields{ - IndexFieldKongCertificateOnKonnectGatewayControlPlane: cp.Namespace + "/" + cp.Name, - }, - ); err != nil { - return nil - } - - return objectListToReconcileRequests(l.Items) - } -} diff --git a/controller/konnect/watch_kongconsumer.go b/controller/konnect/watch_kongconsumer.go index 31dacfe1..86278bfc 100644 --- a/controller/konnect/watch_kongconsumer.go +++ b/controller/konnect/watch_kongconsumer.go @@ -54,7 +54,9 @@ func KongConsumerReconciliationWatchOptions( return b.Watches( &konnectv1alpha1.KonnectGatewayControlPlane{}, handler.EnqueueRequestsFromMapFunc( - enqueueKongConsumerForKonnectGatewayControlPlane(cl), + enqueueObjectForKonnectGatewayControlPlane[*configurationv1.KongConsumerList]( + cl, IndexFieldKongConsumerOnKonnectGatewayControlPlane, + ), ), ) }, @@ -144,29 +146,6 @@ func enqueueKongConsumerForKonnectAPIAuthConfiguration( } } -func enqueueKongConsumerForKonnectGatewayControlPlane( - cl client.Client, -) func(ctx context.Context, obj client.Object) []reconcile.Request { - return func(ctx context.Context, obj client.Object) []reconcile.Request { - cp, ok := obj.(*konnectv1alpha1.KonnectGatewayControlPlane) - if !ok { - return nil - } - var l configurationv1.KongConsumerList - if err := cl.List(ctx, &l, - // TODO: change this when cross namespace refs are allowed. - client.InNamespace(cp.GetNamespace()), - client.MatchingFields{ - IndexFieldKongConsumerOnKonnectGatewayControlPlane: cp.Namespace + "/" + cp.Name, - }, - ); err != nil { - return nil - } - - return objectListToReconcileRequests(l.Items) - } -} - func enqueueKongConsumerForKongConsumerGroup( cl client.Client, ) func(ctx context.Context, obj client.Object) []reconcile.Request { diff --git a/controller/konnect/watch_kongconsumergroup.go b/controller/konnect/watch_kongconsumergroup.go index eba3e2d2..1b124b76 100644 --- a/controller/konnect/watch_kongconsumergroup.go +++ b/controller/konnect/watch_kongconsumergroup.go @@ -53,7 +53,9 @@ func KongConsumerGroupReconciliationWatchOptions( return b.Watches( &konnectv1alpha1.KonnectGatewayControlPlane{}, handler.EnqueueRequestsFromMapFunc( - enqueueKongConsumerGroupForKonnectGatewayControlPlane(cl), + enqueueObjectForKonnectGatewayControlPlane[*configurationv1beta1.KongConsumerGroupList]( + cl, IndexFieldKongConsumerGroupOnKonnectGatewayControlPlane, + ), ), ) }, @@ -134,26 +136,3 @@ func enqueueKongConsumerGroupForKonnectAPIAuthConfiguration( return ret } } - -func enqueueKongConsumerGroupForKonnectGatewayControlPlane( - cl client.Client, -) func(ctx context.Context, obj client.Object) []reconcile.Request { - return func(ctx context.Context, obj client.Object) []reconcile.Request { - cp, ok := obj.(*konnectv1alpha1.KonnectGatewayControlPlane) - if !ok { - return nil - } - var l configurationv1beta1.KongConsumerGroupList - if err := cl.List(ctx, &l, - // TODO: change this when cross namespace refs are allowed. - client.InNamespace(cp.GetNamespace()), - client.MatchingFields{ - IndexFieldKongConsumerGroupOnKonnectGatewayControlPlane: cp.Namespace + "/" + cp.Name, - }, - ); err != nil { - return nil - } - - return objectListToReconcileRequests(l.Items) - } -} diff --git a/controller/konnect/watch_kongdataplanecertificate.go b/controller/konnect/watch_kongdataplanecertificate.go index 800626bd..9224b387 100644 --- a/controller/konnect/watch_kongdataplanecertificate.go +++ b/controller/konnect/watch_kongdataplanecertificate.go @@ -41,34 +41,15 @@ func KongDataPlaneClientCertificateReconciliationWatchOptions(cl client.Client) return b.Watches( &konnectv1alpha1.KonnectGatewayControlPlane{}, handler.EnqueueRequestsFromMapFunc( - enqueueKongDataPlaneClientCertificateForKonnectControlPlane(cl), + enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongDataPlaneClientCertificateList]( + cl, IndexFieldKongDataPlaneClientCertificateOnKonnectGatewayControlPlane, + ), ), ) }, } } -func enqueueKongDataPlaneClientCertificateForKonnectControlPlane(cl client.Client) handler.MapFunc { - return func(ctx context.Context, obj client.Object) []reconcile.Request { - auth, ok := obj.(*konnectv1alpha1.KonnectGatewayControlPlane) - if !ok { - return nil - } - var l configurationv1alpha1.KongDataPlaneClientCertificateList - if err := cl.List(ctx, &l, - // TODO: change this when cross namespace refs are allowed. - client.InNamespace(auth.GetNamespace()), - client.MatchingFields{ - IndexFieldKongDataPlaneClientCertificateOnKonnectGatewayControlPlane: client.ObjectKeyFromObject(auth).String(), - }, - ); err != nil { - return nil - } - - return objectListToReconcileRequests(l.Items) - } -} - func enqueueKongDataPlaneClientCertificateForKonnectAPIAuthConfiguration( cl client.Client, ) func(ctx context.Context, obj client.Object) []reconcile.Request { diff --git a/controller/konnect/watch_kongkey.go b/controller/konnect/watch_kongkey.go index 91e1c03b..2abda208 100644 --- a/controller/konnect/watch_kongkey.go +++ b/controller/konnect/watch_kongkey.go @@ -49,7 +49,9 @@ func KongKeyReconciliationWatchOptions(cl client.Client) []func(*ctrl.Builder) * return b.Watches( &konnectv1alpha1.KonnectGatewayControlPlane{}, handler.EnqueueRequestsFromMapFunc( - enqueueKongKeyForKonnectControlPlane(cl), + enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongKeyList]( + cl, IndexFieldKongKeyOnKonnectGatewayControlPlane, + ), ), ) }, @@ -129,29 +131,6 @@ func enqueueKongKeyForKonnectAPIAuthConfiguration(cl client.Client) handler.MapF } } -func enqueueKongKeyForKonnectControlPlane( - cl client.Client, -) func(ctx context.Context, obj client.Object) []reconcile.Request { - return func(ctx context.Context, obj client.Object) []reconcile.Request { - cp, ok := obj.(*konnectv1alpha1.KonnectGatewayControlPlane) - if !ok { - return nil - } - var l configurationv1alpha1.KongKeyList - if err := cl.List(ctx, &l, - // TODO: change this when cross namespace refs are allowed. - client.InNamespace(cp.GetNamespace()), - client.MatchingFields{ - IndexFieldKongKeyOnKonnectGatewayControlPlane: cp.GetNamespace() + "/" + cp.GetName(), - }, - ); err != nil { - return nil - } - - return objectListToReconcileRequests(l.Items) - } -} - func enqueueKongKeyForKongKeySet(cl client.Client) handler.MapFunc { return func(ctx context.Context, obj client.Object) []reconcile.Request { keySet, ok := obj.(*configurationv1alpha1.KongKeySet) diff --git a/controller/konnect/watch_kongkeyset.go b/controller/konnect/watch_kongkeyset.go index 27216f7a..423731f6 100644 --- a/controller/konnect/watch_kongkeyset.go +++ b/controller/konnect/watch_kongkeyset.go @@ -41,7 +41,9 @@ func KongKeySetReconciliationWatchOptions(cl client.Client) []func(*ctrl.Builder return b.Watches( &konnectv1alpha1.KonnectGatewayControlPlane{}, handler.EnqueueRequestsFromMapFunc( - enqueueKongKeySetForKonnectControlPlane(cl), + enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongKeySetList]( + cl, IndexFieldKongKeySetOnKonnectGatewayControlPlane, + ), ), ) }, @@ -120,26 +122,3 @@ func enqueueKongKeySetForKonnectAPIAuthConfiguration(cl client.Client) handler.M return ret } } - -func enqueueKongKeySetForKonnectControlPlane( - cl client.Client, -) func(ctx context.Context, obj client.Object) []reconcile.Request { - return func(ctx context.Context, obj client.Object) []reconcile.Request { - cp, ok := obj.(*konnectv1alpha1.KonnectGatewayControlPlane) - if !ok { - return nil - } - var l configurationv1alpha1.KongKeySetList - if err := cl.List(ctx, &l, - // TODO: change this when cross namespace refs are allowed. - client.InNamespace(cp.GetNamespace()), - client.MatchingFields{ - IndexFieldKongKeySetOnKonnectGatewayControlPlane: cp.Namespace + "/" + cp.Name, - }, - ); err != nil { - return nil - } - - return objectListToReconcileRequests(l.Items) - } -} diff --git a/controller/konnect/watch_kongpluginbinding.go b/controller/konnect/watch_kongpluginbinding.go index daee8a35..7676d6de 100644 --- a/controller/konnect/watch_kongpluginbinding.go +++ b/controller/konnect/watch_kongpluginbinding.go @@ -60,7 +60,9 @@ func KongPluginBindingReconciliationWatchOptions( return b.Watches( &konnectv1alpha1.KonnectGatewayControlPlane{}, handler.EnqueueRequestsFromMapFunc( - enqueueKongPluginBindingForKonnectGatewayControlPlane(cl), + enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongPluginBindingList]( + cl, IndexFieldKongPluginBindingKonnectGatewayControlPlane, + ), ), ) }, @@ -186,29 +188,6 @@ func enqueueKongPluginBindingForKonnectAPIAuthConfiguration( } } -func enqueueKongPluginBindingForKonnectGatewayControlPlane( - cl client.Client, -) func(ctx context.Context, obj client.Object) []reconcile.Request { - return func(ctx context.Context, obj client.Object) []reconcile.Request { - cp, ok := obj.(*konnectv1alpha1.KonnectGatewayControlPlane) - if !ok { - return nil - } - var l configurationv1alpha1.KongPluginBindingList - if err := cl.List(ctx, &l, - // TODO: change this when cross namespace refs are allowed. - client.InNamespace(cp.GetNamespace()), - client.MatchingFields{ - IndexFieldKongPluginBindingKonnectGatewayControlPlane: cp.Namespace + "/" + cp.Name, - }, - ); err != nil { - return nil - } - - return objectListToReconcileRequests(l.Items) - } -} - func enqueueKongPluginBindingForKongPlugin(cl client.Client) func( ctx context.Context, obj client.Object) []reconcile.Request { return func(ctx context.Context, obj client.Object) []reconcile.Request { diff --git a/controller/konnect/watch_kongservice.go b/controller/konnect/watch_kongservice.go index c8e60b1f..73c24133 100644 --- a/controller/konnect/watch_kongservice.go +++ b/controller/konnect/watch_kongservice.go @@ -52,7 +52,9 @@ func KongServiceReconciliationWatchOptions( return b.Watches( &konnectv1alpha1.KonnectGatewayControlPlane{}, handler.EnqueueRequestsFromMapFunc( - enqueueKongServiceForKonnectGatewayControlPlane(cl), + enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongServiceList]( + cl, IndexFieldKongServiceOnKonnectGatewayControlPlane, + ), ), ) }, @@ -133,26 +135,3 @@ func enqueueKongServiceForKonnectAPIAuthConfiguration( return ret } } - -func enqueueKongServiceForKonnectGatewayControlPlane( - cl client.Client, -) func(ctx context.Context, obj client.Object) []reconcile.Request { - return func(ctx context.Context, obj client.Object) []reconcile.Request { - cp, ok := obj.(*konnectv1alpha1.KonnectGatewayControlPlane) - if !ok { - return nil - } - var l configurationv1alpha1.KongServiceList - if err := cl.List(ctx, &l, - // TODO: change this when cross namespace refs are allowed. - client.InNamespace(cp.GetNamespace()), - client.MatchingFields{ - IndexFieldKongServiceOnKonnectGatewayControlPlane: cp.Namespace + "/" + cp.Name, - }, - ); err != nil { - return nil - } - - return objectListToReconcileRequests(l.Items) - } -} diff --git a/controller/konnect/watch_kongupstream.go b/controller/konnect/watch_kongupstream.go index afd10a81..1514a1ec 100644 --- a/controller/konnect/watch_kongupstream.go +++ b/controller/konnect/watch_kongupstream.go @@ -52,7 +52,9 @@ func KongUpstreamReconciliationWatchOptions( return b.Watches( &konnectv1alpha1.KonnectGatewayControlPlane{}, handler.EnqueueRequestsFromMapFunc( - enqueueKongUpstreamForKonnectGatewayControlPlane(cl), + enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongUpstreamList]( + cl, IndexFieldKongUpstreamOnKonnectGatewayControlPlane, + ), ), ) }, @@ -133,26 +135,3 @@ func enqueueKongUpstreamForKonnectAPIAuthConfiguration( return ret } } - -func enqueueKongUpstreamForKonnectGatewayControlPlane( - cl client.Client, -) func(ctx context.Context, obj client.Object) []reconcile.Request { - return func(ctx context.Context, obj client.Object) []reconcile.Request { - cp, ok := obj.(*konnectv1alpha1.KonnectGatewayControlPlane) - if !ok { - return nil - } - var l configurationv1alpha1.KongUpstreamList - if err := cl.List(ctx, &l, - // TODO: change this when cross namespace refs are allowed. - client.InNamespace(cp.GetNamespace()), - client.MatchingFields{ - IndexFieldKongUpstreamOnKonnectGatewayControlPlane: cp.Namespace + "/" + cp.Name, - }, - ); err != nil { - return nil - } - - return objectListToReconcileRequests(l.Items) - } -} diff --git a/controller/konnect/watch_kongvault.go b/controller/konnect/watch_kongvault.go index aa196b8d..b7976e74 100644 --- a/controller/konnect/watch_kongvault.go +++ b/controller/konnect/watch_kongvault.go @@ -41,7 +41,9 @@ func KongVaultReconciliationWatchOptions(cl client.Client) []func(*ctrl.Builder) return b.Watches( &konnectv1alpha1.KonnectGatewayControlPlane{}, handler.EnqueueRequestsFromMapFunc( - enqueueKongVaultForKonnectGatewayControlPlane(cl), + enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongVaultList]( + cl, IndexFieldKongVaultOnKonnectGatewayControlPlane, + ), ), ) }, @@ -123,27 +125,3 @@ func enqueueKongVaultForKonnectAPIAuthConfiguration( return ret } } - -func enqueueKongVaultForKonnectGatewayControlPlane( - cl client.Client, -) func(ctx context.Context, obj client.Object) []reconcile.Request { - return func(ctx context.Context, obj client.Object) []reconcile.Request { - cp, ok := obj.(*konnectv1alpha1.KonnectGatewayControlPlane) - if !ok { - return nil - } - - var l configurationv1alpha1.KongVaultList - if err := cl.List(ctx, &l, - // TODO: change this when cross namespace refs are allowed. - client.InNamespace(cp.GetNamespace()), - client.MatchingFields{ - IndexFieldKongVaultOnKonnectGatewayControlPlane: cp.Namespace + "/" + cp.Name, - }, - ); err != nil { - return nil - } - - return objectListToReconcileRequests(l.Items) - } -}