From a8465d59fb2798a95403a0e25f096b954906339c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Ma=C5=82ek?= Date: Thu, 26 Sep 2024 19:05:44 +0200 Subject: [PATCH] refactor(konnect): add generic watch func for objects referring directly to KonnectGatewayControlPlane (#658) --- ...conciler_generic_pluginbindingfinalizer.go | 2 +- controller/konnect/watch.go | 27 +++++++++++++++++++ controller/konnect/watch_kongcacertificate.go | 18 +------------ controller/konnect/watch_kongcertificate.go | 18 +------------ controller/konnect/watch_kongconsumer.go | 21 +-------------- controller/konnect/watch_kongconsumergroup.go | 21 +-------------- controller/konnect/watch_kongkey.go | 18 +------------ controller/konnect/watch_kongpluginbinding.go | 25 +---------------- controller/konnect/watch_kongservice.go | 21 +-------------- controller/konnect/watch_kongupstream.go | 23 +--------------- controller/konnect/watch_kongvault.go | 22 +-------------- 11 files changed, 37 insertions(+), 179 deletions(-) diff --git a/controller/konnect/reconciler_generic_pluginbindingfinalizer.go b/controller/konnect/reconciler_generic_pluginbindingfinalizer.go index 547f17350..9cb745383 100644 --- a/controller/konnect/reconciler_generic_pluginbindingfinalizer.go +++ b/controller/konnect/reconciler_generic_pluginbindingfinalizer.go @@ -207,7 +207,7 @@ func (r *KonnectEntityPluginBindingFinalizerReconciler[T, TEnt]) setControllerBu b. For(&configurationv1alpha1.KongService{}, builder.WithPredicates( - predicate.NewPredicateFuncs(kongServiceRefersToKonnectGatewayControlPlane), + predicate.NewPredicateFuncs(objRefersToKonnectGatewayControlPlane[configurationv1alpha1.KongService]), kongPluginsAnnotationChangedPredicate, ), ). diff --git a/controller/konnect/watch.go b/controller/konnect/watch.go index e34b95eab..d27f2df30 100644 --- a/controller/konnect/watch.go +++ b/controller/konnect/watch.go @@ -1,12 +1,16 @@ package konnect import ( + "context" "fmt" + "reflect" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" + ctrllog "sigs.k8s.io/controller-runtime/pkg/log" "github.com/kong/gateway-operator/controller/konnect/constraints" + operatorerrors "github.com/kong/gateway-operator/internal/errors" configurationv1 "github.com/kong/kubernetes-configuration/api/configuration/v1" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" @@ -56,3 +60,26 @@ func ReconciliationWatchOptionsForEntity[ panic(fmt.Sprintf("unsupported entity type %T", ent)) } } + +// objRefersToKonnectGatewayControlPlane returns true if the object +// refers to a KonnectGatewayControlPlane. +func objRefersToKonnectGatewayControlPlane[ + T constraints.SupportedKonnectEntityType, + TEnt constraints.EntityType[T], +](obj client.Object) bool { + ent, ok := obj.(TEnt) + if !ok { + ctrllog.FromContext(context.Background()).Error( + operatorerrors.ErrUnexpectedObject, + "failed to run predicate function", + "expected", constraints.EntityTypeName[T](), "found", reflect.TypeOf(obj), + ) + return false + } + + cpRef, ok := getControlPlaneRef(ent).Get() + if !ok { + return false + } + return cpRef.Type == configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef +} diff --git a/controller/konnect/watch_kongcacertificate.go b/controller/konnect/watch_kongcacertificate.go index d57f9a30f..8f8e4f425 100644 --- a/controller/konnect/watch_kongcacertificate.go +++ b/controller/konnect/watch_kongcacertificate.go @@ -3,7 +3,6 @@ package konnect import ( "context" "fmt" - "reflect" "k8s.io/apimachinery/pkg/types" ctrl "sigs.k8s.io/controller-runtime" @@ -14,7 +13,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" - operatorerrors "github.com/kong/gateway-operator/internal/errors" "github.com/kong/gateway-operator/modules/manager/logging" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" @@ -27,7 +25,7 @@ func KongCACertificateReconciliationWatchOptions(cl client.Client) []func(*ctrl. func(b *ctrl.Builder) *ctrl.Builder { return b.For(&configurationv1alpha1.KongCACertificate{}, builder.WithPredicates( - predicate.NewPredicateFuncs(kongCACertificateRefersToKonnectControlPlane), + predicate.NewPredicateFuncs(objRefersToKonnectGatewayControlPlane[configurationv1alpha1.KongCACertificate]), ), ) }, @@ -50,20 +48,6 @@ func KongCACertificateReconciliationWatchOptions(cl client.Client) []func(*ctrl. } } -func kongCACertificateRefersToKonnectControlPlane(obj client.Object) bool { - kongCACertificate, ok := obj.(*configurationv1alpha1.KongCACertificate) - if !ok { - ctrllog.FromContext(context.Background()).Error( - operatorerrors.ErrUnexpectedObject, - "failed to run predicate function", - "expected", "KongCACertificate", "found", reflect.TypeOf(obj), - ) - return false - } - return kongCACertificate.Spec.ControlPlaneRef != nil && - kongCACertificate.Spec.ControlPlaneRef.Type == configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef -} - func enqueueKongCACertificateForKonnectAPIAuthConfiguration(cl client.Client) handler.MapFunc { return func(ctx context.Context, obj client.Object) []reconcile.Request { auth, ok := obj.(*konnectv1alpha1.KonnectAPIAuthConfiguration) diff --git a/controller/konnect/watch_kongcertificate.go b/controller/konnect/watch_kongcertificate.go index cc67f04af..2a4e6a4d0 100644 --- a/controller/konnect/watch_kongcertificate.go +++ b/controller/konnect/watch_kongcertificate.go @@ -3,7 +3,6 @@ package konnect import ( "context" "fmt" - "reflect" "k8s.io/apimachinery/pkg/types" ctrl "sigs.k8s.io/controller-runtime" @@ -14,7 +13,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" - operatorerrors "github.com/kong/gateway-operator/internal/errors" "github.com/kong/gateway-operator/modules/manager/logging" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" @@ -27,7 +25,7 @@ func KongCertificateReconciliationWatchOptions(cl client.Client) []func(*ctrl.Bu func(b *ctrl.Builder) *ctrl.Builder { return b.For(&configurationv1alpha1.KongCertificate{}, builder.WithPredicates( - predicate.NewPredicateFuncs(kongCertificateRefersToKonnectControlPlane), + predicate.NewPredicateFuncs(objRefersToKonnectGatewayControlPlane[configurationv1alpha1.KongCertificate]), ), ) }, @@ -50,20 +48,6 @@ func KongCertificateReconciliationWatchOptions(cl client.Client) []func(*ctrl.Bu } } -func kongCertificateRefersToKonnectControlPlane(obj client.Object) bool { - kongCertificate, ok := obj.(*configurationv1alpha1.KongCertificate) - if !ok { - ctrllog.FromContext(context.Background()).Error( - operatorerrors.ErrUnexpectedObject, - "failed to run predicate function", - "expected", "KongCertificate", "found", reflect.TypeOf(obj), - ) - return false - } - return kongCertificate.Spec.ControlPlaneRef != nil && - kongCertificate.Spec.ControlPlaneRef.Type == configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef -} - func enqueueKongCertificateForKonnectAPIAuthConfiguration(cl client.Client) handler.MapFunc { return func(ctx context.Context, obj client.Object) []reconcile.Request { auth, ok := obj.(*konnectv1alpha1.KonnectAPIAuthConfiguration) diff --git a/controller/konnect/watch_kongconsumer.go b/controller/konnect/watch_kongconsumer.go index 7b4866f77..688fd3409 100644 --- a/controller/konnect/watch_kongconsumer.go +++ b/controller/konnect/watch_kongconsumer.go @@ -3,7 +3,6 @@ package konnect import ( "context" "fmt" - "reflect" "github.com/samber/lo" "k8s.io/apimachinery/pkg/types" @@ -15,7 +14,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" - operatorerrors "github.com/kong/gateway-operator/internal/errors" "github.com/kong/gateway-operator/modules/manager/logging" configurationv1 "github.com/kong/kubernetes-configuration/api/configuration/v1" @@ -41,7 +39,7 @@ func KongConsumerReconciliationWatchOptions( func(b *ctrl.Builder) *ctrl.Builder { return b.For(&configurationv1.KongConsumer{}, builder.WithPredicates( - predicate.NewPredicateFuncs(kongConsumerRefersToKonnectGatewayControlPlane), + predicate.NewPredicateFuncs(objRefersToKonnectGatewayControlPlane[configurationv1.KongConsumer]), ), ) }, @@ -72,23 +70,6 @@ func KongConsumerReconciliationWatchOptions( } } -// kongConsumerRefersToKonnectGatewayControlPlane returns true if the KongConsumer -// refers to a KonnectGatewayControlPlane. -func kongConsumerRefersToKonnectGatewayControlPlane(obj client.Object) bool { - kongConsumer, ok := obj.(*configurationv1.KongConsumer) - if !ok { - ctrllog.FromContext(context.Background()).Error( - operatorerrors.ErrUnexpectedObject, - "failed to run predicate function", - "expected", "KongConsumer", "found", reflect.TypeOf(obj), - ) - return false - } - - cpRef := kongConsumer.Spec.ControlPlaneRef - return cpRef != nil && cpRef.Type == configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef -} - func enqueueKongConsumerForKonnectAPIAuthConfiguration( 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 a58317026..a8a8031d4 100644 --- a/controller/konnect/watch_kongconsumergroup.go +++ b/controller/konnect/watch_kongconsumergroup.go @@ -3,7 +3,6 @@ package konnect import ( "context" "fmt" - "reflect" "k8s.io/apimachinery/pkg/types" ctrl "sigs.k8s.io/controller-runtime" @@ -14,7 +13,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" - operatorerrors "github.com/kong/gateway-operator/internal/errors" "github.com/kong/gateway-operator/modules/manager/logging" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" @@ -39,7 +37,7 @@ func KongConsumerGroupReconciliationWatchOptions( func(b *ctrl.Builder) *ctrl.Builder { return b.For(&configurationv1beta1.KongConsumerGroup{}, builder.WithPredicates( - predicate.NewPredicateFuncs(kongConsumerGroupRefersToKonnectGatewayControlPlane), + predicate.NewPredicateFuncs(objRefersToKonnectGatewayControlPlane[configurationv1beta1.KongConsumerGroup]), ), ) }, @@ -62,23 +60,6 @@ func KongConsumerGroupReconciliationWatchOptions( } } -// kongConsumerGroupRefersToKonnectGatewayControlPlane returns true if the KongConsumerGroup -// refers to a KonnectGatewayControlPlane. -func kongConsumerGroupRefersToKonnectGatewayControlPlane(obj client.Object) bool { - kongConsumerGroup, ok := obj.(*configurationv1beta1.KongConsumerGroup) - if !ok { - ctrllog.FromContext(context.Background()).Error( - operatorerrors.ErrUnexpectedObject, - "failed to run predicate function", - "expected", "KongConsumerGroup", "found", reflect.TypeOf(obj), - ) - return false - } - - cpRef := kongConsumerGroup.Spec.ControlPlaneRef - return cpRef != nil && cpRef.Type == configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef -} - func enqueueKongConsumerGroupForKonnectAPIAuthConfiguration( 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 a99bdcc47..5d7ea117c 100644 --- a/controller/konnect/watch_kongkey.go +++ b/controller/konnect/watch_kongkey.go @@ -3,7 +3,6 @@ package konnect import ( "context" "fmt" - "reflect" "k8s.io/apimachinery/pkg/types" ctrl "sigs.k8s.io/controller-runtime" @@ -14,7 +13,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" - operatorerrors "github.com/kong/gateway-operator/internal/errors" "github.com/kong/gateway-operator/modules/manager/logging" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" @@ -27,7 +25,7 @@ func KongKeyReconciliationWatchOptions(cl client.Client) []func(*ctrl.Builder) * func(b *ctrl.Builder) *ctrl.Builder { return b.For(&configurationv1alpha1.KongKey{}, builder.WithPredicates( - predicate.NewPredicateFuncs(kongKeyRefersToKonnectControlPlane), + predicate.NewPredicateFuncs(objRefersToKonnectGatewayControlPlane[configurationv1alpha1.KongKey]), ), ) }, @@ -50,20 +48,6 @@ func KongKeyReconciliationWatchOptions(cl client.Client) []func(*ctrl.Builder) * } } -func kongKeyRefersToKonnectControlPlane(obj client.Object) bool { - kongKey, ok := obj.(*configurationv1alpha1.KongKey) - if !ok { - ctrllog.FromContext(context.Background()).Error( - operatorerrors.ErrUnexpectedObject, - "failed to run predicate function", - "expected", "KongKey", "found", reflect.TypeOf(obj), - ) - return false - } - return kongKey.Spec.ControlPlaneRef != nil && - kongKey.Spec.ControlPlaneRef.Type == configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef -} - func enqueueKongKeyForKonnectAPIAuthConfiguration(cl client.Client) handler.MapFunc { return func(ctx context.Context, obj client.Object) []reconcile.Request { auth, ok := obj.(*konnectv1alpha1.KonnectAPIAuthConfiguration) diff --git a/controller/konnect/watch_kongpluginbinding.go b/controller/konnect/watch_kongpluginbinding.go index 6dd271499..7b0369f3b 100644 --- a/controller/konnect/watch_kongpluginbinding.go +++ b/controller/konnect/watch_kongpluginbinding.go @@ -3,7 +3,6 @@ package konnect import ( "context" "fmt" - "reflect" "github.com/samber/lo" "k8s.io/apimachinery/pkg/types" @@ -15,7 +14,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" - operatorerrors "github.com/kong/gateway-operator/internal/errors" "github.com/kong/gateway-operator/modules/manager/logging" configurationv1 "github.com/kong/kubernetes-configuration/api/configuration/v1" @@ -45,7 +43,7 @@ func KongPluginBindingReconciliationWatchOptions( func(b *ctrl.Builder) *ctrl.Builder { return b.For(&configurationv1alpha1.KongPluginBinding{}, builder.WithPredicates( - predicate.NewPredicateFuncs(kongPluginBindingRefersToKonnectGatewayControlPlane), + predicate.NewPredicateFuncs(objRefersToKonnectGatewayControlPlane[configurationv1alpha1.KongPluginBinding]), ), ) }, @@ -108,27 +106,6 @@ func KongPluginBindingReconciliationWatchOptions( } } -// ----------------------------------------------------------------------------- -// KongPluginBinding reconciler - Watch Predicates -// ----------------------------------------------------------------------------- - -// kongPluginBindingRefersToKonnectGatewayControlPlane returns true if the KongPluginBinding -// refers to a KonnectGatewayControlPlane. -func kongPluginBindingRefersToKonnectGatewayControlPlane(obj client.Object) bool { - kongPB, ok := obj.(*configurationv1alpha1.KongPluginBinding) - if !ok { - ctrllog.FromContext(context.Background()).Error( - operatorerrors.ErrUnexpectedObject, - "failed to run predicate function", - "expected", "KongPluginBinding", "found", reflect.TypeOf(obj), - ) - return false - } - - cpRef := kongPB.Spec.ControlPlaneRef - return cpRef != nil && cpRef.Type == configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef -} - // ----------------------------------------------------------------------------- // KongPluginBinding reconciler - Watch Mappers // ----------------------------------------------------------------------------- diff --git a/controller/konnect/watch_kongservice.go b/controller/konnect/watch_kongservice.go index 258e6ff04..0e6ee70ee 100644 --- a/controller/konnect/watch_kongservice.go +++ b/controller/konnect/watch_kongservice.go @@ -3,7 +3,6 @@ package konnect import ( "context" "fmt" - "reflect" "k8s.io/apimachinery/pkg/types" ctrl "sigs.k8s.io/controller-runtime" @@ -14,7 +13,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" - operatorerrors "github.com/kong/gateway-operator/internal/errors" "github.com/kong/gateway-operator/modules/manager/logging" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" @@ -38,7 +36,7 @@ func KongServiceReconciliationWatchOptions( func(b *ctrl.Builder) *ctrl.Builder { return b.For(&configurationv1alpha1.KongService{}, builder.WithPredicates( - predicate.NewPredicateFuncs(kongServiceRefersToKonnectGatewayControlPlane), + predicate.NewPredicateFuncs(objRefersToKonnectGatewayControlPlane[configurationv1alpha1.KongService]), ), ) }, @@ -61,23 +59,6 @@ func KongServiceReconciliationWatchOptions( } } -// kongServiceRefersToKonnectGatewayControlPlane returns true if the KongService -// refers to a KonnectGatewayControlPlane. -func kongServiceRefersToKonnectGatewayControlPlane(obj client.Object) bool { - kongSvc, ok := obj.(*configurationv1alpha1.KongService) - if !ok { - ctrllog.FromContext(context.Background()).Error( - operatorerrors.ErrUnexpectedObject, - "failed to run predicate function", - "expected", "KongService", "found", reflect.TypeOf(obj), - ) - return false - } - - cpRef := kongSvc.Spec.ControlPlaneRef - return cpRef != nil && cpRef.Type == configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef -} - func enqueueKongServiceForKonnectAPIAuthConfiguration( cl client.Client, ) func(ctx context.Context, obj client.Object) []reconcile.Request { diff --git a/controller/konnect/watch_kongupstream.go b/controller/konnect/watch_kongupstream.go index e6b1d4eaf..291508f64 100644 --- a/controller/konnect/watch_kongupstream.go +++ b/controller/konnect/watch_kongupstream.go @@ -3,7 +3,6 @@ package konnect import ( "context" "fmt" - "reflect" "k8s.io/apimachinery/pkg/types" ctrl "sigs.k8s.io/controller-runtime" @@ -14,7 +13,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" - operatorerrors "github.com/kong/gateway-operator/internal/errors" "github.com/kong/gateway-operator/modules/manager/logging" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" @@ -38,7 +36,7 @@ func KongUpstreamReconciliationWatchOptions( func(b *ctrl.Builder) *ctrl.Builder { return b.For(&configurationv1alpha1.KongUpstream{}, builder.WithPredicates( - predicate.NewPredicateFuncs(kongUpstreamRefersToKonnectGatewayControlPlane()), + predicate.NewPredicateFuncs(objRefersToKonnectGatewayControlPlane[configurationv1alpha1.KongUpstream]), ), ) }, @@ -61,25 +59,6 @@ func KongUpstreamReconciliationWatchOptions( } } -// kongUpstreamRefersToKonnectGatewayControlPlane returns true if the KongUpstream -// refers to a KonnectGatewayControlPlane. -func kongUpstreamRefersToKonnectGatewayControlPlane() func(obj client.Object) bool { - return func(obj client.Object) bool { - kongUpstream, ok := obj.(*configurationv1alpha1.KongUpstream) - if !ok { - ctrllog.FromContext(context.Background()).Error( - operatorerrors.ErrUnexpectedObject, - "failed to run predicate function", - "expected", "KongUpstream", "found", reflect.TypeOf(obj), - ) - return false - } - - cpRef := kongUpstream.Spec.ControlPlaneRef - return cpRef != nil && cpRef.Type == configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef - } -} - func enqueueKongUpstreamForKonnectAPIAuthConfiguration( cl client.Client, ) func(ctx context.Context, obj client.Object) []reconcile.Request { diff --git a/controller/konnect/watch_kongvault.go b/controller/konnect/watch_kongvault.go index cb468f200..9a11dc875 100644 --- a/controller/konnect/watch_kongvault.go +++ b/controller/konnect/watch_kongvault.go @@ -3,7 +3,6 @@ package konnect import ( "context" "fmt" - "reflect" "k8s.io/apimachinery/pkg/types" ctrl "sigs.k8s.io/controller-runtime" @@ -14,7 +13,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" - operatorerrors "github.com/kong/gateway-operator/internal/errors" "github.com/kong/gateway-operator/modules/manager/logging" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" @@ -27,7 +25,7 @@ func KongVaultReconciliationWatchOptions(cl client.Client) []func(*ctrl.Builder) func(b *ctrl.Builder) *ctrl.Builder { return b.For(&configurationv1alpha1.KongVault{}, builder.WithPredicates( - predicate.NewPredicateFuncs(kongVaultRefersToKonnectGatewayControlPlane()), + predicate.NewPredicateFuncs(objRefersToKonnectGatewayControlPlane[configurationv1alpha1.KongVault]), ), ) }, @@ -50,23 +48,6 @@ func KongVaultReconciliationWatchOptions(cl client.Client) []func(*ctrl.Builder) } } -func kongVaultRefersToKonnectGatewayControlPlane() func(obj client.Object) bool { - return func(obj client.Object) bool { - kongVault, ok := obj.(*configurationv1alpha1.KongVault) - if !ok { - ctrllog.FromContext(context.Background()).Error( - operatorerrors.ErrUnexpectedObject, - "failed to run predicate function", - "expected", "KongVault", "found", reflect.TypeOf(obj), - ) - return false - } - - cpRef := kongVault.Spec.ControlPlaneRef - return cpRef != nil && cpRef.Type == configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef - } -} - // enqueueKongVaultForKonnectAPIAuthConfiguration enqueues KongVaults // when KonnectAPIAuthConfiguration which is associated with the Konnect Control plane referenced by the KongVault. func enqueueKongVaultForKonnectAPIAuthConfiguration( @@ -197,6 +178,5 @@ func enqueueKongVaultForKonnectGatewayControlPlane( } return ret - } }