From df37e1e4f1bd66f9bc55c0345d2a7c01b907b3b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Ma=C5=82ek?= Date: Thu, 26 Sep 2024 08:48:33 +0200 Subject: [PATCH] refactor(konnect): refactor plugin reconciler watch handlers --- .../konnect/index_credentials_basicauth.go | 6 +- controller/konnect/reconciler_kongplugin.go | 4 +- controller/konnect/watch_kongplugin.go | 76 ++++++++++--------- modules/manager/controller_setup.go | 12 +-- 4 files changed, 51 insertions(+), 47 deletions(-) diff --git a/controller/konnect/index_credentials_basicauth.go b/controller/konnect/index_credentials_basicauth.go index 0c355e351..5f9053fd6 100644 --- a/controller/konnect/index_credentials_basicauth.go +++ b/controller/konnect/index_credentials_basicauth.go @@ -17,13 +17,13 @@ func IndexOptionsForCredentialsBasicAuth() []ReconciliationIndexOption { { IndexObject: &configurationv1alpha1.KongCredentialBasicAuth{}, IndexField: IndexFieldKongCredentialBasicAuthReferencesKongConsumer, - ExtractValue: kongKongCredentialBasicAuthReferencesConsumer, + ExtractValue: kongCredentialBasicAuthReferencesConsumer, }, } } -// kongKongCredentialBasicAuthReferencesConsumer returns the name of referenced Consumer. -func kongKongCredentialBasicAuthReferencesConsumer(obj client.Object) []string { +// kongCredentialBasicAuthReferencesConsumer returns the name of referenced Consumer. +func kongCredentialBasicAuthReferencesConsumer(obj client.Object) []string { cred, ok := obj.(*configurationv1alpha1.KongCredentialBasicAuth) if !ok { return nil diff --git a/controller/konnect/reconciler_kongplugin.go b/controller/konnect/reconciler_kongplugin.go index 3ed235c53..82d3cd94b 100644 --- a/controller/konnect/reconciler_kongplugin.go +++ b/controller/konnect/reconciler_kongplugin.go @@ -53,14 +53,14 @@ func (r *KongPluginReconciler) SetupWithManager(_ context.Context, mgr ctrl.Mana ). Watches( &configurationv1alpha1.KongService{}, - handler.EnqueueRequestsFromMapFunc(r.mapKongServices), + handler.EnqueueRequestsFromMapFunc(mapPluginsFromAnnotation[configurationv1alpha1.KongService](r.developmentMode)), builder.WithPredicates( kongPluginsAnnotationChangedPredicate, ), ). Watches( &configurationv1alpha1.KongRoute{}, - handler.EnqueueRequestsFromMapFunc(r.mapKongRoutes), + handler.EnqueueRequestsFromMapFunc(mapPluginsFromAnnotation[configurationv1alpha1.KongRoute](r.developmentMode)), builder.WithPredicates( kongPluginsAnnotationChangedPredicate, ), diff --git a/controller/konnect/watch_kongplugin.go b/controller/konnect/watch_kongplugin.go index 242a9e606..64b9aba1c 100644 --- a/controller/konnect/watch_kongplugin.go +++ b/controller/konnect/watch_kongplugin.go @@ -3,39 +3,60 @@ package konnect import ( "context" "errors" + "fmt" "k8s.io/apimachinery/pkg/types" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" + "github.com/kong/gateway-operator/controller/konnect/constraints" "github.com/kong/gateway-operator/controller/pkg/log" "github.com/kong/gateway-operator/pkg/annotations" + configurationv1 "github.com/kong/kubernetes-configuration/api/configuration/v1" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" + configurationv1beta1 "github.com/kong/kubernetes-configuration/api/configuration/v1beta1" ) -// mapKongServices enqueue requests for KongPlugin objects based on KongService annotations. -func (r *KongPluginReconciler) mapKongServices(ctx context.Context, obj client.Object) []ctrl.Request { - logger := log.GetLogger(ctx, "KongPlugin", r.developmentMode) - kongService, ok := obj.(*configurationv1alpha1.KongService) - if !ok { - log.Error(logger, errors.New("cannot cast object to KongService"), "KongService mapping handler", obj) - return []ctrl.Request{} - } +// mapPluginsFromAnnotation enqueue requests for KongPlugins based on +// provided object's annotations. +func mapPluginsFromAnnotation[ + T interface { + configurationv1alpha1.KongService | + configurationv1alpha1.KongRoute | + configurationv1.KongConsumer | + configurationv1beta1.KongConsumerGroup + GetTypeName() string + }, +](devMode bool) func(ctx context.Context, obj client.Object) []ctrl.Request { + return func(ctx context.Context, obj client.Object) []ctrl.Request { + _, ok := any(obj).(*T) + if !ok { + entityTypeName := constraints.EntityTypeName[T]() + logger := log.GetLogger(ctx, entityTypeName, devMode) + log.Error(logger, + fmt.Errorf("cannot cast object to %s", entityTypeName), + fmt.Sprintf("%s mapping handler", entityTypeName), obj, + ) + return []ctrl.Request{} + } - return mapObjectRequestsForItsPlugins(kongService) -} + var ( + namespace = obj.GetNamespace() + plugins = annotations.ExtractPlugins(obj) + requests = make([]ctrl.Request, 0, len(plugins)) + ) -// mapKongRoutes enqueue requests for KongPlugin objects based on KongRoute annotations. -func (r *KongPluginReconciler) mapKongRoutes(ctx context.Context, obj client.Object) []ctrl.Request { - logger := log.GetLogger(ctx, "KongPlugin", r.developmentMode) - kongRoute, ok := obj.(*configurationv1alpha1.KongRoute) - if !ok { - log.Error(logger, errors.New("cannot cast object to KongRoute"), "KongRoute mapping handler", obj) - return []ctrl.Request{} + for _, p := range plugins { + requests = append(requests, ctrl.Request{ + NamespacedName: client.ObjectKey{ + Namespace: namespace, + Name: p, + }, + }) + } + return requests } - - return mapObjectRequestsForItsPlugins(kongRoute) } // mapKongPluginBindings enqueue requests for KongPlugins referenced by KongPluginBindings in their .spec.pluginRef field. @@ -56,20 +77,3 @@ func (r *KongPluginReconciler) mapKongPluginBindings(ctx context.Context, obj cl }, } } - -func mapObjectRequestsForItsPlugins(obj client.Object) []ctrl.Request { - var ( - namespace = obj.GetNamespace() - plugins = annotations.ExtractPlugins(obj) - requests = make([]ctrl.Request, 0, len(plugins)) - ) - for _, p := range plugins { - requests = append(requests, ctrl.Request{ - NamespacedName: client.ObjectKey{ - Namespace: namespace, - Name: p, - }, - }) - } - return requests -} diff --git a/modules/manager/controller_setup.go b/modules/manager/controller_setup.go index 71bfdc30f..9175c0b81 100644 --- a/modules/manager/controller_setup.go +++ b/modules/manager/controller_setup.go @@ -84,10 +84,10 @@ const ( KongServicePluginBindingFinalizerControllerName = "KongServicePluginBindingFinalizer" // KongCredentialsSecretControllerName is the name of the Credentials Secret controller. KongCredentialsSecretControllerName = "KongCredentialSecret" - // KongKongCredentialBasicAuthControllerName is the name of the KongCredentialBasicAuth controller. - KongKongCredentialBasicAuthControllerName = "KongCredentialBasicAuth" //nolint:gosec - // KongKongCredentialAPIKeyControllerName is the name of the KongCredentialAPIKey controller. - KongKongCredentialAPIKeyControllerName = "KongCredentialAPIKey" //nolint:gosec + // KongCredentialBasicAuthControllerName is the name of the KongCredentialBasicAuth controller. + KongCredentialBasicAuthControllerName = "KongCredentialBasicAuth" //nolint:gosec + // KongCredentialAPIKeyControllerName is the name of the KongCredentialAPIKey controller. + KongCredentialAPIKeyControllerName = "KongCredentialAPIKey" //nolint:gosec // KongCACertificateControllerName is the name of the KongCACertificate controller. KongCACertificateControllerName = "KongCACertificate" // KongCertificateControllerName is the name of the KongCertificate controller. @@ -436,7 +436,7 @@ func SetupControllers(mgr manager.Manager, c *Config) (map[string]ControllerDef, konnect.WithKonnectEntitySyncPeriod[configurationv1alpha1.KongPluginBinding](c.KonnectSyncPeriod), ), }, - KongKongCredentialBasicAuthControllerName: { + KongCredentialBasicAuthControllerName: { Enabled: c.KonnectControllersEnabled, Controller: konnect.NewKonnectEntityReconciler( sdkFactory, @@ -445,7 +445,7 @@ func SetupControllers(mgr manager.Manager, c *Config) (map[string]ControllerDef, konnect.WithKonnectEntitySyncPeriod[configurationv1alpha1.KongCredentialBasicAuth](c.KonnectSyncPeriod), ), }, - KongKongCredentialAPIKeyControllerName: { + KongCredentialAPIKeyControllerName: { Enabled: c.KonnectControllersEnabled, Controller: konnect.NewKonnectEntityReconciler( sdkFactory,