diff --git a/command/agent/agent.go b/command/agent/agent.go index e047fae0d1..29d8dc0424 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -161,6 +161,11 @@ func Create(config *Config, logOutput io.Writer) (*Agent, error) { config.AdvertiseAddrWan = config.AdvertiseAddr } + // Create the default set of tagged addresses. + config.TaggedAddresses = map[string]string{ + "wan": config.AdvertiseAddrWan, + } + agent := &Agent{ config: config, logger: log.New(logOutput, "", log.LstdFlags), @@ -287,7 +292,6 @@ func (a *Agent) consulConfig() *consul.Config { if a.config.AdvertiseAddrs.SerfWan != nil { base.SerfWANConfig.MemberlistConfig.AdvertiseAddr = a.config.AdvertiseAddrs.SerfWan.IP.String() base.SerfWANConfig.MemberlistConfig.AdvertisePort = a.config.AdvertiseAddrs.SerfWan.Port - a.config.AdvertiseAddrWan = a.config.AdvertiseAddrs.SerfWan.IP.String() } if a.config.AdvertiseAddrs.RPC != nil { base.RPCAdvertise = a.config.AdvertiseAddrs.RPC diff --git a/command/agent/agent_test.go b/command/agent/agent_test.go index fbc0fb75aa..fb28fa3df5 100644 --- a/command/agent/agent_test.go +++ b/command/agent/agent_test.go @@ -168,9 +168,11 @@ func TestAgent_CheckAdvertiseAddrsSettings(t *testing.T) { if rpc != c.AdvertiseAddrs.RPC { t.Fatalf("RPC is not properly set to %v: %s", c.AdvertiseAddrs.RPC, rpc) } - advertiseWanAddress := agent.config.AdvertiseAddrWan - if serfWanAddr != advertiseWanAddress { - t.Fatalf("AdvertiseAddrWan is not properly set to '%s': %s", serfWanAddr, advertiseWanAddress) + expected := map[string]string{ + "wan": agent.config.AdvertiseAddrWan, + } + if !reflect.DeepEqual(agent.config.TaggedAddresses, expected) { + t.Fatalf("Tagged addresses not set up properly: %v", agent.config.TaggedAddresses) } } diff --git a/command/agent/config.go b/command/agent/config.go index 2b625eb1f2..f56e3e740f 100644 --- a/command/agent/config.go +++ b/command/agent/config.go @@ -190,7 +190,8 @@ type Config struct { AdvertiseAddrWan string `mapstructure:"advertise_addr_wan"` // TranslateWanAddrs controls whether or not Consul should prefer - // the AdvertiseAddrWan address when doing lookups in remote datacenters. + // the "wan" tagged address when doing lookups in remote datacenters. + // See TaggedAddresses below for more details. TranslateWanAddrs bool `mapstructure:"translate_wan_addrs"` // Port configurations @@ -199,6 +200,14 @@ type Config struct { // Address configurations Addresses AddressConfig + // Tagged addresses. These are used to publish a set of addresses for + // for a node, which can be used by the remote agent. We currently + // populate only the "wan" tag based on the SerfWan advertise address, + // but this structure is here for possible future features with other + // user-defined tags. The "wan" tag will be used by remote agents if + // they are configured with TranslateWanAddrs set to true. + TaggedAddresses map[string]string + // LeaveOnTerm controls if Serf does a graceful leave when receiving // the TERM signal. Defaults false. This can be changed on reload. LeaveOnTerm bool `mapstructure:"leave_on_terminate"` diff --git a/command/agent/dns.go b/command/agent/dns.go index 98863d11d8..9749b19f05 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.Addresses["wan"] + addr = out.NodeServices.Node.TaggedAddresses["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.Addresses["wan"] != "" { - addr = node.Node.Addresses["wan"] + } else if useWan == true && node.Node.TaggedAddresses["wan"] != "" { + addr = node.Node.TaggedAddresses["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.Addresses["wan"] != "" { - addr = node.Node.Addresses["wan"] + } else if useWan == true && node.Node.TaggedAddresses["wan"] != "" { + addr = node.Node.TaggedAddresses["wan"] } // Add the extra record diff --git a/command/agent/dns_test.go b/command/agent/dns_test.go index 0ef519c60a..cb017c9121 100644 --- a/command/agent/dns_test.go +++ b/command/agent/dns_test.go @@ -117,7 +117,7 @@ func TestDNS_NodeLookup(t *testing.T) { Datacenter: "dc1", Node: "foo", Address: "127.0.0.1", - Addresses: map[string]string { + TaggedAddresses: map[string]string{ "wan": "127.0.0.2", }, } @@ -758,7 +758,7 @@ func TestDNS_ServiceLookup_WanAddress(t *testing.T) { Datacenter: "dc2", Node: "foo", Address: "127.0.0.1", - Addresses: map[string]string { + TaggedAddresses: map[string]string{ "wan": "127.0.0.2", }, Service: &structs.NodeService{ diff --git a/command/agent/local.go b/command/agent/local.go index 0381ef4888..15752555a7 100644 --- a/command/agent/local.go +++ b/command/agent/local.go @@ -523,14 +523,12 @@ func (l *localState) deleteCheck(id string) error { // syncService is used to sync a service to the server func (l *localState) syncService(id string) error { req := structs.RegisterRequest{ - Datacenter: l.config.Datacenter, - Node: l.config.NodeName, - Address: l.config.AdvertiseAddr, - Addresses: map[string]string { - "wan": l.config.AdvertiseAddrWan, - }, - Service: l.services[id], - WriteRequest: structs.WriteRequest{Token: l.serviceToken(id)}, + Datacenter: l.config.Datacenter, + Node: l.config.NodeName, + Address: l.config.AdvertiseAddr, + TaggedAddresses: l.config.TaggedAddresses, + Service: l.services[id], + WriteRequest: structs.WriteRequest{Token: l.serviceToken(id)}, } // If the service has associated checks that are out of sync, @@ -583,15 +581,13 @@ func (l *localState) syncCheck(id string) error { } req := structs.RegisterRequest{ - Datacenter: l.config.Datacenter, - Node: l.config.NodeName, - Address: l.config.AdvertiseAddr, - Addresses: map[string]string { - "wan": l.config.AdvertiseAddrWan, - }, - Service: service, - Check: l.checks[id], - WriteRequest: structs.WriteRequest{Token: l.checkToken(id)}, + Datacenter: l.config.Datacenter, + Node: l.config.NodeName, + Address: l.config.AdvertiseAddr, + TaggedAddresses: l.config.TaggedAddresses, + Service: service, + Check: l.checks[id], + WriteRequest: structs.WriteRequest{Token: l.checkToken(id)}, } var out struct{} err := l.iface.RPC("Catalog.Register", &req, &out) diff --git a/consul/fsm.go b/consul/fsm.go index 59231e65f7..9f786024ab 100644 --- a/consul/fsm.go +++ b/consul/fsm.go @@ -472,11 +472,9 @@ func (s *consulSnapshot) persistNodes(sink raft.SnapshotSink, for node := nodes.Next(); node != nil; node = nodes.Next() { n := node.(*structs.Node) req := structs.RegisterRequest{ - Node: n.Node, - Address: n.Address, - Addresses: map[string]string { - "wan": n.Addresses["wan"], - }, + Node: n.Node, + Address: n.Address, + TaggedAddresses: n.TaggedAddresses, } // Register the node itself diff --git a/consul/leader.go b/consul/leader.go index 92909090c0..374e7a9a22 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.Addresses["wan"] != member.Tags["wan_addr"] { + if node.TaggedAddresses["wan"] != member.Tags["wan_addr"] { goto AFTER_CHECK } @@ -423,10 +423,10 @@ AFTER_CHECK: Datacenter: s.config.Datacenter, Node: member.Name, Address: member.Addr.String(), - Addresses: map[string]string { + TaggedAddresses: map[string]string{ "wan": member.Tags["wan_addr"], }, - Service: service, + Service: service, Check: &structs.HealthCheck{ Node: member.Name, CheckID: SerfCheckID, @@ -468,7 +468,7 @@ func (s *Server) handleFailedMember(member serf.Member) error { Datacenter: s.config.Datacenter, Node: member.Name, Address: member.Addr.String(), - Addresses: map[string]string { + TaggedAddresses: map[string]string{ "wan": member.Tags["wan_addr"], }, Check: &structs.HealthCheck{ diff --git a/consul/state/state_store.go b/consul/state/state_store.go index 4ca07c7329..473775caef 100644 --- a/consul/state/state_store.go +++ b/consul/state/state_store.go @@ -474,7 +474,11 @@ 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, Addresses: req.Addresses} + node := &structs.Node{ + Node: req.Node, + Address: req.Address, + TaggedAddresses: req.TaggedAddresses, + } if err := s.ensureNodeTxn(tx, idx, watches, node); err != nil { return fmt.Errorf("failed inserting node: %s", err) } @@ -1373,9 +1377,9 @@ func (s *StateStore) parseNodes(tx *memdb.Txn, idx uint64, // Create the wrapped node dump := &structs.NodeInfo{ - Node: node.Node, - Address: node.Address, - Addresses: node.Addresses, + Node: node.Node, + Address: node.Address, + TaggedAddresses: node.TaggedAddresses, } // Query the node services diff --git a/consul/structs/structs.go b/consul/structs/structs.go index 0d86d77ed1..3e7ef5955e 100644 --- a/consul/structs/structs.go +++ b/consul/structs/structs.go @@ -159,13 +159,13 @@ type QueryMeta struct { // to register a node as providing a service. If no service // is provided, the node is registered. type RegisterRequest struct { - Datacenter string - Node string - Address string - Addresses map[string]string - Service *NodeService - Check *HealthCheck - Checks HealthChecks + Datacenter string + Node string + Address string + TaggedAddresses map[string]string + Service *NodeService + Check *HealthCheck + Checks HealthChecks WriteRequest } @@ -246,9 +246,9 @@ func (r *ChecksInStateRequest) RequestDatacenter() string { // Used to return information about a node type Node struct { - Node string - Address string - Addresses map[string]string + Node string + Address string + TaggedAddresses map[string]string RaftIndex } @@ -440,11 +440,11 @@ OUTER: // a node. This is currently used for the UI only, as it is // rather expensive to generate. type NodeInfo struct { - Node string - Address string - Addresses map[string]string - Services []*NodeService - Checks []*HealthCheck + Node string + Address string + TaggedAddresses map[string]string + Services []*NodeService + Checks []*HealthCheck } // NodeDump is used to dump all the nodes with all their