mirror of
https://github.com/status-im/consul.git
synced 2025-01-09 13:26:07 +00:00
agent: refactor: make address translation part of the agent
This commit is contained in:
parent
bd03f8a8ed
commit
63447a0cf3
@ -74,7 +74,7 @@ func (s *HTTPServer) CatalogNodes(resp http.ResponseWriter, req *http.Request) (
|
|||||||
if err := s.agent.RPC("Catalog.ListNodes", &args, &out); err != nil {
|
if err := s.agent.RPC("Catalog.ListNodes", &args, &out); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
translateAddresses(s.agent.config, args.Datacenter, out.Nodes)
|
s.agent.TranslateAddresses(args.Datacenter, out.Nodes)
|
||||||
|
|
||||||
// Use empty list instead of nil
|
// Use empty list instead of nil
|
||||||
if out.Nodes == nil {
|
if out.Nodes == nil {
|
||||||
@ -134,7 +134,7 @@ func (s *HTTPServer) CatalogServiceNodes(resp http.ResponseWriter, req *http.Req
|
|||||||
if err := s.agent.RPC("Catalog.ServiceNodes", &args, &out); err != nil {
|
if err := s.agent.RPC("Catalog.ServiceNodes", &args, &out); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
translateAddresses(s.agent.config, args.Datacenter, out.ServiceNodes)
|
s.agent.TranslateAddresses(args.Datacenter, out.ServiceNodes)
|
||||||
|
|
||||||
// Use empty list instead of nil
|
// Use empty list instead of nil
|
||||||
if out.ServiceNodes == nil {
|
if out.ServiceNodes == nil {
|
||||||
@ -170,7 +170,7 @@ func (s *HTTPServer) CatalogNodeServices(resp http.ResponseWriter, req *http.Req
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if out.NodeServices != nil && out.NodeServices.Node != nil {
|
if out.NodeServices != nil && out.NodeServices.Node != nil {
|
||||||
translateAddresses(s.agent.config, args.Datacenter, out.NodeServices.Node)
|
s.agent.TranslateAddresses(args.Datacenter, out.NodeServices.Node)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use empty list instead of nil
|
// Use empty list instead of nil
|
||||||
|
@ -414,7 +414,7 @@ RPC:
|
|||||||
// Add the node record
|
// Add the node record
|
||||||
n := out.NodeServices.Node
|
n := out.NodeServices.Node
|
||||||
edns := req.IsEdns0() != nil
|
edns := req.IsEdns0() != nil
|
||||||
addr := translateAddress(d.agent.config, datacenter, n.Address, n.TaggedAddresses)
|
addr := d.agent.TranslateAddress(datacenter, n.Address, n.TaggedAddresses)
|
||||||
records := d.formatNodeRecord(out.NodeServices.Node, addr,
|
records := d.formatNodeRecord(out.NodeServices.Node, addr,
|
||||||
req.Question[0].Name, qType, d.config.NodeTTL, edns)
|
req.Question[0].Name, qType, d.config.NodeTTL, edns)
|
||||||
if records != nil {
|
if records != nil {
|
||||||
@ -784,7 +784,7 @@ func (d *DNSServer) serviceNodeRecords(dc string, nodes structs.CheckServiceNode
|
|||||||
for _, node := range nodes {
|
for _, node := range nodes {
|
||||||
// Start with the translated address but use the service address,
|
// Start with the translated address but use the service address,
|
||||||
// if specified.
|
// if specified.
|
||||||
addr := translateAddress(d.agent.config, dc, node.Node.Address, node.Node.TaggedAddresses)
|
addr := d.agent.TranslateAddress(dc, node.Node.Address, node.Node.TaggedAddresses)
|
||||||
if node.Service.Address != "" {
|
if node.Service.Address != "" {
|
||||||
addr = node.Service.Address
|
addr = node.Service.Address
|
||||||
}
|
}
|
||||||
@ -841,7 +841,7 @@ func (d *DNSServer) serviceSRVRecords(dc string, nodes structs.CheckServiceNodes
|
|||||||
|
|
||||||
// Start with the translated address but use the service address,
|
// Start with the translated address but use the service address,
|
||||||
// if specified.
|
// if specified.
|
||||||
addr := translateAddress(d.agent.config, dc, node.Node.Address, node.Node.TaggedAddresses)
|
addr := d.agent.TranslateAddress(dc, node.Node.Address, node.Node.TaggedAddresses)
|
||||||
if node.Service.Address != "" {
|
if node.Service.Address != "" {
|
||||||
addr = node.Service.Address
|
addr = node.Service.Address
|
||||||
}
|
}
|
||||||
|
@ -171,7 +171,7 @@ func (s *HTTPServer) HealthServiceNodes(resp http.ResponseWriter, req *http.Requ
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Translate addresses after filtering so we don't waste effort.
|
// Translate addresses after filtering so we don't waste effort.
|
||||||
translateAddresses(s.agent.config, args.Datacenter, out.Nodes)
|
s.agent.TranslateAddresses(args.Datacenter, out.Nodes)
|
||||||
|
|
||||||
// Use empty list instead of nil
|
// Use empty list instead of nil
|
||||||
if out.Nodes == nil {
|
if out.Nodes == nil {
|
||||||
|
@ -122,7 +122,7 @@ func (s *HTTPServer) preparedQueryExecute(id string, resp http.ResponseWriter, r
|
|||||||
// a query can fail over to a different DC than where the execute request
|
// a query can fail over to a different DC than where the execute request
|
||||||
// was sent to. That's why we use the reply's DC and not the one from
|
// was sent to. That's why we use the reply's DC and not the one from
|
||||||
// the args.
|
// the args.
|
||||||
translateAddresses(s.agent.config, reply.Datacenter, reply.Nodes)
|
s.agent.TranslateAddresses(reply.Datacenter, reply.Nodes)
|
||||||
|
|
||||||
// Use empty list instead of nil.
|
// Use empty list instead of nil.
|
||||||
if reply.Nodes == nil {
|
if reply.Nodes == nil {
|
||||||
|
@ -6,11 +6,11 @@ import (
|
|||||||
"github.com/hashicorp/consul/agent/consul/structs"
|
"github.com/hashicorp/consul/agent/consul/structs"
|
||||||
)
|
)
|
||||||
|
|
||||||
// translateAddress is used to provide the final, translated address for a node,
|
// TranslateAddress is used to provide the final, translated address for a node,
|
||||||
// depending on how the agent and the other node are configured. The dc
|
// depending on how the agent and the other node are configured. The dc
|
||||||
// parameter is the dc the datacenter this node is from.
|
// parameter is the dc the datacenter this node is from.
|
||||||
func translateAddress(config *Config, dc string, addr string, taggedAddresses map[string]string) string {
|
func (a *Agent) TranslateAddress(dc string, addr string, taggedAddresses map[string]string) string {
|
||||||
if config.TranslateWanAddrs && (config.Datacenter != dc) {
|
if a.config.TranslateWanAddrs && (a.config.Datacenter != dc) {
|
||||||
wanAddr := taggedAddresses["wan"]
|
wanAddr := taggedAddresses["wan"]
|
||||||
if wanAddr != "" {
|
if wanAddr != "" {
|
||||||
addr = wanAddr
|
addr = wanAddr
|
||||||
@ -19,10 +19,10 @@ func translateAddress(config *Config, dc string, addr string, taggedAddresses ma
|
|||||||
return addr
|
return addr
|
||||||
}
|
}
|
||||||
|
|
||||||
// translateAddresses translates addresses in the given structure into the
|
// TranslateAddresses translates addresses in the given structure into the
|
||||||
// final, translated address, depending on how the agent and the other node are
|
// final, translated address, depending on how the agent and the other node are
|
||||||
// configured. The dc parameter is the datacenter this structure is from.
|
// configured. The dc parameter is the datacenter this structure is from.
|
||||||
func translateAddresses(config *Config, dc string, subj interface{}) {
|
func (a *Agent) TranslateAddresses(dc string, subj interface{}) {
|
||||||
// CAUTION - SUBTLE! An agent running on a server can, in some cases,
|
// CAUTION - SUBTLE! An agent running on a server can, in some cases,
|
||||||
// return pointers directly into the immutable state store for
|
// return pointers directly into the immutable state store for
|
||||||
// performance (it's via the in-memory RPC mechanism). It's never safe
|
// performance (it's via the in-memory RPC mechanism). It's never safe
|
||||||
@ -34,7 +34,7 @@ func translateAddresses(config *Config, dc string, subj interface{}) {
|
|||||||
// done. This also happens to skip looking at any of the incoming
|
// done. This also happens to skip looking at any of the incoming
|
||||||
// structure for the common case of not needing to translate, so it will
|
// structure for the common case of not needing to translate, so it will
|
||||||
// skip a lot of work if no translation needs to be done.
|
// skip a lot of work if no translation needs to be done.
|
||||||
if !config.TranslateWanAddrs || (config.Datacenter == dc) {
|
if !a.config.TranslateWanAddrs || (a.config.Datacenter == dc) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,24 +44,19 @@ func translateAddresses(config *Config, dc string, subj interface{}) {
|
|||||||
switch v := subj.(type) {
|
switch v := subj.(type) {
|
||||||
case structs.CheckServiceNodes:
|
case structs.CheckServiceNodes:
|
||||||
for _, entry := range v {
|
for _, entry := range v {
|
||||||
entry.Node.Address = translateAddress(config, dc,
|
entry.Node.Address = a.TranslateAddress(dc, entry.Node.Address, entry.Node.TaggedAddresses)
|
||||||
entry.Node.Address, entry.Node.TaggedAddresses)
|
|
||||||
}
|
}
|
||||||
case *structs.Node:
|
case *structs.Node:
|
||||||
v.Address = translateAddress(config, dc,
|
v.Address = a.TranslateAddress(dc, v.Address, v.TaggedAddresses)
|
||||||
v.Address, v.TaggedAddresses)
|
|
||||||
case structs.Nodes:
|
case structs.Nodes:
|
||||||
for _, node := range v {
|
for _, node := range v {
|
||||||
node.Address = translateAddress(config, dc,
|
node.Address = a.TranslateAddress(dc, node.Address, node.TaggedAddresses)
|
||||||
node.Address, node.TaggedAddresses)
|
|
||||||
}
|
}
|
||||||
case structs.ServiceNodes:
|
case structs.ServiceNodes:
|
||||||
for _, entry := range v {
|
for _, entry := range v {
|
||||||
entry.Address = translateAddress(config, dc,
|
entry.Address = a.TranslateAddress(dc, entry.Address, entry.TaggedAddresses)
|
||||||
entry.Address, entry.TaggedAddresses)
|
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
panic(fmt.Errorf("Unhandled type passed to address translator: %#v", subj))
|
panic(fmt.Errorf("Unhandled type passed to address translator: %#v", subj))
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user