Register new catalog & mesh protobuf types with the resource registry (#17225)

This commit is contained in:
Matt Keeler 2023-05-08 15:36:35 -04:00 committed by GitHub
parent 73b65228f5
commit 6919dabb50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 538 additions and 3 deletions

View File

@ -68,7 +68,9 @@ import (
"github.com/hashicorp/consul/agent/rpc/peering" "github.com/hashicorp/consul/agent/rpc/peering"
"github.com/hashicorp/consul/agent/structs" "github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/agent/token" "github.com/hashicorp/consul/agent/token"
"github.com/hashicorp/consul/internal/catalog"
"github.com/hashicorp/consul/internal/controller" "github.com/hashicorp/consul/internal/controller"
"github.com/hashicorp/consul/internal/mesh"
"github.com/hashicorp/consul/internal/resource" "github.com/hashicorp/consul/internal/resource"
"github.com/hashicorp/consul/internal/resource/demo" "github.com/hashicorp/consul/internal/resource/demo"
raftstorage "github.com/hashicorp/consul/internal/storage/raft" 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 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 { if s.config.DevMode {
demo.RegisterTypes(s.typeRegistry) demo.RegisterTypes(s.typeRegistry)
demo.RegisterControllers(s.controllerManager) demo.RegisterControllers(s.controllerManager)
} }
go s.controllerManager.Run(&lib.StopChannelContext{StopCh: shutdownCh})
return s, nil
} }
func newGRPCHandlerFromConfig(deps Deps, config *Config, s *Server) connHandler { func newGRPCHandlerFromConfig(deps Deps, config *Config, s *Server) connHandler {

View File

@ -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)
}

View File

@ -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,
})
}

View File

@ -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,
})
}

View File

@ -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,
})
}

View File

@ -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,
})
}

View File

@ -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,
})
}

View File

@ -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,
})
}

View File

@ -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)
}

View File

@ -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)
})
}
}

View File

@ -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,
})
}

View File

@ -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,
})
}

33
internal/mesh/exports.go Normal file
View File

@ -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)
}

View File

@ -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,
})
}

View File

@ -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)
}

View File

@ -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)
})
}
}

View File

@ -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,
})
}