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

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

View File

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

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

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

View File

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