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.
This commit is contained in:
R.B. Boyer 2020-01-23 09:59:31 -06:00 committed by GitHub
parent db3f0711db
commit 0f44bcd3d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 4 deletions

View File

@ -689,9 +689,9 @@ func (b *Builder) Build() (rt RuntimeConfig, err error) {
aclsEnabled = b.boolVal(c.ACL.Enabled) aclsEnabled = b.boolVal(c.ACL.Enabled)
} }
aclDC := primaryDatacenter // Set the primary DC if it wasn't set.
if aclsEnabled && aclDC == "" { if primaryDatacenter == "" {
aclDC = datacenter primaryDatacenter = datacenter
} }
enableTokenReplication := false enableTokenReplication := false
@ -776,7 +776,7 @@ func (b *Builder) Build() (rt RuntimeConfig, err error) {
ACLsEnabled: aclsEnabled, ACLsEnabled: aclsEnabled,
ACLAgentMasterToken: b.stringValWithDefault(c.ACL.Tokens.AgentMaster, b.stringVal(c.ACLAgentMasterToken)), ACLAgentMasterToken: b.stringValWithDefault(c.ACL.Tokens.AgentMaster, b.stringVal(c.ACLAgentMasterToken)),
ACLAgentToken: b.stringValWithDefault(c.ACL.Tokens.Agent, b.stringVal(c.ACLAgentToken)), 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)), ACLDefaultPolicy: b.stringValWithDefault(c.ACL.DefaultPolicy, b.stringVal(c.ACLDefaultPolicy)),
ACLDownPolicy: b.stringValWithDefault(c.ACL.DownPolicy, b.stringVal(c.ACLDownPolicy)), ACLDownPolicy: b.stringValWithDefault(c.ACL.DownPolicy, b.stringVal(c.ACLDownPolicy)),
ACLEnableKeyListPolicy: b.boolValWithDefault(c.ACL.EnableKeyListPolicy, b.boolVal(c.ACLEnableKeyListPolicy)), ACLEnableKeyListPolicy: b.boolValWithDefault(c.ACL.EnableKeyListPolicy, b.boolVal(c.ACLEnableKeyListPolicy)),

View File

@ -196,6 +196,8 @@ func TestConfigFlagsAndEdgecases(t *testing.T) {
}, },
patch: func(rt *RuntimeConfig) { patch: func(rt *RuntimeConfig) {
rt.Datacenter = "a" rt.Datacenter = "a"
rt.ACLDatacenter = "a"
rt.PrimaryDatacenter = "a"
rt.DataDir = dataDir rt.DataDir = dataDir
}, },
pre: func() { pre: func() {
@ -210,6 +212,8 @@ func TestConfigFlagsAndEdgecases(t *testing.T) {
}, },
patch: func(rt *RuntimeConfig) { patch: func(rt *RuntimeConfig) {
rt.Datacenter = "a" rt.Datacenter = "a"
rt.ACLDatacenter = "a"
rt.PrimaryDatacenter = "a"
rt.DataDir = dataDir rt.DataDir = dataDir
}, },
pre: func() { pre: func() {
@ -225,6 +229,8 @@ func TestConfigFlagsAndEdgecases(t *testing.T) {
}, },
patch: func(rt *RuntimeConfig) { patch: func(rt *RuntimeConfig) {
rt.Datacenter = "b" rt.Datacenter = "b"
rt.ACLDatacenter = "b"
rt.PrimaryDatacenter = "b"
rt.DataDir = dataDir rt.DataDir = dataDir
}, },
pre: func() { pre: func() {
@ -254,6 +260,8 @@ func TestConfigFlagsAndEdgecases(t *testing.T) {
}, },
patch: func(rt *RuntimeConfig) { patch: func(rt *RuntimeConfig) {
rt.Datacenter = "a" rt.Datacenter = "a"
rt.ACLDatacenter = "a"
rt.PrimaryDatacenter = "a"
rt.DataDir = dataDir rt.DataDir = dataDir
}, },
}, },
@ -436,6 +444,8 @@ func TestConfigFlagsAndEdgecases(t *testing.T) {
}, },
patch: func(rt *RuntimeConfig) { patch: func(rt *RuntimeConfig) {
rt.Datacenter = "a" rt.Datacenter = "a"
rt.ACLDatacenter = "a"
rt.PrimaryDatacenter = "a"
rt.DataDir = dataDir rt.DataDir = dataDir
}, },
pre: func() { pre: func() {
@ -452,6 +462,8 @@ func TestConfigFlagsAndEdgecases(t *testing.T) {
}, },
patch: func(rt *RuntimeConfig) { patch: func(rt *RuntimeConfig) {
rt.Datacenter = "a" rt.Datacenter = "a"
rt.ACLDatacenter = "a"
rt.PrimaryDatacenter = "a"
rt.DataDir = dataDir rt.DataDir = dataDir
}, },
pre: func() { pre: func() {
@ -467,6 +479,8 @@ func TestConfigFlagsAndEdgecases(t *testing.T) {
}, },
patch: func(rt *RuntimeConfig) { patch: func(rt *RuntimeConfig) {
rt.Datacenter = "a" rt.Datacenter = "a"
rt.ACLDatacenter = "a"
rt.PrimaryDatacenter = "a"
rt.DataDir = dataDir rt.DataDir = dataDir
}, },
pre: func() { pre: func() {
@ -1398,6 +1412,8 @@ func TestConfigFlagsAndEdgecases(t *testing.T) {
rt.Bootstrap = false rt.Bootstrap = false
rt.BootstrapExpect = 0 rt.BootstrapExpect = 0
rt.Datacenter = "b" rt.Datacenter = "b"
rt.ACLDatacenter = "b"
rt.PrimaryDatacenter = "b"
rt.StartJoinAddrsLAN = []string{"a", "b", "c", "d"} rt.StartJoinAddrsLAN = []string{"a", "b", "c", "d"}
rt.NodeMeta = map[string]string{"a": "c"} rt.NodeMeta = map[string]string{"a": "c"}
rt.DataDir = dataDir rt.DataDir = dataDir
@ -1453,6 +1469,8 @@ func TestConfigFlagsAndEdgecases(t *testing.T) {
rt.SerfAdvertiseAddrLAN = tcpAddr("1.1.1.1:8301") rt.SerfAdvertiseAddrLAN = tcpAddr("1.1.1.1:8301")
rt.SerfAdvertiseAddrWAN = tcpAddr("2.2.2.2:8302") rt.SerfAdvertiseAddrWAN = tcpAddr("2.2.2.2:8302")
rt.Datacenter = "b" 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.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.NodeMeta = map[string]string{"a": "c"}
rt.SerfBindAddrLAN = tcpAddr("3.3.3.3:8301") rt.SerfBindAddrLAN = tcpAddr("3.3.3.3:8301")
@ -1503,6 +1521,8 @@ func TestConfigFlagsAndEdgecases(t *testing.T) {
hcl: []string{`datacenter = "A"`}, hcl: []string{`datacenter = "A"`},
patch: func(rt *RuntimeConfig) { patch: func(rt *RuntimeConfig) {
rt.Datacenter = "a" rt.Datacenter = "a"
rt.ACLDatacenter = "a"
rt.PrimaryDatacenter = "a"
rt.DataDir = dataDir rt.DataDir = dataDir
}, },
}, },