From 519cfb7f81ed3f8be3b37b5d877380f86ee80bfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Ma=C5=82ek?= Date: Wed, 2 Oct 2024 12:50:09 +0200 Subject: [PATCH] refactor(konnect): add indices for objects that reference KonnectGatewayControlPlane --- controller/konnect/index.go | 21 +++++++++++ controller/konnect/index_kongconsumer.go | 16 ++++++++ controller/konnect/index_kongservice.go | 16 ++++++++ controller/konnect/index_kongtarget.go | 32 ++++++++++++++++ controller/konnect/index_kongupstream.go | 32 ++++++++++++++++ controller/konnect/watch_kongconsumer.go | 47 ++++-------------------- controller/konnect/watch_kongservice.go | 46 ++++------------------- controller/konnect/watch_kongtarget.go | 21 ++++------- controller/konnect/watch_kongupstream.go | 46 ++++------------------- modules/manager/controller_setup.go | 12 ++++++ 10 files changed, 158 insertions(+), 131 deletions(-) create mode 100644 controller/konnect/index_kongtarget.go create mode 100644 controller/konnect/index_kongupstream.go diff --git a/controller/konnect/index.go b/controller/konnect/index.go index 614bdeab4..471a95c8b 100644 --- a/controller/konnect/index.go +++ b/controller/konnect/index.go @@ -2,6 +2,8 @@ package konnect import ( "sigs.k8s.io/controller-runtime/pkg/client" + + "github.com/kong/gateway-operator/controller/konnect/constraints" ) // ReconciliationIndexOption contains required options of index for a kind of object required for reconciliation. @@ -10,3 +12,22 @@ type ReconciliationIndexOption struct { IndexField string ExtractValue client.IndexerFunc } + +// controlPlaneKonnectNamespacedRefRefAsSlice returns a slice of strings representing +// the KonnectNamespacedRef of the object. +func controlPlaneKonnectNamespacedRefRefAsSlice[ + T constraints.SupportedKonnectEntityType, + TEnt constraints.EntityType[T], +](ent TEnt) []string { + cpRef, ok := controlPlaneIsRefKonnectNamespacedRef(ent) + if !ok { + return nil + } + + konnectRef := cpRef.KonnectNamespacedRef + if konnectRef == nil { + return nil + } + + return []string{konnectRef.Namespace + "/" + konnectRef.Name} +} diff --git a/controller/konnect/index_kongconsumer.go b/controller/konnect/index_kongconsumer.go index 80c3a9683..01cc3cd1c 100644 --- a/controller/konnect/index_kongconsumer.go +++ b/controller/konnect/index_kongconsumer.go @@ -13,6 +13,8 @@ const ( IndexFieldKongConsumerOnKongConsumerGroup = "consumerGroupRef" // IndexFieldKongConsumerOnPlugin is the index field for KongConsumer -> KongPlugin. IndexFieldKongConsumerOnPlugin = "consumerPluginRef" + // IndexFieldKongConsumerOnKonnectGatewayControlPlane is the index field for KongConsumer -> KonnectGatewayControlPlane. + IndexFieldKongConsumerOnKonnectGatewayControlPlane = "consumerKonnectGatewayControlPlaneRef" ) // IndexOptionsForKongConsumer returns required Index options for KongConsumer reconciler. @@ -28,6 +30,11 @@ func IndexOptionsForKongConsumer() []ReconciliationIndexOption { IndexField: IndexFieldKongConsumerOnPlugin, ExtractValue: kongConsumerReferencesKongPluginsViaAnnotation, }, + { + IndexObject: &configurationv1.KongConsumer{}, + IndexField: IndexFieldKongConsumerOnKonnectGatewayControlPlane, + ExtractValue: kongConsumerReferencesKonnectGatewayControlPlane, + }, } } @@ -46,3 +53,12 @@ func kongConsumerReferencesKongPluginsViaAnnotation(object client.Object) []stri } return annotations.ExtractPluginsWithNamespaces(consumer) } + +func kongConsumerReferencesKonnectGatewayControlPlane(object client.Object) []string { + consumer, ok := object.(*configurationv1.KongConsumer) + if !ok { + return nil + } + + return controlPlaneKonnectNamespacedRefRefAsSlice(consumer) +} diff --git a/controller/konnect/index_kongservice.go b/controller/konnect/index_kongservice.go index a1a75b053..f934893b1 100644 --- a/controller/konnect/index_kongservice.go +++ b/controller/konnect/index_kongservice.go @@ -11,6 +11,8 @@ import ( const ( // IndexFieldKongServiceOnReferencedPluginNames is the index field for KongService -> KongPlugin. IndexFieldKongServiceOnReferencedPluginNames = "kongServiceKongPluginRef" + // IndexFieldKongServiceOnKonnectGatewayControlPlane is the index field for KongService -> KonnectGatewayControlPlane. + IndexFieldKongServiceOnKonnectGatewayControlPlane = "kongServiceKonnectGatewayControlPlaneRef" ) // IndexOptionsForKongService returns required Index options for KongService reconciler. @@ -21,6 +23,11 @@ func IndexOptionsForKongService() []ReconciliationIndexOption { IndexField: IndexFieldKongServiceOnReferencedPluginNames, ExtractValue: kongServiceUsesPlugins, }, + { + IndexObject: &configurationv1alpha1.KongService{}, + IndexField: IndexFieldKongServiceOnKonnectGatewayControlPlane, + ExtractValue: kongServiceReferencesKonnectGatewayControlPlane, + }, } } @@ -32,3 +39,12 @@ func kongServiceUsesPlugins(object client.Object) []string { return annotations.ExtractPluginsWithNamespaces(svc) } + +func kongServiceReferencesKonnectGatewayControlPlane(object client.Object) []string { + svc, ok := object.(*configurationv1alpha1.KongService) + if !ok { + return nil + } + + return controlPlaneKonnectNamespacedRefRefAsSlice(svc) +} diff --git a/controller/konnect/index_kongtarget.go b/controller/konnect/index_kongtarget.go new file mode 100644 index 000000000..6a3a55b68 --- /dev/null +++ b/controller/konnect/index_kongtarget.go @@ -0,0 +1,32 @@ +package konnect + +import ( + "sigs.k8s.io/controller-runtime/pkg/client" + + configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" +) + +const ( + // IndexFieldKongTargetOnReferencedUpstream is the index field for KongTarget -> KongUpstream. + IndexFieldKongTargetOnReferencedUpstream = "kongTargetUpstreamRef" +) + +// IndexOptionsForKongTarget returns required Index options for KongTarget reconciler. +func IndexOptionsForKongTarget() []ReconciliationIndexOption { + return []ReconciliationIndexOption{ + { + IndexObject: &configurationv1alpha1.KongTarget{}, + IndexField: IndexFieldKongTargetOnReferencedUpstream, + ExtractValue: kongTargetReferencesKongUpstream, + }, + } +} + +func kongTargetReferencesKongUpstream(object client.Object) []string { + target, ok := object.(*configurationv1alpha1.KongTarget) + if !ok { + return nil + } + + return []string{target.Spec.UpstreamRef.Name} +} diff --git a/controller/konnect/index_kongupstream.go b/controller/konnect/index_kongupstream.go new file mode 100644 index 000000000..26bd63801 --- /dev/null +++ b/controller/konnect/index_kongupstream.go @@ -0,0 +1,32 @@ +package konnect + +import ( + "sigs.k8s.io/controller-runtime/pkg/client" + + configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" +) + +const ( + // IndexFieldKongUpstreamOnKonnectGatewayControlPlane is the index field for KongUpstream -> KonnectGatewayControlPlane. + IndexFieldKongUpstreamOnKonnectGatewayControlPlane = "kongUpstreamKonnectGatewayControlPlaneRef" +) + +// IndexOptionsForKongUpstream returns required Index options for KongUpstream reconciler. +func IndexOptionsForKongUpstream() []ReconciliationIndexOption { + return []ReconciliationIndexOption{ + { + IndexObject: &configurationv1alpha1.KongUpstream{}, + IndexField: IndexFieldKongUpstreamOnKonnectGatewayControlPlane, + ExtractValue: kongUpstreamReferencesKonnectGatewayControlPlane, + }, + } +} + +func kongUpstreamReferencesKonnectGatewayControlPlane(object client.Object) []string { + upstream, ok := object.(*configurationv1alpha1.KongUpstream) + if !ok { + return nil + } + + return controlPlaneKonnectNamespacedRefRefAsSlice(upstream) +} diff --git a/controller/konnect/watch_kongconsumer.go b/controller/konnect/watch_kongconsumer.go index 99c025e10..31dacfe1f 100644 --- a/controller/konnect/watch_kongconsumer.go +++ b/controller/konnect/watch_kongconsumer.go @@ -153,50 +153,17 @@ func enqueueKongConsumerForKonnectGatewayControlPlane( return nil } var l configurationv1.KongConsumerList - if err := cl.List(ctx, &l, &client.ListOptions{ + if err := cl.List(ctx, &l, // TODO: change this when cross namespace refs are allowed. - Namespace: cp.GetNamespace(), - }); err != nil { + client.InNamespace(cp.GetNamespace()), + client.MatchingFields{ + IndexFieldKongConsumerOnKonnectGatewayControlPlane: cp.Namespace + "/" + cp.Name, + }, + ); err != nil { return nil } - var ret []reconcile.Request - for _, consumer := range l.Items { - cpRef, ok := getControlPlaneRef(&consumer).Get() - if !ok { - continue - } - switch cpRef.Type { - case configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef: - // TODO: change this when cross namespace refs are allowed. - if cpRef.KonnectNamespacedRef.Name != cp.Name { - continue - } - - ret = append(ret, reconcile.Request{ - NamespacedName: types.NamespacedName{ - Namespace: consumer.Namespace, - Name: consumer.Name, - }, - }) - - case configurationv1alpha1.ControlPlaneRefKonnectID: - ctrllog.FromContext(ctx).Error( - fmt.Errorf("unimplemented ControlPlaneRef type %q", cpRef.Type), - "unimplemented ControlPlaneRef for KongConsumer", - "KongConsumer", consumer, "refType", cpRef.Type, - ) - continue - - default: - ctrllog.FromContext(ctx).V(logging.DebugLevel.Value()).Info( - "unsupported ControlPlaneRef for KongConsumer", - "KongConsumer", consumer, "refType", cpRef.Type, - ) - continue - } - } - return ret + return objectListToReconcileRequests(l.Items) } } diff --git a/controller/konnect/watch_kongservice.go b/controller/konnect/watch_kongservice.go index 0e6ee70ee..c8e60b1f7 100644 --- a/controller/konnect/watch_kongservice.go +++ b/controller/konnect/watch_kongservice.go @@ -143,48 +143,16 @@ func enqueueKongServiceForKonnectGatewayControlPlane( return nil } var l configurationv1alpha1.KongServiceList - if err := cl.List(ctx, &l, &client.ListOptions{ + if err := cl.List(ctx, &l, // TODO: change this when cross namespace refs are allowed. - Namespace: cp.GetNamespace(), - }); err != nil { + client.InNamespace(cp.GetNamespace()), + client.MatchingFields{ + IndexFieldKongServiceOnKonnectGatewayControlPlane: cp.Namespace + "/" + cp.Name, + }, + ); err != nil { return nil } - var ret []reconcile.Request - for _, svc := range l.Items { - if svc.Spec.ControlPlaneRef == nil { - continue - } - switch svc.Spec.ControlPlaneRef.Type { - case configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef: - // TODO: change this when cross namespace refs are allowed. - if svc.Spec.ControlPlaneRef.KonnectNamespacedRef.Name != cp.Name { - continue - } - - ret = append(ret, reconcile.Request{ - NamespacedName: types.NamespacedName{ - Namespace: svc.Namespace, - Name: svc.Name, - }, - }) - - case configurationv1alpha1.ControlPlaneRefKonnectID: - ctrllog.FromContext(ctx).Error( - fmt.Errorf("unimplemented ControlPlaneRef type %q", svc.Spec.ControlPlaneRef.Type), - "unimplemented ControlPlaneRef for KongService", - "KongService", svc, "refType", svc.Spec.ControlPlaneRef.Type, - ) - continue - - default: - ctrllog.FromContext(ctx).V(logging.DebugLevel.Value()).Info( - "unsupported ControlPlaneRef for KongService", - "KongService", svc, "refType", svc.Spec.ControlPlaneRef.Type, - ) - continue - } - } - return ret + return objectListToReconcileRequests(l.Items) } } diff --git a/controller/konnect/watch_kongtarget.go b/controller/konnect/watch_kongtarget.go index f4b600606..8975e72cf 100644 --- a/controller/konnect/watch_kongtarget.go +++ b/controller/konnect/watch_kongtarget.go @@ -77,22 +77,17 @@ func enqueueKongTargetForKongUpstream(cl client.Client, return nil } - var targetList configurationv1alpha1.KongTargetList - if err := cl.List(ctx, &targetList, &client.ListOptions{ + var l configurationv1alpha1.KongTargetList + if err := cl.List(ctx, &l, // TODO: change this when cross namespace refs are allowed. - Namespace: kongUpstream.GetNamespace(), - }); err != nil { + client.InNamespace(kongUpstream.GetNamespace()), + client.MatchingFields{ + IndexFieldKongTargetOnReferencedUpstream: kongUpstream.Name, + }, + ); err != nil { return nil } - var ret []reconcile.Request - for _, target := range targetList.Items { - if target.Spec.UpstreamRef.Name == kongUpstream.Name { - ret = append(ret, reconcile.Request{ - NamespacedName: client.ObjectKeyFromObject(&target), - }) - } - } - return ret + return objectListToReconcileRequests(l.Items) } } diff --git a/controller/konnect/watch_kongupstream.go b/controller/konnect/watch_kongupstream.go index 291508f64..afd10a81d 100644 --- a/controller/konnect/watch_kongupstream.go +++ b/controller/konnect/watch_kongupstream.go @@ -143,48 +143,16 @@ func enqueueKongUpstreamForKonnectGatewayControlPlane( return nil } var l configurationv1alpha1.KongUpstreamList - if err := cl.List(ctx, &l, &client.ListOptions{ + if err := cl.List(ctx, &l, // TODO: change this when cross namespace refs are allowed. - Namespace: cp.GetNamespace(), - }); err != nil { + client.InNamespace(cp.GetNamespace()), + client.MatchingFields{ + IndexFieldKongUpstreamOnKonnectGatewayControlPlane: cp.Namespace + "/" + cp.Name, + }, + ); err != nil { return nil } - var ret []reconcile.Request - for _, upstream := range l.Items { - if upstream.Spec.ControlPlaneRef == nil { - continue - } - switch upstream.Spec.ControlPlaneRef.Type { - case configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef: - // TODO: change this when cross namespace refs are allowed. - if upstream.Spec.ControlPlaneRef.KonnectNamespacedRef.Name != cp.Name { - continue - } - - ret = append(ret, reconcile.Request{ - NamespacedName: types.NamespacedName{ - Namespace: upstream.Namespace, - Name: upstream.Name, - }, - }) - - case configurationv1alpha1.ControlPlaneRefKonnectID: - ctrllog.FromContext(ctx).Error( - fmt.Errorf("unimplemented ControlPlaneRef type %q", upstream.Spec.ControlPlaneRef.Type), - "unimplemented ControlPlaneRef for KongUpstream", - "KongUpstream", upstream, "refType", upstream.Spec.ControlPlaneRef.Type, - ) - continue - - default: - ctrllog.FromContext(ctx).V(logging.DebugLevel.Value()).Info( - "unsupported ControlPlaneRef for KongUpstream", - "KongUpstream", upstream, "refType", upstream.Spec.ControlPlaneRef.Type, - ) - continue - } - } - return ret + return objectListToReconcileRequests(l.Items) } } diff --git a/modules/manager/controller_setup.go b/modules/manager/controller_setup.go index a53a0b767..f343ac93e 100644 --- a/modules/manager/controller_setup.go +++ b/modules/manager/controller_setup.go @@ -588,6 +588,10 @@ func SetupCacheIndicesForKonnectTypes(ctx context.Context, mgr manager.Manager, Object: &configurationv1alpha1.KongCredentialJWT{}, IndexOptions: konnect.IndexOptionsForCredentialsJWT(), }, + { + Object: &configurationv1alpha1.KongCredentialAPIKey{}, + IndexOptions: konnect.IndexOptionsForCredentialsAPIKey(), + }, { Object: &configurationv1.KongConsumer{}, IndexOptions: konnect.IndexOptionsForKongConsumer(), @@ -600,6 +604,14 @@ func SetupCacheIndicesForKonnectTypes(ctx context.Context, mgr manager.Manager, Object: &configurationv1alpha1.KongRoute{}, IndexOptions: konnect.IndexOptionsForKongRoute(), }, + { + Object: &configurationv1alpha1.KongUpstream{}, + IndexOptions: konnect.IndexOptionsForKongUpstream(), + }, + { + Object: &configurationv1alpha1.KongTarget{}, + IndexOptions: konnect.IndexOptionsForKongTarget(), + }, { Object: &configurationv1alpha1.KongSNI{}, IndexOptions: konnect.IndexOptionsForKongSNI(),