2016-08-15 22:05:02 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2016-08-15 22:05:02 +00:00
|
|
|
)
|
|
|
|
|
2017-06-30 19:42:53 +00:00
|
|
|
// TranslateAddress is used to provide the final, translated address for a node,
|
2016-08-15 22:05:02 +00:00
|
|
|
// depending on how the agent and the other node are configured. The dc
|
|
|
|
// parameter is the dc the datacenter this node is from.
|
2017-06-30 19:42:53 +00:00
|
|
|
func (a *Agent) TranslateAddress(dc string, addr string, taggedAddresses map[string]string) string {
|
|
|
|
if a.config.TranslateWanAddrs && (a.config.Datacenter != dc) {
|
2016-08-15 22:05:02 +00:00
|
|
|
wanAddr := taggedAddresses["wan"]
|
|
|
|
if wanAddr != "" {
|
|
|
|
addr = wanAddr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
|
2017-06-30 19:42:53 +00:00
|
|
|
// TranslateAddresses translates addresses in the given structure into the
|
2016-08-15 22:05:02 +00:00
|
|
|
// final, translated address, depending on how the agent and the other node are
|
|
|
|
// configured. The dc parameter is the datacenter this structure is from.
|
2017-06-30 19:42:53 +00:00
|
|
|
func (a *Agent) TranslateAddresses(dc string, subj interface{}) {
|
2016-08-15 22:05:02 +00:00
|
|
|
// CAUTION - SUBTLE! An agent running on a server can, in some cases,
|
|
|
|
// return pointers directly into the immutable state store for
|
|
|
|
// performance (it's via the in-memory RPC mechanism). It's never safe
|
|
|
|
// to modify those values, so we short circuit here so that we never
|
|
|
|
// update any structures that are from our own datacenter. This works
|
|
|
|
// for address translation because we *never* need to translate local
|
|
|
|
// addresses, but this is super subtle, so we've piped all the in-place
|
|
|
|
// address translation into this function which makes sure this check is
|
|
|
|
// 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
|
|
|
|
// skip a lot of work if no translation needs to be done.
|
2017-06-30 19:42:53 +00:00
|
|
|
if !a.config.TranslateWanAddrs || (a.config.Datacenter == dc) {
|
2016-08-15 22:05:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Translate addresses in-place, subject to the condition checked above
|
|
|
|
// which ensures this is safe to do since we are operating on a local
|
|
|
|
// copy of the data.
|
|
|
|
switch v := subj.(type) {
|
|
|
|
case structs.CheckServiceNodes:
|
|
|
|
for _, entry := range v {
|
2017-06-30 19:42:53 +00:00
|
|
|
entry.Node.Address = a.TranslateAddress(dc, entry.Node.Address, entry.Node.TaggedAddresses)
|
2016-08-15 22:05:02 +00:00
|
|
|
}
|
|
|
|
case *structs.Node:
|
2017-06-30 19:42:53 +00:00
|
|
|
v.Address = a.TranslateAddress(dc, v.Address, v.TaggedAddresses)
|
2016-08-15 22:05:02 +00:00
|
|
|
case structs.Nodes:
|
|
|
|
for _, node := range v {
|
2017-06-30 19:42:53 +00:00
|
|
|
node.Address = a.TranslateAddress(dc, node.Address, node.TaggedAddresses)
|
2016-08-15 22:05:02 +00:00
|
|
|
}
|
|
|
|
case structs.ServiceNodes:
|
|
|
|
for _, entry := range v {
|
2017-06-30 19:42:53 +00:00
|
|
|
entry.Address = a.TranslateAddress(dc, entry.Address, entry.TaggedAddresses)
|
2016-08-15 22:05:02 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("Unhandled type passed to address translator: %#v", subj))
|
|
|
|
}
|
|
|
|
}
|