From 0e6a482b76d7b5f1514ea4ac96838dd4145f1705 Mon Sep 17 00:00:00 2001 From: "R.B. Boyer" <4903+rboyer@users.noreply.github.com> Date: Thu, 8 Jul 2021 10:03:38 -0500 Subject: [PATCH] config: add agent config flag for enterprise clients to indicate they wish to join a particular partition (#10572) --- .changelog/10572.txt | 3 ++ agent/config/builder_oss.go | 3 ++ agent/config/config.go | 2 + agent/config/runtime_oss.go | 2 + agent/config/runtime_oss_test.go | 60 ++++++++++++++++++++++++++ agent/config/testdata/full-config.hcl | 1 + agent/config/testdata/full-config.json | 1 + command/agent/agent.go | 3 ++ 8 files changed, 75 insertions(+) create mode 100644 .changelog/10572.txt diff --git a/.changelog/10572.txt b/.changelog/10572.txt new file mode 100644 index 000000000..03388d2b8 --- /dev/null +++ b/.changelog/10572.txt @@ -0,0 +1,3 @@ +```release-note:feature +config: add agent config flag for enterprise clients to indicate they wish to join a particular partition +``` diff --git a/agent/config/builder_oss.go b/agent/config/builder_oss.go index c663e8bb6..f78bea300 100644 --- a/agent/config/builder_oss.go +++ b/agent/config/builder_oss.go @@ -25,6 +25,9 @@ func validateEnterpriseConfigKeys(config *Config) []error { if len(config.Segments) > 0 { add("segments") } + if stringVal(config.Partition) != "" { + add("partition") + } if stringVal(config.Autopilot.RedundancyZoneTag) != "" { add("autopilot.redundancy_zone_tag") } diff --git a/agent/config/config.go b/agent/config/config.go index 3e5372391..eb11cc356 100644 --- a/agent/config/config.go +++ b/agent/config/config.go @@ -290,6 +290,8 @@ type Config struct { SegmentName *string `mapstructure:"segment"` // Enterprise Only Segments []Segment `mapstructure:"segments"` + // Enterprise Only + Partition *string `mapstructure:"partition"` // Enterprise Only - not user configurable LicensePollBaseTime *string `mapstructure:"license_poll_base_time"` diff --git a/agent/config/runtime_oss.go b/agent/config/runtime_oss.go index 8f5758648..a83946f10 100644 --- a/agent/config/runtime_oss.go +++ b/agent/config/runtime_oss.go @@ -3,3 +3,5 @@ package config type EnterpriseRuntimeConfig struct{} + +func (c *RuntimeConfig) PartitionOrEmpty() string { return "" } diff --git a/agent/config/runtime_oss_test.go b/agent/config/runtime_oss_test.go index 4635302fb..4e9a87ffe 100644 --- a/agent/config/runtime_oss_test.go +++ b/agent/config/runtime_oss_test.go @@ -2,6 +2,14 @@ package config +import ( + "fmt" + "os" + "testing" + + "github.com/hashicorp/consul/sdk/testutil" +) + var testRuntimeConfigSanitizeExpectedFilename = "TestRuntimeConfig_Sanitize.golden" func entFullRuntimeConfig(rt *RuntimeConfig) {} @@ -19,3 +27,55 @@ var enterpriseConfigKeyWarnings = []string{ enterpriseConfigKeyError{key: "acl.tokens.managed_service_provider"}.Error(), enterpriseConfigKeyError{key: "audit"}.Error(), } + +// OSS-only equivalent of TestConfigFlagsAndEdgecases +// used for flags validated in ent-only code +func TestLoad_IntegrationWithFlags_OSS(t *testing.T) { + dataDir := testutil.TempDir(t, "consul") + defer os.RemoveAll(dataDir) + + tests := []testCase{ + { + desc: "partition config on a client", + args: []string{ + `-data-dir=` + dataDir, + `-server=false`, + }, + json: []string{`{ "partition": "foo" }`}, + hcl: []string{`partition = "foo"`}, + expectedWarnings: []string{ + `"partition" is a Consul Enterprise configuration and will have no effect`, + }, + expected: func(rt *RuntimeConfig) { + rt.DataDir = dataDir + rt.ServerMode = false + }, + }, + { + desc: "partition config on a server", + args: []string{ + `-data-dir=` + dataDir, + `-server`, + }, + json: []string{`{ "partition": "foo" }`}, + hcl: []string{`partition = "foo"`}, + expectedWarnings: []string{ + `"partition" is a Consul Enterprise configuration and will have no effect`, + }, + expected: func(rt *RuntimeConfig) { + rt.DataDir = dataDir + rt.ServerMode = true + rt.LeaveOnTerm = false + rt.SkipLeaveOnInt = true + rt.RPCConfig.EnableStreaming = true + }, + }, + } + + for _, tc := range tests { + for _, format := range []string{"json", "hcl"} { + name := fmt.Sprintf("%v_%v", tc.desc, format) + t.Run(name, tc.run(format, dataDir)) + } + } +} diff --git a/agent/config/testdata/full-config.hcl b/agent/config/testdata/full-config.hcl index a91a6f255..0f113d59a 100644 --- a/agent/config/testdata/full-config.hcl +++ b/agent/config/testdata/full-config.hcl @@ -297,6 +297,7 @@ node_meta { } node_name = "otlLxGaI" non_voting_server = true +partition = "" performance { leave_drain_time = "8265s" raft_multiplier = 5 diff --git a/agent/config/testdata/full-config.json b/agent/config/testdata/full-config.json index 5c86b9bc3..2392eb8eb 100644 --- a/agent/config/testdata/full-config.json +++ b/agent/config/testdata/full-config.json @@ -297,6 +297,7 @@ }, "node_name": "otlLxGaI", "non_voting_server": true, + "partition": "", "performance": { "leave_drain_time": "8265s", "raft_multiplier": 5, diff --git a/command/agent/agent.go b/command/agent/agent.go index fade33e6b..37ec6de84 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -195,6 +195,9 @@ func (c *cmd) run(args []string) int { ui.Info(fmt.Sprintf(" Version: '%s'", c.versionHuman)) ui.Info(fmt.Sprintf(" Node ID: '%s'", config.NodeID)) ui.Info(fmt.Sprintf(" Node name: '%s'", config.NodeName)) + if ap := config.PartitionOrEmpty(); ap != "" { + ui.Info(fmt.Sprintf(" Partition: '%s'", ap)) + } ui.Info(fmt.Sprintf(" Datacenter: '%s' (Segment: '%s')", config.Datacenter, segment)) ui.Info(fmt.Sprintf(" Server: %v (Bootstrap: %v)", config.ServerMode, config.Bootstrap)) ui.Info(fmt.Sprintf(" Client Addr: %v (HTTP: %d, HTTPS: %d, gRPC: %d, DNS: %d)", config.ClientAddrs,