From 1bd253021b3085cecc17090751b5749ffa167ea6 Mon Sep 17 00:00:00 2001 From: Eric Haberkorn Date: Wed, 7 Feb 2024 14:05:42 -0500 Subject: [PATCH] V1 Compat Exported Services Controller Optimizations (#20517) V1 compat exported services controller optimizations * Don't start the v2 exported services controller in v1 mode. * Use the controller cache. --- agent/consul/server.go | 7 +-- internal/multicluster/exports.go | 18 ++++++-- .../internal/controllers/register.go | 8 +++- .../controllers/v1compat/controller.go | 44 ++++++++++++++----- 4 files changed, 57 insertions(+), 20 deletions(-) diff --git a/agent/consul/server.go b/agent/consul/server.go index 32617e80f0..9513b839f7 100644 --- a/agent/consul/server.go +++ b/agent/consul/server.go @@ -1043,11 +1043,12 @@ func (s *Server) registerControllers(deps Deps, proxyUpdater ProxyUpdater) error }) auth.RegisterControllers(s.controllerManager, auth.DefaultControllerDependencies()) + multicluster.RegisterControllers(s.controllerManager) + } else { + shim := NewExportedServicesShim(s) + multicluster.RegisterCompatControllers(s.controllerManager, multicluster.DefaultCompatControllerDependencies(shim)) } - shim := NewExportedServicesShim(s) - multicluster.RegisterControllers(s.controllerManager, multicluster.DefaultControllerDependencies(shim)) - reaper.RegisterControllers(s.controllerManager) if s.config.DevMode { diff --git a/internal/multicluster/exports.go b/internal/multicluster/exports.go index e23269619d..6d4e77efee 100644 --- a/internal/multicluster/exports.go +++ b/internal/multicluster/exports.go @@ -26,16 +26,26 @@ func RegisterTypes(r resource.Registry) { } type ControllerDependencies = controllers.Dependencies +type CompatControllerDependencies = controllers.CompatDependencies -func DefaultControllerDependencies(ac v1compat.AggregatedConfig) ControllerDependencies { +func DefaultControllerDependencies() ControllerDependencies { return ControllerDependencies{ ExportedServicesSamenessGroupsExpander: exportedServicesSamenessGroupExpander.New(), - ConfigEntryExports: ac, + } +} + +func DefaultCompatControllerDependencies(ac v1compat.AggregatedConfig) CompatControllerDependencies { + return CompatControllerDependencies{ + ConfigEntryExports: ac, } } // RegisterControllers registers controllers for the multicluster types with // the given controller Manager. -func RegisterControllers(mgr *controller.Manager, deps ControllerDependencies) { - controllers.Register(mgr, deps) +func RegisterControllers(mgr *controller.Manager) { + controllers.Register(mgr, DefaultControllerDependencies()) +} + +func RegisterCompatControllers(mgr *controller.Manager, deps CompatControllerDependencies) { + controllers.RegisterCompat(mgr, deps) } diff --git a/internal/multicluster/internal/controllers/register.go b/internal/multicluster/internal/controllers/register.go index e779c21aa0..8bc4f07615 100644 --- a/internal/multicluster/internal/controllers/register.go +++ b/internal/multicluster/internal/controllers/register.go @@ -11,10 +11,16 @@ import ( type Dependencies struct { ExportedServicesSamenessGroupsExpander exportedservices.ExportedServicesSamenessGroupExpander - ConfigEntryExports v1compat.AggregatedConfig +} + +type CompatDependencies struct { + ConfigEntryExports v1compat.AggregatedConfig } func Register(mgr *controller.Manager, deps Dependencies) { mgr.Register(exportedservices.Controller(deps.ExportedServicesSamenessGroupsExpander)) +} + +func RegisterCompat(mgr *controller.Manager, deps CompatDependencies) { mgr.Register(v1compat.Controller(deps.ConfigEntryExports)) } diff --git a/internal/multicluster/internal/controllers/v1compat/controller.go b/internal/multicluster/internal/controllers/v1compat/controller.go index c6e2a97ac9..e5c64a6642 100644 --- a/internal/multicluster/internal/controllers/v1compat/controller.go +++ b/internal/multicluster/internal/controllers/v1compat/controller.go @@ -11,6 +11,8 @@ import ( "github.com/hashicorp/consul/acl" "github.com/hashicorp/consul/agent/structs" "github.com/hashicorp/consul/internal/controller" + "github.com/hashicorp/consul/internal/controller/cache" + "github.com/hashicorp/consul/internal/controller/cache/index" "github.com/hashicorp/consul/internal/multicluster/internal/types" "github.com/hashicorp/consul/internal/resource" pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v2beta1" @@ -105,30 +107,48 @@ func (r *reconciler) Reconcile(ctx context.Context, rt controller.Runtime, req c EnterpriseMeta: *entMeta, } - partitionExports, err := resource.ListDecodedResource[*pbmulticluster.PartitionExportedServices](ctx, rt.Client, &pbresource.ListRequest{ - Type: pbmulticluster.PartitionExportedServicesType, - Tenancy: req.ID.Tenancy, - }) + partitionExports, err := cache.ListDecoded[*pbmulticluster.PartitionExportedServices]( + rt.Cache, + pbmulticluster.PartitionExportedServicesType, + "id", + &pbresource.ID{ + Type: pbmulticluster.PartitionExportedServicesType, + Tenancy: req.ID.Tenancy, + }, + index.IndexQueryOptions{Prefix: true}, + ) if err != nil { rt.Logger.Error("error retrieving partition exported services", "error", err) return err } - namespaceExports, err := resource.ListDecodedResource[*pbmulticluster.NamespaceExportedServices](ctx, rt.Client, &pbresource.ListRequest{ - Type: pbmulticluster.NamespaceExportedServicesType, - Tenancy: req.ID.Tenancy, - }) + namespaceExports, err := cache.ListDecoded[*pbmulticluster.NamespaceExportedServices]( + rt.Cache, + pbmulticluster.NamespaceExportedServicesType, + "id", + &pbresource.ID{ + Type: pbmulticluster.NamespaceExportedServicesType, + Tenancy: req.ID.Tenancy, + }, + index.IndexQueryOptions{Prefix: true}, + ) if err != nil { rt.Logger.Error("error retrieving namespace exported service", "error", err) return err } - serviceExports, err := resource.ListDecodedResource[*pbmulticluster.ExportedServices](ctx, rt.Client, &pbresource.ListRequest{ - Type: pbmulticluster.ExportedServicesType, - Tenancy: req.ID.Tenancy, - }) + serviceExports, err := cache.ListDecoded[*pbmulticluster.ExportedServices]( + rt.Cache, + pbmulticluster.ExportedServicesType, + "id", + &pbresource.ID{ + Type: pbmulticluster.ExportedServicesType, + Tenancy: req.ID.Tenancy, + }, + index.IndexQueryOptions{Prefix: true}, + ) if err != nil { rt.Logger.Error("error retrieving exported services", "error", err)