Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

re org registry #18133

Merged
merged 6 commits into from
Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions agent/consul/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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.
Expand Down
27 changes: 9 additions & 18 deletions agent/consul/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"crypto/x509"
"errors"
"fmt"
"github.com/hashicorp/consul/internal/resource"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

goimports

"io"
"net"
"os"
Expand Down Expand Up @@ -72,8 +73,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"
Expand Down Expand Up @@ -439,9 +438,6 @@ type Server struct {
// run by the Server
routineManager *routine.Manager

// typeRegistry contains Consul's registered resource types.
typeRegistry resource.Registry

// internalResourceServiceClient is a client that can be used to communicate
// with the Resource Service in-process (i.e. not via the network) without auth.
// It should only be used for purely-internal workloads, such as controllers.
Expand Down Expand Up @@ -526,7 +522,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)

Expand Down Expand Up @@ -794,7 +789,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.
//
Expand All @@ -803,14 +798,14 @@ func NewServer(config *Config, flat Deps, externalGRPCServer *grpc.Server, incom
s.grpcHandler = newGRPCHandlerFromConfig(flat, config, s)
s.grpcLeaderForwarder = flat.LeaderForwarder

if err := s.setupInternalResourceService(logger); err != nil {
if err := s.setupInternalResourceService(flat.Registry, logger); err != nil {
return nil, err
}
s.controllerManager = controller.NewManager(
s.internalResourceServiceClient,
logger.Named(logging.ControllerRuntime),
)
s.registerResources(flat)
s.registerControllers(flat)
go s.controllerManager.Run(&lib.StopChannelContext{StopCh: shutdownCh})

go s.trackLeaderChanges()
Expand Down Expand Up @@ -861,18 +856,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)
}
}
Expand Down Expand Up @@ -1269,7 +1260,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) {
Expand Down Expand Up @@ -1335,18 +1326,18 @@ func (s *Server) setupExternalGRPC(config *Config, logger hclog.Logger) {
s.peerStreamServer.Register(s.externalGRPCServer)

resourcegrpc.NewServer(resourcegrpc.Config{
Registry: s.typeRegistry,
Registry: typeRegistry,
Backend: s.raftStorageBackend,
ACLResolver: s.ACLResolver,
Logger: logger.Named("grpc-api.resource"),
}).Register(s.externalGRPCServer)
}

func (s *Server) setupInternalResourceService(logger hclog.Logger) error {
func (s *Server) setupInternalResourceService(typeRegistry resource.Registry, logger hclog.Logger) error {
server := grpc.NewServer()

resourcegrpc.NewServer(resourcegrpc.Config{
Registry: s.typeRegistry,
Registry: typeRegistry,
Backend: s.raftStorageBackend,
ACLResolver: resolver.DANGER_NO_AUTH{},
Logger: logger.Named("grpc-api.resource"),
Expand Down
25 changes: 25 additions & 0 deletions agent/consul/type_registry.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions agent/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ func NewBaseDeps(configLoader ConfigLoader, logOut io.Writer, providedLogger hcl

d.XDSStreamLimiter = limiter.NewSessionLimiter()

d.Registry = consul.NewTypeRegistry()

return d, nil
}

Expand Down
Loading