config-entry: remove Kind and Name field from Mesh config entry

No config entry needs a Kind field. It is only used to determine the Go type to
target. As we introduce new config entries (like this one) we can remove the kind field
and have the GetKind method return the single supported value.

In this case (similar to proxy-defaults) the Name field is also unnecessary. We always
use the same value. So we can omit the name field entirely.
This commit is contained in:
Daniel Nephin 2021-04-29 15:54:27 -04:00
parent 71d45a3460
commit 62efaaab21
13 changed files with 18 additions and 48 deletions

View File

@ -4157,7 +4157,6 @@ func TestLoad_IntegrationWithFlags(t *testing.T) {
"bootstrap": [ "bootstrap": [
{ {
"kind": "mesh", "kind": "mesh",
"name": "mesh",
"meta" : { "meta" : {
"foo": "bar", "foo": "bar",
"gir": "zim" "gir": "zim"
@ -4174,7 +4173,6 @@ func TestLoad_IntegrationWithFlags(t *testing.T) {
config_entries { config_entries {
bootstrap { bootstrap {
kind = "mesh" kind = "mesh"
name = "mesh"
meta { meta {
"foo" = "bar" "foo" = "bar"
"gir" = "zim" "gir" = "zim"
@ -4190,8 +4188,6 @@ func TestLoad_IntegrationWithFlags(t *testing.T) {
rt.DataDir = dataDir rt.DataDir = dataDir
rt.ConfigEntryBootstrap = []structs.ConfigEntry{ rt.ConfigEntryBootstrap = []structs.ConfigEntry{
&structs.MeshConfigEntry{ &structs.MeshConfigEntry{
Kind: structs.MeshConfig,
Name: structs.MeshConfigMesh,
Meta: map[string]string{ Meta: map[string]string{
"foo": "bar", "foo": "bar",
"gir": "zim", "gir": "zim",
@ -4212,7 +4208,6 @@ func TestLoad_IntegrationWithFlags(t *testing.T) {
"bootstrap": [ "bootstrap": [
{ {
"Kind": "mesh", "Kind": "mesh",
"Name": "mesh",
"Meta" : { "Meta" : {
"foo": "bar", "foo": "bar",
"gir": "zim" "gir": "zim"
@ -4229,7 +4224,6 @@ func TestLoad_IntegrationWithFlags(t *testing.T) {
config_entries { config_entries {
bootstrap { bootstrap {
Kind = "mesh" Kind = "mesh"
Name = "mesh"
Meta { Meta {
"foo" = "bar" "foo" = "bar"
"gir" = "zim" "gir" = "zim"
@ -4245,8 +4239,6 @@ func TestLoad_IntegrationWithFlags(t *testing.T) {
rt.DataDir = dataDir rt.DataDir = dataDir
rt.ConfigEntryBootstrap = []structs.ConfigEntry{ rt.ConfigEntryBootstrap = []structs.ConfigEntry{
&structs.MeshConfigEntry{ &structs.MeshConfigEntry{
Kind: structs.MeshConfig,
Name: structs.MeshConfigMesh,
Meta: map[string]string{ Meta: map[string]string{
"foo": "bar", "foo": "bar",
"gir": "zim", "gir": "zim",

View File

@ -428,8 +428,6 @@ func TestFSM_SnapshotRestore_OSS(t *testing.T) {
// mesh config entry // mesh config entry
meshConfig := &structs.MeshConfigEntry{ meshConfig := &structs.MeshConfigEntry{
Kind: structs.MeshConfig,
Name: structs.MeshConfigMesh,
TransparentProxy: structs.TransparentProxyMeshConfig{ TransparentProxy: structs.TransparentProxyMeshConfig{
CatalogDestinationsOnly: true, CatalogDestinationsOnly: true,
}, },

View File

@ -1674,8 +1674,6 @@ func TestState_WatchesAndUpdates(t *testing.T) {
CorrelationID: meshConfigEntryID, CorrelationID: meshConfigEntryID,
Result: &structs.ConfigEntryResponse{ Result: &structs.ConfigEntryResponse{
Entry: &structs.MeshConfigEntry{ Entry: &structs.MeshConfigEntry{
Kind: structs.MeshConfig,
Name: structs.MeshConfigMesh,
TransparentProxy: structs.TransparentProxyMeshConfig{}, TransparentProxy: structs.TransparentProxyMeshConfig{},
}, },
}, },

View File

@ -529,7 +529,7 @@ func MakeConfigEntry(kind, name string) (ConfigEntry, error) {
case ServiceIntentions: case ServiceIntentions:
return &ServiceIntentionsConfigEntry{Name: name}, nil return &ServiceIntentionsConfigEntry{Name: name}, nil
case MeshConfig: case MeshConfig:
return &MeshConfigEntry{Name: name}, nil return &MeshConfigEntry{}, nil
default: default:
return nil, fmt.Errorf("invalid config entry kind: %s", kind) return nil, fmt.Errorf("invalid config entry kind: %s", kind)
} }

View File

@ -7,9 +7,6 @@ import (
) )
type MeshConfigEntry struct { type MeshConfigEntry struct {
Kind string
Name string
// TransparentProxy contains cluster-wide options pertaining to TPROXY mode // TransparentProxy contains cluster-wide options pertaining to TPROXY mode
// when enabled. // when enabled.
TransparentProxy TransparentProxyMeshConfig `alias:"transparent_proxy"` TransparentProxy TransparentProxyMeshConfig `alias:"transparent_proxy"`
@ -36,7 +33,7 @@ func (e *MeshConfigEntry) GetName() string {
return "" return ""
} }
return e.Name return MeshConfigMesh
} }
func (e *MeshConfigEntry) GetMeta() map[string]string { func (e *MeshConfigEntry) GetMeta() map[string]string {
@ -51,11 +48,7 @@ func (e *MeshConfigEntry) Normalize() error {
return fmt.Errorf("config entry is nil") return fmt.Errorf("config entry is nil")
} }
e.Kind = MeshConfig
e.Name = MeshConfigMesh
e.EnterpriseMeta.Normalize() e.EnterpriseMeta.Normalize()
return nil return nil
} }
@ -63,11 +56,6 @@ func (e *MeshConfigEntry) Validate() error {
if e == nil { if e == nil {
return fmt.Errorf("config entry is nil") return fmt.Errorf("config entry is nil")
} }
if e.Name != MeshConfigMesh {
return fmt.Errorf("invalid name (%q), only %q is supported", e.Name, MeshConfigMesh)
}
if err := validateConfigEntryMeta(e.Meta); err != nil { if err := validateConfigEntryMeta(e.Meta); err != nil {
return err return err
} }

View File

@ -19,6 +19,9 @@ func validateUnusedKeys(unused []string) error {
for _, k := range unused { for _, k := range unused {
switch { switch {
case k == "CreateIndex" || k == "ModifyIndex": case k == "CreateIndex" || k == "ModifyIndex":
case k == "kind" || k == "Kind":
// The kind field is used to determine the target, but doesn't need
// to exist on the target.
case strings.HasSuffix(strings.ToLower(k), "namespace"): case strings.HasSuffix(strings.ToLower(k), "namespace"):
err = multierror.Append(err, fmt.Errorf("invalid config key %q, namespaces are a consul enterprise feature", k)) err = multierror.Append(err, fmt.Errorf("invalid config key %q, namespaces are a consul enterprise feature", k))
default: default:

View File

@ -1310,7 +1310,6 @@ func TestDecodeConfigEntry(t *testing.T) {
name: "mesh", name: "mesh",
snake: ` snake: `
kind = "mesh" kind = "mesh"
name = "mesh"
meta { meta {
"foo" = "bar" "foo" = "bar"
"gir" = "zim" "gir" = "zim"
@ -1321,7 +1320,6 @@ func TestDecodeConfigEntry(t *testing.T) {
`, `,
camel: ` camel: `
Kind = "mesh" Kind = "mesh"
Name = "mesh"
Meta { Meta {
"foo" = "bar" "foo" = "bar"
"gir" = "zim" "gir" = "zim"
@ -1331,8 +1329,6 @@ func TestDecodeConfigEntry(t *testing.T) {
} }
`, `,
expect: &MeshConfigEntry{ expect: &MeshConfigEntry{
Kind: MeshConfig,
Name: MeshConfigMesh,
Meta: map[string]string{ Meta: map[string]string{
"foo": "bar", "foo": "bar",
"gir": "zim", "gir": "zim",

View File

@ -519,8 +519,6 @@ func TestListenersFromSnapshot(t *testing.T) {
snap.ConnectProxy.MeshConfigSet = true snap.ConnectProxy.MeshConfigSet = true
snap.ConnectProxy.MeshConfig = &structs.MeshConfigEntry{ snap.ConnectProxy.MeshConfig = &structs.MeshConfigEntry{
Kind: structs.MeshConfig,
Name: structs.MeshConfigMesh,
TransparentProxy: structs.TransparentProxyMeshConfig{ TransparentProxy: structs.TransparentProxyMeshConfig{
CatalogDestinationsOnly: true, CatalogDestinationsOnly: true,
}, },

View File

@ -295,7 +295,7 @@ func makeConfigEntry(kind, name string) (ConfigEntry, error) {
case ServiceIntentions: case ServiceIntentions:
return &ServiceIntentionsConfigEntry{Kind: kind, Name: name}, nil return &ServiceIntentionsConfigEntry{Kind: kind, Name: name}, nil
case MeshConfig: case MeshConfig:
return &MeshConfigEntry{Kind: kind, Name: name}, nil return &MeshConfigEntry{}, nil
default: default:
return nil, fmt.Errorf("invalid config entry kind: %s", kind) return nil, fmt.Errorf("invalid config entry kind: %s", kind)
} }

View File

@ -1,7 +1,6 @@
package api package api
type MeshConfigEntry struct { type MeshConfigEntry struct {
Kind string
Name string Name string
Namespace string `json:",omitempty"` Namespace string `json:",omitempty"`
TransparentProxy TransparentProxyMeshConfig `alias:"transparent_proxy"` TransparentProxy TransparentProxyMeshConfig `alias:"transparent_proxy"`
@ -15,11 +14,11 @@ type TransparentProxyMeshConfig struct {
} }
func (e *MeshConfigEntry) GetKind() string { func (e *MeshConfigEntry) GetKind() string {
return e.Kind return MeshConfig
} }
func (e *MeshConfigEntry) GetName() string { func (e *MeshConfigEntry) GetName() string {
return e.Name return MeshConfigMesh
} }
func (e *MeshConfigEntry) GetNamespace() string { func (e *MeshConfigEntry) GetNamespace() string {

View File

@ -1141,7 +1141,6 @@ func TestDecodeConfigEntry(t *testing.T) {
body: ` body: `
{ {
"Kind": "mesh", "Kind": "mesh",
"Name": "mesh",
"Meta" : { "Meta" : {
"foo": "bar", "foo": "bar",
"gir": "zim" "gir": "zim"
@ -1152,8 +1151,6 @@ func TestDecodeConfigEntry(t *testing.T) {
} }
`, `,
expect: &MeshConfigEntry{ expect: &MeshConfigEntry{
Kind: "mesh",
Name: "mesh",
Meta: map[string]string{ Meta: map[string]string{
"foo": "bar", "foo": "bar",
"gir": "zim", "gir": "zim",

View File

@ -6,13 +6,14 @@ import (
"io" "io"
"time" "time"
"github.com/hashicorp/go-multierror"
"github.com/mitchellh/cli"
"github.com/mitchellh/mapstructure"
"github.com/hashicorp/consul/api" "github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/command/flags" "github.com/hashicorp/consul/command/flags"
"github.com/hashicorp/consul/command/helpers" "github.com/hashicorp/consul/command/helpers"
"github.com/hashicorp/consul/lib/decode" "github.com/hashicorp/consul/lib/decode"
"github.com/hashicorp/go-multierror"
"github.com/mitchellh/cli"
"github.com/mitchellh/mapstructure"
) )
func New(ui cli.Ui) *cmd { func New(ui cli.Ui) *cmd {
@ -155,6 +156,12 @@ func newDecodeConfigEntry(raw map[string]interface{}) (api.ConfigEntry, error) {
} }
for _, k := range md.Unused { for _, k := range md.Unused {
switch k {
case "kind", "Kind":
// The kind field is used to determine the target, but doesn't need
// to exist on the target.
continue
}
err = multierror.Append(err, fmt.Errorf("invalid config key %q", k)) err = multierror.Append(err, fmt.Errorf("invalid config key %q", k))
} }
if err != nil { if err != nil {

View File

@ -2627,7 +2627,6 @@ func TestParseConfigEntry(t *testing.T) {
name: "mesh", name: "mesh",
snake: ` snake: `
kind = "mesh" kind = "mesh"
name = "mesh"
meta { meta {
"foo" = "bar" "foo" = "bar"
"gir" = "zim" "gir" = "zim"
@ -2638,7 +2637,6 @@ func TestParseConfigEntry(t *testing.T) {
`, `,
camel: ` camel: `
Kind = "mesh" Kind = "mesh"
Name = "mesh"
Meta { Meta {
"foo" = "bar" "foo" = "bar"
"gir" = "zim" "gir" = "zim"
@ -2650,7 +2648,6 @@ func TestParseConfigEntry(t *testing.T) {
snakeJSON: ` snakeJSON: `
{ {
"kind": "mesh", "kind": "mesh",
"name": "mesh",
"meta" : { "meta" : {
"foo": "bar", "foo": "bar",
"gir": "zim" "gir": "zim"
@ -2663,7 +2660,6 @@ func TestParseConfigEntry(t *testing.T) {
camelJSON: ` camelJSON: `
{ {
"Kind": "mesh", "Kind": "mesh",
"Name": "mesh",
"Meta" : { "Meta" : {
"foo": "bar", "foo": "bar",
"gir": "zim" "gir": "zim"
@ -2674,8 +2670,6 @@ func TestParseConfigEntry(t *testing.T) {
} }
`, `,
expect: &api.MeshConfigEntry{ expect: &api.MeshConfigEntry{
Kind: api.MeshConfig,
Name: api.MeshConfigMesh,
Meta: map[string]string{ Meta: map[string]string{
"foo": "bar", "foo": "bar",
"gir": "zim", "gir": "zim",