Sets up config for more address tags down the road, renames struct members.

This commit is contained in:
James Phillips 2016-02-07 10:37:34 -08:00
parent 71cf39b5f8
commit 79bd1fd4bb
10 changed files with 70 additions and 57 deletions

View File

@ -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

View File

@ -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)
}
}

View File

@ -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"`

View File

@ -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

View File

@ -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{

View File

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

View File

@ -474,9 +474,7 @@ func (s *consulSnapshot) persistNodes(sink raft.SnapshotSink,
req := structs.RegisterRequest{
Node: n.Node,
Address: n.Address,
Addresses: map[string]string {
"wan": n.Addresses["wan"],
},
TaggedAddresses: n.TaggedAddresses,
}
// 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() {
// 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,7 +423,7 @@ 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,
@ -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{

View File

@ -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)
}
@ -1375,7 +1379,7 @@ func (s *StateStore) parseNodes(tx *memdb.Txn, idx uint64,
dump := &structs.NodeInfo{
Node: node.Node,
Address: node.Address,
Addresses: node.Addresses,
TaggedAddresses: node.TaggedAddresses,
}
// Query the node services

View File

@ -162,7 +162,7 @@ type RegisterRequest struct {
Datacenter string
Node string
Address string
Addresses map[string]string
TaggedAddresses 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
Addresses map[string]string
TaggedAddresses map[string]string
RaftIndex
}
@ -442,7 +442,7 @@ OUTER:
type NodeInfo struct {
Node string
Address string
Addresses map[string]string
TaggedAddresses map[string]string
Services []*NodeService
Checks []*HealthCheck
}