From 0f44bcd3d8d2ce86404667b71329e6a14c5ff03e Mon Sep 17 00:00:00 2001 From: "R.B. Boyer" Date: Thu, 23 Jan 2020 09:59:31 -0600 Subject: [PATCH] agent: default the primary_datacenter to the datacenter if not configured (#7111) Something similar already happens inside of the server (agent/consul/server.go) but by doing it in the general config parsing for the agent we can have agent-level code rely on the PrimaryDatacenter field, too. --- agent/config/builder.go | 8 ++++---- agent/config/runtime_test.go | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/agent/config/builder.go b/agent/config/builder.go index 917f27fca3..ab781000e2 100644 --- a/agent/config/builder.go +++ b/agent/config/builder.go @@ -689,9 +689,9 @@ func (b *Builder) Build() (rt RuntimeConfig, err error) { aclsEnabled = b.boolVal(c.ACL.Enabled) } - aclDC := primaryDatacenter - if aclsEnabled && aclDC == "" { - aclDC = datacenter + // Set the primary DC if it wasn't set. + if primaryDatacenter == "" { + primaryDatacenter = datacenter } enableTokenReplication := false @@ -776,7 +776,7 @@ func (b *Builder) Build() (rt RuntimeConfig, err error) { ACLsEnabled: aclsEnabled, ACLAgentMasterToken: b.stringValWithDefault(c.ACL.Tokens.AgentMaster, b.stringVal(c.ACLAgentMasterToken)), ACLAgentToken: b.stringValWithDefault(c.ACL.Tokens.Agent, b.stringVal(c.ACLAgentToken)), - ACLDatacenter: aclDC, + ACLDatacenter: primaryDatacenter, ACLDefaultPolicy: b.stringValWithDefault(c.ACL.DefaultPolicy, b.stringVal(c.ACLDefaultPolicy)), ACLDownPolicy: b.stringValWithDefault(c.ACL.DownPolicy, b.stringVal(c.ACLDownPolicy)), ACLEnableKeyListPolicy: b.boolValWithDefault(c.ACL.EnableKeyListPolicy, b.boolVal(c.ACLEnableKeyListPolicy)), diff --git a/agent/config/runtime_test.go b/agent/config/runtime_test.go index c53e16b186..c7ea1904c4 100644 --- a/agent/config/runtime_test.go +++ b/agent/config/runtime_test.go @@ -196,6 +196,8 @@ func TestConfigFlagsAndEdgecases(t *testing.T) { }, patch: func(rt *RuntimeConfig) { rt.Datacenter = "a" + rt.ACLDatacenter = "a" + rt.PrimaryDatacenter = "a" rt.DataDir = dataDir }, pre: func() { @@ -210,6 +212,8 @@ func TestConfigFlagsAndEdgecases(t *testing.T) { }, patch: func(rt *RuntimeConfig) { rt.Datacenter = "a" + rt.ACLDatacenter = "a" + rt.PrimaryDatacenter = "a" rt.DataDir = dataDir }, pre: func() { @@ -225,6 +229,8 @@ func TestConfigFlagsAndEdgecases(t *testing.T) { }, patch: func(rt *RuntimeConfig) { rt.Datacenter = "b" + rt.ACLDatacenter = "b" + rt.PrimaryDatacenter = "b" rt.DataDir = dataDir }, pre: func() { @@ -254,6 +260,8 @@ func TestConfigFlagsAndEdgecases(t *testing.T) { }, patch: func(rt *RuntimeConfig) { rt.Datacenter = "a" + rt.ACLDatacenter = "a" + rt.PrimaryDatacenter = "a" rt.DataDir = dataDir }, }, @@ -436,6 +444,8 @@ func TestConfigFlagsAndEdgecases(t *testing.T) { }, patch: func(rt *RuntimeConfig) { rt.Datacenter = "a" + rt.ACLDatacenter = "a" + rt.PrimaryDatacenter = "a" rt.DataDir = dataDir }, pre: func() { @@ -452,6 +462,8 @@ func TestConfigFlagsAndEdgecases(t *testing.T) { }, patch: func(rt *RuntimeConfig) { rt.Datacenter = "a" + rt.ACLDatacenter = "a" + rt.PrimaryDatacenter = "a" rt.DataDir = dataDir }, pre: func() { @@ -467,6 +479,8 @@ func TestConfigFlagsAndEdgecases(t *testing.T) { }, patch: func(rt *RuntimeConfig) { rt.Datacenter = "a" + rt.ACLDatacenter = "a" + rt.PrimaryDatacenter = "a" rt.DataDir = dataDir }, pre: func() { @@ -1398,6 +1412,8 @@ func TestConfigFlagsAndEdgecases(t *testing.T) { rt.Bootstrap = false rt.BootstrapExpect = 0 rt.Datacenter = "b" + rt.ACLDatacenter = "b" + rt.PrimaryDatacenter = "b" rt.StartJoinAddrsLAN = []string{"a", "b", "c", "d"} rt.NodeMeta = map[string]string{"a": "c"} rt.DataDir = dataDir @@ -1453,6 +1469,8 @@ func TestConfigFlagsAndEdgecases(t *testing.T) { rt.SerfAdvertiseAddrLAN = tcpAddr("1.1.1.1:8301") rt.SerfAdvertiseAddrWAN = tcpAddr("2.2.2.2:8302") rt.Datacenter = "b" + rt.ACLDatacenter = "b" + rt.PrimaryDatacenter = "b" rt.DNSRecursors = []string{"1.2.3.6", "5.6.7.10", "1.2.3.5", "5.6.7.9"} rt.NodeMeta = map[string]string{"a": "c"} rt.SerfBindAddrLAN = tcpAddr("3.3.3.3:8301") @@ -1503,6 +1521,8 @@ func TestConfigFlagsAndEdgecases(t *testing.T) { hcl: []string{`datacenter = "A"`}, patch: func(rt *RuntimeConfig) { rt.Datacenter = "a" + rt.ACLDatacenter = "a" + rt.PrimaryDatacenter = "a" rt.DataDir = dataDir }, },