From de8fd561d05506f7921f65b1f702c4d27a2009a6 Mon Sep 17 00:00:00 2001 From: Evan Gilman Date: Tue, 26 Jan 2016 10:30:28 -0800 Subject: [PATCH] Use a map for additional node addresses --- command/agent/dns.go | 10 +++++----- command/agent/dns_test.go | 8 ++++++-- command/agent/local.go | 8 ++++++-- consul/fsm.go | 4 +++- consul/leader.go | 10 +++++++--- consul/state/state_store.go | 4 ++-- consul/structs/structs.go | 6 +++--- 7 files changed, 32 insertions(+), 18 deletions(-) diff --git a/command/agent/dns.go b/command/agent/dns.go index bc150f0541..98863d11d8 100644 --- a/command/agent/dns.go +++ b/command/agent/dns.go @@ -405,7 +405,7 @@ RPC: // Determine whether we should use the WAN address or not addr := out.NodeServices.Node.Address if d.agent.config.TranslateWanAddrs && datacenter != d.agent.config.Datacenter { - addr = out.NodeServices.Node.WanAddress + addr = out.NodeServices.Node.Addresses["wan"] } // Add the node record @@ -674,8 +674,8 @@ func (d *DNSServer) serviceNodeRecords(nodes structs.CheckServiceNodes, req, res addr := node.Node.Address if node.Service.Address != "" { addr = node.Service.Address - } else if useWan == true && node.Node.WanAddress != "" { - addr = node.Node.WanAddress + } else if useWan == true && node.Node.Addresses["wan"] != "" { + addr = node.Node.Addresses["wan"] } // Avoid duplicate entries, possible if a node has @@ -724,8 +724,8 @@ func (d *DNSServer) serviceSRVRecords(dc string, nodes structs.CheckServiceNodes addr := node.Node.Address if node.Service.Address != "" { addr = node.Service.Address - } else if useWan == true && node.Node.WanAddress != "" { - addr = node.Node.WanAddress + } else if useWan == true && node.Node.Addresses["wan"] != "" { + addr = node.Node.Addresses["wan"] } // Add the extra record diff --git a/command/agent/dns_test.go b/command/agent/dns_test.go index 2fca3afbc0..0ef519c60a 100644 --- a/command/agent/dns_test.go +++ b/command/agent/dns_test.go @@ -117,7 +117,9 @@ func TestDNS_NodeLookup(t *testing.T) { Datacenter: "dc1", Node: "foo", Address: "127.0.0.1", - WanAddress: "127.0.0.2", + Addresses: map[string]string { + "wan": "127.0.0.2", + }, } var out struct{} @@ -756,7 +758,9 @@ func TestDNS_ServiceLookup_WanAddress(t *testing.T) { Datacenter: "dc2", Node: "foo", Address: "127.0.0.1", - WanAddress: "127.0.0.2", + Addresses: map[string]string { + "wan": "127.0.0.2", + }, Service: &structs.NodeService{ Service: "db", }, diff --git a/command/agent/local.go b/command/agent/local.go index 7320db8dc0..0381ef4888 100644 --- a/command/agent/local.go +++ b/command/agent/local.go @@ -526,7 +526,9 @@ func (l *localState) syncService(id string) error { Datacenter: l.config.Datacenter, Node: l.config.NodeName, Address: l.config.AdvertiseAddr, - WanAddress: l.config.AdvertiseAddrWan, + Addresses: map[string]string { + "wan": l.config.AdvertiseAddrWan, + }, Service: l.services[id], WriteRequest: structs.WriteRequest{Token: l.serviceToken(id)}, } @@ -584,7 +586,9 @@ func (l *localState) syncCheck(id string) error { Datacenter: l.config.Datacenter, Node: l.config.NodeName, Address: l.config.AdvertiseAddr, - WanAddress: l.config.AdvertiseAddrWan, + Addresses: map[string]string { + "wan": l.config.AdvertiseAddrWan, + }, Service: service, Check: l.checks[id], WriteRequest: structs.WriteRequest{Token: l.checkToken(id)}, diff --git a/consul/fsm.go b/consul/fsm.go index 5ccf7afcdd..59231e65f7 100644 --- a/consul/fsm.go +++ b/consul/fsm.go @@ -474,7 +474,9 @@ func (s *consulSnapshot) persistNodes(sink raft.SnapshotSink, req := structs.RegisterRequest{ Node: n.Node, Address: n.Address, - WanAddress: n.WanAddress, + Addresses: map[string]string { + "wan": n.Addresses["wan"], + }, } // Register the node itself diff --git a/consul/leader.go b/consul/leader.go index 8f811ddeeb..92909090c0 100644 --- a/consul/leader.go +++ b/consul/leader.go @@ -381,7 +381,7 @@ func (s *Server) handleAliveMember(member serf.Member) error { } if node != nil && node.Address == member.Addr.String() { // Check if the WAN address was updated - if node.WanAddress != member.Tags["wan_addr"] { + if node.Addresses["wan"] != member.Tags["wan_addr"] { goto AFTER_CHECK } @@ -423,7 +423,9 @@ AFTER_CHECK: Datacenter: s.config.Datacenter, Node: member.Name, Address: member.Addr.String(), - WanAddress: member.Tags["wan_addr"], + Addresses: map[string]string { + "wan": member.Tags["wan_addr"], + }, Service: service, Check: &structs.HealthCheck{ Node: member.Name, @@ -466,7 +468,9 @@ func (s *Server) handleFailedMember(member serf.Member) error { Datacenter: s.config.Datacenter, Node: member.Name, Address: member.Addr.String(), - WanAddress: member.Tags["wan_addr"], + Addresses: map[string]string { + "wan": member.Tags["wan_addr"], + }, Check: &structs.HealthCheck{ Node: member.Name, CheckID: SerfCheckID, diff --git a/consul/state/state_store.go b/consul/state/state_store.go index 45f7d43f5a..4ca07c7329 100644 --- a/consul/state/state_store.go +++ b/consul/state/state_store.go @@ -474,7 +474,7 @@ func (s *StateStore) EnsureRegistration(idx uint64, req *structs.RegisterRequest func (s *StateStore) ensureRegistrationTxn(tx *memdb.Txn, idx uint64, watches *DumbWatchManager, req *structs.RegisterRequest) error { // Add the node. - node := &structs.Node{Node: req.Node, Address: req.Address, WanAddress: req.WanAddress} + node := &structs.Node{Node: req.Node, Address: req.Address, Addresses: req.Addresses} if err := s.ensureNodeTxn(tx, idx, watches, node); err != nil { return fmt.Errorf("failed inserting node: %s", err) } @@ -1375,7 +1375,7 @@ func (s *StateStore) parseNodes(tx *memdb.Txn, idx uint64, dump := &structs.NodeInfo{ Node: node.Node, Address: node.Address, - WanAddress: node.WanAddress, + Addresses: node.Addresses, } // Query the node services diff --git a/consul/structs/structs.go b/consul/structs/structs.go index 9417cecf8e..0d86d77ed1 100644 --- a/consul/structs/structs.go +++ b/consul/structs/structs.go @@ -162,7 +162,7 @@ type RegisterRequest struct { Datacenter string Node string Address string - WanAddress string + Addresses map[string]string Service *NodeService Check *HealthCheck Checks HealthChecks @@ -248,7 +248,7 @@ func (r *ChecksInStateRequest) RequestDatacenter() string { type Node struct { Node string Address string - WanAddress string + Addresses map[string]string RaftIndex } @@ -442,7 +442,7 @@ OUTER: type NodeInfo struct { Node string Address string - WanAddress string + Addresses map[string]string Services []*NodeService Checks []*HealthCheck }