diff --git a/agent/consul/client_test.go b/agent/consul/client_test.go index 4b8f5c433d..174820c067 100644 --- a/agent/consul/client_test.go +++ b/agent/consul/client_test.go @@ -14,6 +14,8 @@ import ( "testing" "time" + "github.com/hashicorp/consul/internal/resource" + "github.com/hashicorp/go-hclog" "github.com/hashicorp/serf/serf" "github.com/stretchr/testify/require" @@ -576,6 +578,7 @@ func newDefaultDeps(t *testing.T, c *Config) Deps { GetNetRPCInterceptorFunc: middleware.GetNetRPCInterceptor, EnterpriseDeps: newDefaultDepsEnterprise(t, logger, c), XDSStreamLimiter: limiter.NewSessionLimiter(), + Registry: resource.NewRegistry(), } } diff --git a/agent/consul/options.go b/agent/consul/options.go index 26cb2471a8..4b1d088249 100644 --- a/agent/consul/options.go +++ b/agent/consul/options.go @@ -16,6 +16,7 @@ import ( "github.com/hashicorp/consul/agent/router" "github.com/hashicorp/consul/agent/rpc/middleware" "github.com/hashicorp/consul/agent/token" + "github.com/hashicorp/consul/internal/resource" "github.com/hashicorp/consul/tlsutil" ) @@ -29,6 +30,7 @@ type Deps struct { GRPCConnPool GRPCClientConner LeaderForwarder LeaderForwarder XDSStreamLimiter *limiter.SessionLimiter + Registry resource.Registry // GetNetRPCInterceptorFunc, if not nil, sets the net/rpc rpc.ServerServiceCallInterceptor on // the server side to record metrics around the RPC requests. If nil, no interceptor is added to // the rpc server. diff --git a/agent/consul/server.go b/agent/consul/server.go index 2cfe9cb0aa..6e5ea29da0 100644 --- a/agent/consul/server.go +++ b/agent/consul/server.go @@ -19,6 +19,8 @@ import ( "sync/atomic" "time" + "github.com/hashicorp/consul/internal/resource" + "github.com/armon/go-metrics" "github.com/hashicorp/go-connlimit" "github.com/hashicorp/go-hclog" @@ -72,8 +74,6 @@ import ( "github.com/hashicorp/consul/agent/token" "github.com/hashicorp/consul/internal/catalog" "github.com/hashicorp/consul/internal/controller" - "github.com/hashicorp/consul/internal/mesh" - "github.com/hashicorp/consul/internal/resource" "github.com/hashicorp/consul/internal/resource/demo" "github.com/hashicorp/consul/internal/resource/reaper" raftstorage "github.com/hashicorp/consul/internal/storage/raft" @@ -439,9 +439,6 @@ type Server struct { // run by the Server routineManager *routine.Manager - // typeRegistry contains Consul's registered resource types. - typeRegistry resource.Registry - // resourceServiceServer implements the Resource Service. resourceServiceServer *resourcegrpc.Server @@ -536,7 +533,6 @@ func NewServer(config *Config, flat Deps, externalGRPCServer *grpc.Server, incom publisher: flat.EventPublisher, incomingRPCLimiter: incomingRPCLimiter, routineManager: routine.NewManager(logger.Named(logging.ConsulServer)), - typeRegistry: resource.NewRegistry(), } incomingRPCLimiter.Register(s) @@ -804,7 +800,7 @@ func NewServer(config *Config, flat Deps, externalGRPCServer *grpc.Server, incom go s.reportingManager.Run(&lib.StopChannelContext{StopCh: s.shutdownCh}) // Initialize external gRPC server - s.setupExternalGRPC(config, logger) + s.setupExternalGRPC(config, flat.Registry, logger) // Initialize internal gRPC server. // @@ -817,7 +813,7 @@ func NewServer(config *Config, flat Deps, externalGRPCServer *grpc.Server, incom return nil, err } - if err := s.setupInsecureResourceServiceClient(logger); err != nil { + if err := s.setupInsecureResourceServiceClient(flat.Registry, logger); err != nil { return nil, err } @@ -825,7 +821,7 @@ func NewServer(config *Config, flat Deps, externalGRPCServer *grpc.Server, incom s.insecureResourceServiceClient, logger.Named(logging.ControllerRuntime), ) - s.registerResources(flat) + s.registerControllers(flat) go s.controllerManager.Run(&lib.StopChannelContext{StopCh: shutdownCh}) go s.trackLeaderChanges() @@ -876,18 +872,14 @@ func NewServer(config *Config, flat Deps, externalGRPCServer *grpc.Server, incom return s, nil } -func (s *Server) registerResources(deps Deps) { +func (s *Server) registerControllers(deps Deps) { if stringslice.Contains(deps.Experiments, catalogResourceExperimentName) { - catalog.RegisterTypes(s.typeRegistry) catalog.RegisterControllers(s.controllerManager, catalog.DefaultControllerDependencies()) - - mesh.RegisterTypes(s.typeRegistry) } reaper.RegisterControllers(s.controllerManager) if s.config.DevMode { - demo.RegisterTypes(s.typeRegistry) demo.RegisterControllers(s.controllerManager) } } @@ -1285,7 +1277,7 @@ func (s *Server) setupRPC() error { } // Initialize and register services on external gRPC server. -func (s *Server) setupExternalGRPC(config *Config, logger hclog.Logger) { +func (s *Server) setupExternalGRPC(config *Config, typeRegistry resource.Registry, logger hclog.Logger) { s.externalACLServer = aclgrpc.NewServer(aclgrpc.Config{ ACLsEnabled: s.config.ACLsEnabled, ForwardRPC: func(info structs.RPCInfo, fn func(*grpc.ClientConn) error) (bool, error) { @@ -1351,7 +1343,7 @@ func (s *Server) setupExternalGRPC(config *Config, logger hclog.Logger) { s.peerStreamServer.Register(s.externalGRPCServer) s.resourceServiceServer = resourcegrpc.NewServer(resourcegrpc.Config{ - Registry: s.typeRegistry, + Registry: typeRegistry, Backend: s.raftStorageBackend, ACLResolver: s.ACLResolver, Logger: logger.Named("grpc-api.resource"), @@ -1359,9 +1351,9 @@ func (s *Server) setupExternalGRPC(config *Config, logger hclog.Logger) { s.resourceServiceServer.Register(s.externalGRPCServer) } -func (s *Server) setupInsecureResourceServiceClient(logger hclog.Logger) error { +func (s *Server) setupInsecureResourceServiceClient(typeRegistry resource.Registry, logger hclog.Logger) error { server := resourcegrpc.NewServer(resourcegrpc.Config{ - Registry: s.typeRegistry, + Registry: typeRegistry, Backend: s.raftStorageBackend, ACLResolver: resolver.DANGER_NO_AUTH{}, Logger: logger.Named("grpc-api.resource"), diff --git a/agent/consul/type_registry.go b/agent/consul/type_registry.go new file mode 100644 index 0000000000..1e5ba55a0c --- /dev/null +++ b/agent/consul/type_registry.go @@ -0,0 +1,25 @@ +package consul + +import ( + "github.com/hashicorp/consul/internal/catalog" + "github.com/hashicorp/consul/internal/mesh" + "github.com/hashicorp/consul/internal/resource" + "github.com/hashicorp/consul/internal/resource/demo" +) + +// NewTypeRegistry returns a registry populated with all supported resource +// types. +// +// Note: the registry includes resource types that may not be suitable for +// production use (e.g. experimental or development resource types) because +// it is used in the CLI, where feature flags and other runtime configuration +// may not be available. +func NewTypeRegistry() resource.Registry { + registry := resource.NewRegistry() + + demo.RegisterTypes(registry) + mesh.RegisterTypes(registry) + catalog.RegisterTypes(registry) + + return registry +} diff --git a/agent/rpc/peering/service_test.go b/agent/rpc/peering/service_test.go index 9ae1f65977..0af92f5316 100644 --- a/agent/rpc/peering/service_test.go +++ b/agent/rpc/peering/service_test.go @@ -15,6 +15,8 @@ import ( "testing" "time" + "github.com/hashicorp/consul/internal/resource" + "github.com/google/tcpproxy" "github.com/hashicorp/go-hclog" "github.com/hashicorp/go-uuid" @@ -1950,6 +1952,7 @@ func newDefaultDeps(t *testing.T, c *consul.Config) consul.Deps { NewRequestRecorderFunc: middleware.NewRequestRecorder, GetNetRPCInterceptorFunc: middleware.GetNetRPCInterceptor, XDSStreamLimiter: limiter.NewSessionLimiter(), + Registry: resource.NewRegistry(), } } diff --git a/agent/setup.go b/agent/setup.go index 4d5d0feed7..7599668e33 100644 --- a/agent/setup.go +++ b/agent/setup.go @@ -260,6 +260,8 @@ func NewBaseDeps(configLoader ConfigLoader, logOut io.Writer, providedLogger hcl d.XDSStreamLimiter = limiter.NewSessionLimiter() + d.Registry = consul.NewTypeRegistry() + return d, nil } diff --git a/docs/resources/guide.md b/docs/resources/guide.md index b3f389c006..c19566577b 100644 --- a/docs/resources/guide.md +++ b/docs/resources/guide.md @@ -61,8 +61,10 @@ func RegisterTypes(r resource.Registry) { } ``` -Update the `registerResources` method in [`server.go`] to call your package's -type registration method: +Update the `NewTypeRegistry` method in [`type_registry.go`] to call your +package's type registration method: + +[`type_registry.go`]: ../../agent/consul/type_registry.go ```Go import ( @@ -71,15 +73,13 @@ import ( // … ) -func (s *Server) registerResources() { +func NewTypeRegistry() resource.Registry { // … - foo.RegisterTypes(s.typeRegistry) + foo.RegisterTypes(registry) // … } ``` -[`server.go`]: ../../agent/consul/server.go - That should be all you need to start using your new resource type. Test it out by starting an agent in dev mode: @@ -277,7 +277,9 @@ func (barReconciler) Reconcile(ctx context.Context, rt controller.Runtime, req c Next, register your controller with the controller manager. Another common pattern is to have your package expose a method for registering controllers, -which is also called from `registerResources` in [`server.go`]. +which is called from `registerControllers` in [`server.go`]. + +[`server.go`]: ../../agent/consul/server.go ```Go package foo @@ -290,7 +292,7 @@ func RegisterControllers(mgr *controller.Manager) { ```Go package consul -func (s *Server) registerResources() { +func (s *Server) registerControllers() { // … foo.RegisterControllers(s.controllerManager) // …