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

resource: make the populated registry available outside of server #18000

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 3 additions & 8 deletions agent/consul/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,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"
Expand Down Expand Up @@ -526,7 +525,7 @@ 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(),
typeRegistry: NewTypeRegistry(),
}
incomingRPCLimiter.Register(s)

Expand Down Expand Up @@ -810,7 +809,7 @@ func NewServer(config *Config, flat Deps, externalGRPCServer *grpc.Server, incom
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 +860,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
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)
Copy link
Member

Choose a reason for hiding this comment

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

Previously we only made these resources available in dev mode, should we still do the same thing?

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I guess dev mode is just another feature flag.

Copy link
Member

Choose a reason for hiding this comment

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

This actually makes me wonder whether enablement based on flags should be a feature of the resource service.

type Registration struct {
	// Type is the GVK of the resource type.
	Type *pbresource.Type

	// Proto is the resource's protobuf message type.
	Proto proto.Message

	// ACLs are hooks called to perform authorization on RPCs.
	ACLs *ACLHooks

	// Validate is called to structurally validate the resource (e.g.
	// check for required fields).
	Validate func(*pbresource.Resource) error

	// Mutate is called to fill out any autogenerated fields (e.g. UUIDs).
	Mutate func(*pbresource.Resource) error

	// RequireDevMode being set to true will only allow usage of this resource when in dev mode.
	RequireDevMode bool

	// RequireExperiment will cause the resource to only be allowed to be used if the corresponding experiment flag is set.
	RequireExperiment string      

	// In the future, we'll add hooks, the controller etc. here.
	// TODO: https://github.com/hashicorp/consul/pull/16622#discussion_r1134515909
}

mesh.RegisterTypes(registry)
catalog.RegisterTypes(registry)

return registry
}
18 changes: 10 additions & 8 deletions docs/resources/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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:

Expand Down Expand Up @@ -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
Expand All @@ -290,7 +292,7 @@ func RegisterControllers(mgr *controller.Manager) {
```Go
package consul

func (s *Server) registerResources() {
func (s *Server) registerControllers() {
// …
foo.RegisterControllers(s.controllerManager)
// …
Expand Down