Never return 0.0.0.0 as a non-bind address

This commit is contained in:
Evan Gilman 2016-09-11 11:40:52 -07:00 committed by Alex Dadgar
parent 7511490c12
commit 59489e4e4f
2 changed files with 49 additions and 24 deletions

View file

@ -486,6 +486,7 @@ func (a *Agent) setupClient() error {
type addrSelector func(bool) (*net.TCPAddr, error) type addrSelector func(bool) (*net.TCPAddr, error)
// Choose the right address given a selector, and return it as a PortLabel // Choose the right address given a selector, and return it as a PortLabel
// preferBind is a weak preference, and will skip 0.0.0.0
func (a *Agent) selectAddr(selector addrSelector, preferBind bool) (string, error) { func (a *Agent) selectAddr(selector addrSelector, preferBind bool) (string, error) {
addr, err := selector(preferBind) addr, err := selector(preferBind)
if err != nil { if err != nil {
@ -503,15 +504,22 @@ func (a *Agent) selectAddr(selector addrSelector, preferBind bool) (string, erro
return address, nil return address, nil
} }
// HTTP address selector // Address selectors
// Resolve Bind address and Advertise address. Skip 0.0.0.0 unless
// we're looking for the Bind address, since that's the only time
// it's useful
func (a *Agent) getHTTPAddr(returnBind bool) (*net.TCPAddr, error) { func (a *Agent) getHTTPAddr(returnBind bool) (*net.TCPAddr, error) {
var serverAddr string var serverAddr string
if a.config.AdvertiseAddrs.HTTP != "" && !returnBind { advertAddr := a.config.AdvertiseAddrs.HTTP
serverAddr = a.config.AdvertiseAddrs.HTTP bindAddr := a.config.Addresses.HTTP
} else if a.config.Addresses.HTTP != "" { globalBindAddr := a.config.BindAddr
serverAddr = net.JoinHostPort(a.config.Addresses.HTTP, strconv.Itoa(a.config.Ports.HTTP))
} else if a.config.BindAddr != "" { if advertAddr != "" && !returnBind {
serverAddr = net.JoinHostPort(a.config.BindAddr, strconv.Itoa(a.config.Ports.HTTP)) serverAddr = advertAddr
} else if bindAddr != "" && !(bindAddr == "0.0.0.0" && !returnBind) {
serverAddr = net.JoinHostPort(bindAddr, strconv.Itoa(a.config.Ports.HTTP))
} else if globalBindAddr != "" && !(globalBindAddr == "0.0.0.0" && !returnBind) {
serverAddr = net.JoinHostPort(globalBindAddr, strconv.Itoa(a.config.Ports.HTTP))
} else { } else {
serverAddr = net.JoinHostPort("127.0.0.1", strconv.Itoa(a.config.Ports.HTTP)) serverAddr = net.JoinHostPort("127.0.0.1", strconv.Itoa(a.config.Ports.HTTP))
} }
@ -523,15 +531,18 @@ func (a *Agent) getHTTPAddr(returnBind bool) (*net.TCPAddr, error) {
return addr, nil return addr, nil
} }
// RPC address selector
func (a *Agent) getRPCAddr(returnBind bool) (*net.TCPAddr, error) { func (a *Agent) getRPCAddr(returnBind bool) (*net.TCPAddr, error) {
var serverAddr string var serverAddr string
if a.config.AdvertiseAddrs.RPC != "" && !returnBind { advertAddr := a.config.AdvertiseAddrs.RPC
serverAddr = a.config.AdvertiseAddrs.RPC bindAddr := a.config.Addresses.RPC
} else if a.config.Addresses.RPC != "" { globalBindAddr := a.config.BindAddr
serverAddr = net.JoinHostPort(a.config.Addresses.RPC, strconv.Itoa(a.config.Ports.RPC))
} else if a.config.BindAddr != "" { if advertAddr != "" && !returnBind {
serverAddr = net.JoinHostPort(a.config.BindAddr, strconv.Itoa(a.config.Ports.RPC)) serverAddr = advertAddr
} else if bindAddr != "" && !(bindAddr == "0.0.0.0" && !returnBind) {
serverAddr = net.JoinHostPort(bindAddr, strconv.Itoa(a.config.Ports.RPC))
} else if globalBindAddr != "" && !(globalBindAddr == "0.0.0.0" && !returnBind) {
serverAddr = net.JoinHostPort(globalBindAddr, strconv.Itoa(a.config.Ports.RPC))
} else { } else {
serverAddr = net.JoinHostPort("127.0.0.1", strconv.Itoa(a.config.Ports.RPC)) serverAddr = net.JoinHostPort("127.0.0.1", strconv.Itoa(a.config.Ports.RPC))
} }
@ -543,15 +554,18 @@ func (a *Agent) getRPCAddr(returnBind bool) (*net.TCPAddr, error) {
return addr, nil return addr, nil
} }
// Serf address selector
func (a *Agent) getSerfAddr(returnBind bool) (*net.TCPAddr, error) { func (a *Agent) getSerfAddr(returnBind bool) (*net.TCPAddr, error) {
var serverAddr string var serverAddr string
if a.config.AdvertiseAddrs.Serf != "" && !returnBind { advertAddr := a.config.AdvertiseAddrs.Serf
serverAddr = a.config.AdvertiseAddrs.Serf bindAddr := a.config.Addresses.Serf
} else if a.config.Addresses.Serf != "" { globalBindAddr := a.config.BindAddr
serverAddr = net.JoinHostPort(a.config.Addresses.Serf, strconv.Itoa(a.config.Ports.Serf))
} else if a.config.BindAddr != "" { if advertAddr != "" && !returnBind {
serverAddr = net.JoinHostPort(a.config.BindAddr, strconv.Itoa(a.config.Ports.Serf)) serverAddr = advertAddr
} else if bindAddr != "" && !(bindAddr == "0.0.0.0" && !returnBind) {
serverAddr = net.JoinHostPort(bindAddr, strconv.Itoa(a.config.Ports.Serf))
} else if globalBindAddr != "" && !(globalBindAddr == "0.0.0.0" && !returnBind) {
serverAddr = net.JoinHostPort(globalBindAddr, strconv.Itoa(a.config.Ports.Serf))
} else { } else {
serverAddr = net.JoinHostPort("127.0.0.1", strconv.Itoa(a.config.Ports.Serf)) serverAddr = net.JoinHostPort("127.0.0.1", strconv.Itoa(a.config.Ports.Serf))
} }

View file

@ -205,10 +205,21 @@ func TestAgent_ServerConfig(t *testing.T) {
t.Fatalf("expect 10.0.0.12:4004, got: %s", addr) t.Fatalf("expect 10.0.0.12:4004, got: %s", addr)
} }
// selectAddr does not return 0.0.0.0 with preferBind // We don't resolve 0.0.0.0 unless we're asking for bind
conf.Addresses.HTTP = "0.0.0.0" conf.Addresses.HTTP = "0.0.0.0"
if addr, err := a.selectAddr(a.getHTTPAddr, true); addr != "10.0.0.10:4646" || err != nil { conf.AdvertiseAddrs.HTTP = ""
t.Fatalf("expect 10.0.0.10:4646, got: %s", addr) if addr, err := a.getHTTPAddr(false); addr.IP.String() != "127.0.0.3" || err != nil {
t.Fatalf("expect 127.0.0.3, got: %s", addr.IP.String())
}
// We still get 0.0.0.0 when explicitly asking for bind
if addr, err := a.getHTTPAddr(true); addr.IP.String() != "0.0.0.0" || err != nil {
t.Fatalf("expect 0.0.0.0, got: %s", addr.IP.String())
}
// selectAddr does not return 0.0.0.0 with preferBind
if addr, err := a.selectAddr(a.getHTTPAddr, true); addr != "127.0.0.3:4646" || err != nil {
t.Fatalf("expect 127.0.0.3:4646, got: %s", addr)
} }
conf.Server.NodeGCThreshold = "42g" conf.Server.NodeGCThreshold = "42g"