diff --git a/agent/consul/server.go b/agent/consul/server.go index 1f3476c92..3ec392daa 100644 --- a/agent/consul/server.go +++ b/agent/consul/server.go @@ -68,7 +68,9 @@ import ( "github.com/hashicorp/consul/agent/rpc/peering" "github.com/hashicorp/consul/agent/structs" "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" raftstorage "github.com/hashicorp/consul/internal/storage/raft" @@ -830,13 +832,20 @@ func NewServer(config *Config, flat Deps, externalGRPCServer *grpc.Server, incom return nil, err } + s.registerResources() + go s.controllerManager.Run(&lib.StopChannelContext{StopCh: shutdownCh}) + + return s, nil +} + +func (s *Server) registerResources() { + catalog.RegisterTypes(s.typeRegistry) + mesh.RegisterTypes(s.typeRegistry) + if s.config.DevMode { demo.RegisterTypes(s.typeRegistry) demo.RegisterControllers(s.controllerManager) } - go s.controllerManager.Run(&lib.StopChannelContext{StopCh: shutdownCh}) - - return s, nil } func newGRPCHandlerFromConfig(deps Deps, config *Config, s *Server) connHandler { diff --git a/internal/catalog/exports.go b/internal/catalog/exports.go new file mode 100644 index 000000000..4f3ddb6a9 --- /dev/null +++ b/internal/catalog/exports.go @@ -0,0 +1,45 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package catalog + +import ( + "github.com/hashicorp/consul/internal/catalog/internal/types" + "github.com/hashicorp/consul/internal/resource" +) + +var ( + // API Group Information + + APIGroup = types.GroupName + VersionV1Alpha1 = types.VersionV1Alpha1 + CurrentVersion = types.CurrentVersion + + // Resource Kind Names. + + WorkloadKind = types.WorkloadKind + ServiceKind = types.ServiceKind + ServiceEndpointsKind = types.ServiceEndpointsKind + VirtualIPsKind = types.VirtualIPsKind + NodeKind = types.NodeKind + HealthStatusKind = types.HealthStatusKind + HealthChecksKind = types.HealthChecksKind + DNSPolicyKind = types.DNSPolicyKind + + // Resource Types for the v1alpha1 version. + + WorkloadV1Alpha1Type = types.WorkloadV1Alpha1Type + ServiceV1Alpha1Type = types.ServiceV1Alpha1Type + ServiceEndpointsV1Alpha1Type = types.ServiceEndpointsV1Alpha1Type + VirtualIPsV1Alpha1Type = types.VirtualIPsV1Alpha1Type + NodeV1Alpha1Type = types.NodeV1Alpha1Type + HealthStatusV1Alpha1Type = types.HealthStatusV1Alpha1Type + HealthChecksV1Alpha1Type = types.HealthChecksV1Alpha1Type + DNSPolicyV1Alpha1Type = types.DNSPolicyV1Alpha1Type +) + +// RegisterTypes adds all resource types within the "catalog" API group +// to the given type registry +func RegisterTypes(r resource.Registry) { + types.Register(r) +} diff --git a/internal/catalog/internal/types/dns_policy.go b/internal/catalog/internal/types/dns_policy.go new file mode 100644 index 000000000..d1d037590 --- /dev/null +++ b/internal/catalog/internal/types/dns_policy.go @@ -0,0 +1,32 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package types + +import ( + "github.com/hashicorp/consul/internal/resource" + pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v1alpha1" + "github.com/hashicorp/consul/proto-public/pbresource" +) + +const ( + DNSPolicyKind = "DNSPolicy" +) + +var ( + DNSPolicyV1Alpha1Type = &pbresource.Type{ + Group: GroupName, + GroupVersion: VersionV1Alpha1, + Kind: DNSPolicyKind, + } + + DNSPolicyType = DNSPolicyV1Alpha1Type +) + +func RegisterDNSPolicy(r resource.Registry) { + r.Register(resource.Registration{ + Type: DNSPolicyV1Alpha1Type, + Proto: &pbcatalog.DNSPolicy{}, + Validate: nil, + }) +} diff --git a/internal/catalog/internal/types/health_checks.go b/internal/catalog/internal/types/health_checks.go new file mode 100644 index 000000000..e11708898 --- /dev/null +++ b/internal/catalog/internal/types/health_checks.go @@ -0,0 +1,32 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package types + +import ( + "github.com/hashicorp/consul/internal/resource" + pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v1alpha1" + "github.com/hashicorp/consul/proto-public/pbresource" +) + +const ( + HealthChecksKind = "HealthChecks" +) + +var ( + HealthChecksV1Alpha1Type = &pbresource.Type{ + Group: GroupName, + GroupVersion: VersionV1Alpha1, + Kind: HealthChecksKind, + } + + HealthChecksType = HealthChecksV1Alpha1Type +) + +func RegisterHealthChecks(r resource.Registry) { + r.Register(resource.Registration{ + Type: HealthChecksV1Alpha1Type, + Proto: &pbcatalog.HealthChecks{}, + Validate: nil, + }) +} diff --git a/internal/catalog/internal/types/health_status.go b/internal/catalog/internal/types/health_status.go new file mode 100644 index 000000000..b85c8036d --- /dev/null +++ b/internal/catalog/internal/types/health_status.go @@ -0,0 +1,32 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package types + +import ( + "github.com/hashicorp/consul/internal/resource" + pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v1alpha1" + "github.com/hashicorp/consul/proto-public/pbresource" +) + +const ( + HealthStatusKind = "HealthStatus" +) + +var ( + HealthStatusV1Alpha1Type = &pbresource.Type{ + Group: GroupName, + GroupVersion: VersionV1Alpha1, + Kind: HealthStatusKind, + } + + HealthStatusType = HealthStatusV1Alpha1Type +) + +func RegisterHealthStatus(r resource.Registry) { + r.Register(resource.Registration{ + Type: HealthStatusV1Alpha1Type, + Proto: &pbcatalog.HealthStatus{}, + Validate: nil, + }) +} diff --git a/internal/catalog/internal/types/node.go b/internal/catalog/internal/types/node.go new file mode 100644 index 000000000..09d8cca1a --- /dev/null +++ b/internal/catalog/internal/types/node.go @@ -0,0 +1,32 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package types + +import ( + "github.com/hashicorp/consul/internal/resource" + pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v1alpha1" + "github.com/hashicorp/consul/proto-public/pbresource" +) + +const ( + NodeKind = "Node" +) + +var ( + NodeV1Alpha1Type = &pbresource.Type{ + Group: GroupName, + GroupVersion: VersionV1Alpha1, + Kind: NodeKind, + } + + NodeType = NodeV1Alpha1Type +) + +func RegisterNode(r resource.Registry) { + r.Register(resource.Registration{ + Type: NodeV1Alpha1Type, + Proto: &pbcatalog.Node{}, + Validate: nil, + }) +} diff --git a/internal/catalog/internal/types/service.go b/internal/catalog/internal/types/service.go new file mode 100644 index 000000000..78486128d --- /dev/null +++ b/internal/catalog/internal/types/service.go @@ -0,0 +1,32 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package types + +import ( + "github.com/hashicorp/consul/internal/resource" + pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v1alpha1" + "github.com/hashicorp/consul/proto-public/pbresource" +) + +const ( + ServiceKind = "Service" +) + +var ( + ServiceV1Alpha1Type = &pbresource.Type{ + Group: GroupName, + GroupVersion: VersionV1Alpha1, + Kind: ServiceKind, + } + + ServiceType = ServiceV1Alpha1Type +) + +func RegisterService(r resource.Registry) { + r.Register(resource.Registration{ + Type: ServiceV1Alpha1Type, + Proto: &pbcatalog.Service{}, + Validate: nil, + }) +} diff --git a/internal/catalog/internal/types/service_endpoints.go b/internal/catalog/internal/types/service_endpoints.go new file mode 100644 index 000000000..bef9ca27e --- /dev/null +++ b/internal/catalog/internal/types/service_endpoints.go @@ -0,0 +1,32 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package types + +import ( + "github.com/hashicorp/consul/internal/resource" + pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v1alpha1" + "github.com/hashicorp/consul/proto-public/pbresource" +) + +const ( + ServiceEndpointsKind = "ServiceEndpoints" +) + +var ( + ServiceEndpointsV1Alpha1Type = &pbresource.Type{ + Group: GroupName, + GroupVersion: VersionV1Alpha1, + Kind: ServiceEndpointsKind, + } + + ServiceEndpointsType = ServiceEndpointsV1Alpha1Type +) + +func RegisterServiceEndpoints(r resource.Registry) { + r.Register(resource.Registration{ + Type: ServiceEndpointsV1Alpha1Type, + Proto: &pbcatalog.ServiceEndpoints{}, + Validate: nil, + }) +} diff --git a/internal/catalog/internal/types/types.go b/internal/catalog/internal/types/types.go new file mode 100644 index 000000000..6ab3730bb --- /dev/null +++ b/internal/catalog/internal/types/types.go @@ -0,0 +1,25 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package types + +import ( + "github.com/hashicorp/consul/internal/resource" +) + +const ( + GroupName = "catalog" + VersionV1Alpha1 = "v1alpha1" + CurrentVersion = VersionV1Alpha1 +) + +func Register(r resource.Registry) { + RegisterWorkload(r) + RegisterService(r) + RegisterServiceEndpoints(r) + RegisterNode(r) + RegisterHealthStatus(r) + RegisterHealthChecks(r) + RegisterDNSPolicy(r) + RegisterVirtualIPs(r) +} diff --git a/internal/catalog/internal/types/types_test.go b/internal/catalog/internal/types/types_test.go new file mode 100644 index 000000000..d40fc42dd --- /dev/null +++ b/internal/catalog/internal/types/types_test.go @@ -0,0 +1,45 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package types + +import ( + "testing" + + "github.com/hashicorp/consul/internal/resource" + "github.com/hashicorp/consul/proto-public/pbresource" + "github.com/stretchr/testify/require" +) + +func TestTypeRegistration(t *testing.T) { + // This test will ensure that all the required types have been registered + // It is not trying to determine whether the settings for the registrations + // are correct as that would amount more or less to hardcoding structs + // from types.go a second time here. + requiredKinds := []string{ + WorkloadKind, + ServiceKind, + ServiceEndpointsKind, + VirtualIPsKind, + NodeKind, + HealthStatusKind, + HealthChecksKind, + DNSPolicyKind, + } + + r := resource.NewRegistry() + Register(r) + + for _, kind := range requiredKinds { + t.Run(kind, func(t *testing.T) { + registration, ok := r.Resolve(&pbresource.Type{ + Group: GroupName, + GroupVersion: CurrentVersion, + Kind: kind, + }) + + require.True(t, ok, "Resource kind %s has not been registered to the type registry", kind) + require.NotNil(t, registration, "Registration for %s was found but is nil", kind) + }) + } +} diff --git a/internal/catalog/internal/types/virtual_ips.go b/internal/catalog/internal/types/virtual_ips.go new file mode 100644 index 000000000..4a114fc80 --- /dev/null +++ b/internal/catalog/internal/types/virtual_ips.go @@ -0,0 +1,32 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package types + +import ( + "github.com/hashicorp/consul/internal/resource" + pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v1alpha1" + "github.com/hashicorp/consul/proto-public/pbresource" +) + +const ( + VirtualIPsKind = "VirtualIPs" +) + +var ( + VirtualIPsV1Alpha1Type = &pbresource.Type{ + Group: GroupName, + GroupVersion: VersionV1Alpha1, + Kind: VirtualIPsKind, + } + + VirtualIPsType = VirtualIPsV1Alpha1Type +) + +func RegisterVirtualIPs(r resource.Registry) { + r.Register(resource.Registration{ + Type: VirtualIPsV1Alpha1Type, + Proto: &pbcatalog.VirtualIPs{}, + Validate: nil, + }) +} diff --git a/internal/catalog/internal/types/workload.go b/internal/catalog/internal/types/workload.go new file mode 100644 index 000000000..0fa742a53 --- /dev/null +++ b/internal/catalog/internal/types/workload.go @@ -0,0 +1,32 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package types + +import ( + "github.com/hashicorp/consul/internal/resource" + pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v1alpha1" + "github.com/hashicorp/consul/proto-public/pbresource" +) + +const ( + WorkloadKind = "Workload" +) + +var ( + WorkloadV1Alpha1Type = &pbresource.Type{ + Group: GroupName, + GroupVersion: VersionV1Alpha1, + Kind: WorkloadKind, + } + + WorkloadType = WorkloadV1Alpha1Type +) + +func RegisterWorkload(r resource.Registry) { + r.Register(resource.Registration{ + Type: WorkloadV1Alpha1Type, + Proto: &pbcatalog.Workload{}, + Validate: nil, + }) +} diff --git a/internal/mesh/exports.go b/internal/mesh/exports.go new file mode 100644 index 000000000..753c10a7b --- /dev/null +++ b/internal/mesh/exports.go @@ -0,0 +1,33 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package mesh + +import ( + "github.com/hashicorp/consul/internal/mesh/internal/types" + "github.com/hashicorp/consul/internal/resource" +) + +var ( + // API Group Information + + APIGroup = types.GroupName + VersionV1Alpha1 = types.VersionV1Alpha1 + CurrentVersion = types.CurrentVersion + + // Resource Kind Names. + + ProxyConfigurationKind = types.ProxyConfigurationKind + UpstreamsKind = types.UpstreamsKind + + // Resource Types for the v1alpha1 version. + + ProxyConfigurationV1Alpha1Type = types.ProxyConfigurationV1Alpha1Type + UpstreamsV1Alpha1Type = types.UpstreamsV1Alpha1Type +) + +// RegisterTypes adds all resource types within the "catalog" API group +// to the given type registry +func RegisterTypes(r resource.Registry) { + types.Register(r) +} diff --git a/internal/mesh/internal/types/proxy_configuration.go b/internal/mesh/internal/types/proxy_configuration.go new file mode 100644 index 000000000..9205dc81b --- /dev/null +++ b/internal/mesh/internal/types/proxy_configuration.go @@ -0,0 +1,32 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package types + +import ( + "github.com/hashicorp/consul/internal/resource" + pbmesh "github.com/hashicorp/consul/proto-public/pbmesh/v1alpha1" + "github.com/hashicorp/consul/proto-public/pbresource" +) + +const ( + ProxyConfigurationKind = "ProxyConfiguration" +) + +var ( + ProxyConfigurationV1Alpha1Type = &pbresource.Type{ + Group: GroupName, + GroupVersion: CurrentVersion, + Kind: ProxyConfigurationKind, + } + + ProxyConfigurationType = ProxyConfigurationV1Alpha1Type +) + +func RegisterProxyConfiguration(r resource.Registry) { + r.Register(resource.Registration{ + Type: ProxyConfigurationV1Alpha1Type, + Proto: &pbmesh.ProxyConfiguration{}, + Validate: nil, + }) +} diff --git a/internal/mesh/internal/types/types.go b/internal/mesh/internal/types/types.go new file mode 100644 index 000000000..3eeb69bd1 --- /dev/null +++ b/internal/mesh/internal/types/types.go @@ -0,0 +1,19 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package types + +import ( + "github.com/hashicorp/consul/internal/resource" +) + +const ( + GroupName = "mesh" + VersionV1Alpha1 = "v1alpha1" + CurrentVersion = VersionV1Alpha1 +) + +func Register(r resource.Registry) { + RegisterProxyConfiguration(r) + RegisterUpstreams(r) +} diff --git a/internal/mesh/internal/types/types_test.go b/internal/mesh/internal/types/types_test.go new file mode 100644 index 000000000..4324e8707 --- /dev/null +++ b/internal/mesh/internal/types/types_test.go @@ -0,0 +1,39 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package types + +import ( + "testing" + + "github.com/hashicorp/consul/internal/resource" + "github.com/hashicorp/consul/proto-public/pbresource" + "github.com/stretchr/testify/require" +) + +func TestTypeRegistration(t *testing.T) { + // This test will ensure that all the required types have been registered + // It is not trying to determine whether the settings for the registrations + // are correct as that would amount more or less to hardcoding structs + // from types.go a second time here. + requiredKinds := []string{ + ProxyConfigurationKind, + UpstreamsKind, + } + + r := resource.NewRegistry() + Register(r) + + for _, kind := range requiredKinds { + t.Run(kind, func(t *testing.T) { + registration, ok := r.Resolve(&pbresource.Type{ + Group: GroupName, + GroupVersion: CurrentVersion, + Kind: kind, + }) + + require.True(t, ok, "Resource kind %s has not been registered to the type registry", kind) + require.NotNil(t, registration, "Registration for %s was found but is nil", kind) + }) + } +} diff --git a/internal/mesh/internal/types/upstreams.go b/internal/mesh/internal/types/upstreams.go new file mode 100644 index 000000000..54fd14b09 --- /dev/null +++ b/internal/mesh/internal/types/upstreams.go @@ -0,0 +1,32 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package types + +import ( + "github.com/hashicorp/consul/internal/resource" + pbmesh "github.com/hashicorp/consul/proto-public/pbmesh/v1alpha1" + "github.com/hashicorp/consul/proto-public/pbresource" +) + +const ( + UpstreamsKind = "Upstreams" +) + +var ( + UpstreamsV1Alpha1Type = &pbresource.Type{ + Group: GroupName, + GroupVersion: CurrentVersion, + Kind: UpstreamsKind, + } + + UpstreamsType = UpstreamsV1Alpha1Type +) + +func RegisterUpstreams(r resource.Registry) { + r.Register(resource.Registration{ + Type: UpstreamsV1Alpha1Type, + Proto: &pbmesh.Upstreams{}, + Validate: nil, + }) +}