Use a map for additional node addresses

This commit is contained in:
Evan Gilman 2016-01-26 10:30:28 -08:00 committed by James Phillips
parent 2f26771bb1
commit de8fd561d0
7 changed files with 32 additions and 18 deletions

View File

@ -405,7 +405,7 @@ RPC:
// Determine whether we should use the WAN address or not // Determine whether we should use the WAN address or not
addr := out.NodeServices.Node.Address addr := out.NodeServices.Node.Address
if d.agent.config.TranslateWanAddrs && datacenter != d.agent.config.Datacenter { 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 // Add the node record
@ -674,8 +674,8 @@ func (d *DNSServer) serviceNodeRecords(nodes structs.CheckServiceNodes, req, res
addr := node.Node.Address addr := node.Node.Address
if node.Service.Address != "" { if node.Service.Address != "" {
addr = node.Service.Address addr = node.Service.Address
} else if useWan == true && node.Node.WanAddress != "" { } else if useWan == true && node.Node.Addresses["wan"] != "" {
addr = node.Node.WanAddress addr = node.Node.Addresses["wan"]
} }
// Avoid duplicate entries, possible if a node has // 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 addr := node.Node.Address
if node.Service.Address != "" { if node.Service.Address != "" {
addr = node.Service.Address addr = node.Service.Address
} else if useWan == true && node.Node.WanAddress != "" { } else if useWan == true && node.Node.Addresses["wan"] != "" {
addr = node.Node.WanAddress addr = node.Node.Addresses["wan"]
} }
// Add the extra record // Add the extra record

View File

@ -117,7 +117,9 @@ func TestDNS_NodeLookup(t *testing.T) {
Datacenter: "dc1", Datacenter: "dc1",
Node: "foo", Node: "foo",
Address: "127.0.0.1", Address: "127.0.0.1",
WanAddress: "127.0.0.2", Addresses: map[string]string {
"wan": "127.0.0.2",
},
} }
var out struct{} var out struct{}
@ -756,7 +758,9 @@ func TestDNS_ServiceLookup_WanAddress(t *testing.T) {
Datacenter: "dc2", Datacenter: "dc2",
Node: "foo", Node: "foo",
Address: "127.0.0.1", Address: "127.0.0.1",
WanAddress: "127.0.0.2", Addresses: map[string]string {
"wan": "127.0.0.2",
},
Service: &structs.NodeService{ Service: &structs.NodeService{
Service: "db", Service: "db",
}, },

View File

@ -526,7 +526,9 @@ func (l *localState) syncService(id string) error {
Datacenter: l.config.Datacenter, Datacenter: l.config.Datacenter,
Node: l.config.NodeName, Node: l.config.NodeName,
Address: l.config.AdvertiseAddr, Address: l.config.AdvertiseAddr,
WanAddress: l.config.AdvertiseAddrWan, Addresses: map[string]string {
"wan": l.config.AdvertiseAddrWan,
},
Service: l.services[id], Service: l.services[id],
WriteRequest: structs.WriteRequest{Token: l.serviceToken(id)}, WriteRequest: structs.WriteRequest{Token: l.serviceToken(id)},
} }
@ -584,7 +586,9 @@ func (l *localState) syncCheck(id string) error {
Datacenter: l.config.Datacenter, Datacenter: l.config.Datacenter,
Node: l.config.NodeName, Node: l.config.NodeName,
Address: l.config.AdvertiseAddr, Address: l.config.AdvertiseAddr,
WanAddress: l.config.AdvertiseAddrWan, Addresses: map[string]string {
"wan": l.config.AdvertiseAddrWan,
},
Service: service, Service: service,
Check: l.checks[id], Check: l.checks[id],
WriteRequest: structs.WriteRequest{Token: l.checkToken(id)}, WriteRequest: structs.WriteRequest{Token: l.checkToken(id)},

View File

@ -474,7 +474,9 @@ func (s *consulSnapshot) persistNodes(sink raft.SnapshotSink,
req := structs.RegisterRequest{ req := structs.RegisterRequest{
Node: n.Node, Node: n.Node,
Address: n.Address, Address: n.Address,
WanAddress: n.WanAddress, Addresses: map[string]string {
"wan": n.Addresses["wan"],
},
} }
// Register the node itself // Register the node itself

View File

@ -381,7 +381,7 @@ func (s *Server) handleAliveMember(member serf.Member) error {
} }
if node != nil && node.Address == member.Addr.String() { if node != nil && node.Address == member.Addr.String() {
// Check if the WAN address was updated // 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 goto AFTER_CHECK
} }
@ -423,7 +423,9 @@ AFTER_CHECK:
Datacenter: s.config.Datacenter, Datacenter: s.config.Datacenter,
Node: member.Name, Node: member.Name,
Address: member.Addr.String(), Address: member.Addr.String(),
WanAddress: member.Tags["wan_addr"], Addresses: map[string]string {
"wan": member.Tags["wan_addr"],
},
Service: service, Service: service,
Check: &structs.HealthCheck{ Check: &structs.HealthCheck{
Node: member.Name, Node: member.Name,
@ -466,7 +468,9 @@ func (s *Server) handleFailedMember(member serf.Member) error {
Datacenter: s.config.Datacenter, Datacenter: s.config.Datacenter,
Node: member.Name, Node: member.Name,
Address: member.Addr.String(), Address: member.Addr.String(),
WanAddress: member.Tags["wan_addr"], Addresses: map[string]string {
"wan": member.Tags["wan_addr"],
},
Check: &structs.HealthCheck{ Check: &structs.HealthCheck{
Node: member.Name, Node: member.Name,
CheckID: SerfCheckID, CheckID: SerfCheckID,

View File

@ -474,7 +474,7 @@ func (s *StateStore) EnsureRegistration(idx uint64, req *structs.RegisterRequest
func (s *StateStore) ensureRegistrationTxn(tx *memdb.Txn, idx uint64, watches *DumbWatchManager, func (s *StateStore) ensureRegistrationTxn(tx *memdb.Txn, idx uint64, watches *DumbWatchManager,
req *structs.RegisterRequest) error { req *structs.RegisterRequest) error {
// Add the node. // 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 { if err := s.ensureNodeTxn(tx, idx, watches, node); err != nil {
return fmt.Errorf("failed inserting node: %s", err) return fmt.Errorf("failed inserting node: %s", err)
} }
@ -1375,7 +1375,7 @@ func (s *StateStore) parseNodes(tx *memdb.Txn, idx uint64,
dump := &structs.NodeInfo{ dump := &structs.NodeInfo{
Node: node.Node, Node: node.Node,
Address: node.Address, Address: node.Address,
WanAddress: node.WanAddress, Addresses: node.Addresses,
} }
// Query the node services // Query the node services

View File

@ -162,7 +162,7 @@ type RegisterRequest struct {
Datacenter string Datacenter string
Node string Node string
Address string Address string
WanAddress string Addresses map[string]string
Service *NodeService Service *NodeService
Check *HealthCheck Check *HealthCheck
Checks HealthChecks Checks HealthChecks
@ -248,7 +248,7 @@ func (r *ChecksInStateRequest) RequestDatacenter() string {
type Node struct { type Node struct {
Node string Node string
Address string Address string
WanAddress string Addresses map[string]string
RaftIndex RaftIndex
} }
@ -442,7 +442,7 @@ OUTER:
type NodeInfo struct { type NodeInfo struct {
Node string Node string
Address string Address string
WanAddress string Addresses map[string]string
Services []*NodeService Services []*NodeService
Checks []*HealthCheck Checks []*HealthCheck
} }