Fix IPv6 advertise addresses

This commit is contained in:
Wim 2016-07-25 22:52:18 +02:00
parent 67a122df0c
commit a04907ad91
2 changed files with 37 additions and 16 deletions

View File

@ -175,49 +175,49 @@ func (a *Agent) serverConfig() (*nomad.Config, error) {
if a.config.AdvertiseAddrs.HTTP != "" { if a.config.AdvertiseAddrs.HTTP != "" {
a.serverHTTPAddr = a.config.AdvertiseAddrs.HTTP a.serverHTTPAddr = a.config.AdvertiseAddrs.HTTP
} else if a.config.Addresses.HTTP != "" { } else if a.config.Addresses.HTTP != "" {
a.serverHTTPAddr = fmt.Sprintf("%v:%v", a.config.Addresses.HTTP, a.config.Ports.HTTP) a.serverHTTPAddr = joinIPPort(a.config.Addresses.HTTP, a.config.Ports.HTTP)
} else if a.config.BindAddr != "" { } else if a.config.BindAddr != "" {
a.serverHTTPAddr = fmt.Sprintf("%v:%v", a.config.BindAddr, a.config.Ports.HTTP) a.serverHTTPAddr = joinIPPort(a.config.BindAddr, a.config.Ports.HTTP)
} else { } else {
a.serverHTTPAddr = fmt.Sprintf("%v:%v", "127.0.0.1", a.config.Ports.HTTP) a.serverHTTPAddr = joinIPPort("127.0.0.1", a.config.Ports.HTTP)
} }
addr, err := net.ResolveTCPAddr("tcp", a.serverHTTPAddr) addr, err := net.ResolveTCPAddr("tcp", a.serverHTTPAddr)
if err != nil { if err != nil {
return nil, fmt.Errorf("error resolving HTTP addr %+q: %v", a.serverHTTPAddr, err) return nil, fmt.Errorf("error resolving HTTP addr %+q: %v", a.serverHTTPAddr, err)
} }
a.serverHTTPAddr = fmt.Sprintf("%s:%d", addr.IP.String(), addr.Port) a.serverHTTPAddr = joinIPPort(addr.IP.String(), addr.Port)
// Resolve the Server's RPC Address // Resolve the Server's RPC Address
if a.config.AdvertiseAddrs.RPC != "" { if a.config.AdvertiseAddrs.RPC != "" {
a.serverRPCAddr = a.config.AdvertiseAddrs.RPC a.serverRPCAddr = a.config.AdvertiseAddrs.RPC
} else if a.config.Addresses.RPC != "" { } else if a.config.Addresses.RPC != "" {
a.serverRPCAddr = fmt.Sprintf("%v:%v", a.config.Addresses.RPC, a.config.Ports.RPC) a.serverRPCAddr = joinIPPort(a.config.Addresses.RPC, a.config.Ports.RPC)
} else if a.config.BindAddr != "" { } else if a.config.BindAddr != "" {
a.serverRPCAddr = fmt.Sprintf("%v:%v", a.config.BindAddr, a.config.Ports.RPC) a.serverRPCAddr = joinIPPort(a.config.BindAddr, a.config.Ports.RPC)
} else { } else {
a.serverRPCAddr = fmt.Sprintf("%v:%v", "127.0.0.1", a.config.Ports.RPC) a.serverRPCAddr = joinIPPort("127.0.0.1", a.config.Ports.RPC)
} }
addr, err = net.ResolveTCPAddr("tcp", a.serverRPCAddr) addr, err = net.ResolveTCPAddr("tcp", a.serverRPCAddr)
if err != nil { if err != nil {
return nil, fmt.Errorf("error resolving RPC addr %+q: %v", a.serverRPCAddr, err) return nil, fmt.Errorf("error resolving RPC addr %+q: %v", a.serverRPCAddr, err)
} }
a.serverRPCAddr = fmt.Sprintf("%s:%d", addr.IP.String(), addr.Port) a.serverRPCAddr = joinIPPort(addr.IP.String(), addr.Port)
// Resolve the Server's Serf Address // Resolve the Server's Serf Address
if a.config.AdvertiseAddrs.Serf != "" { if a.config.AdvertiseAddrs.Serf != "" {
a.serverSerfAddr = a.config.AdvertiseAddrs.Serf a.serverSerfAddr = a.config.AdvertiseAddrs.Serf
} else if a.config.Addresses.Serf != "" { } else if a.config.Addresses.Serf != "" {
a.serverSerfAddr = fmt.Sprintf("%v:%v", a.config.Addresses.Serf, a.config.Ports.Serf) a.serverSerfAddr = joinIPPort(a.config.Addresses.Serf, a.config.Ports.Serf)
} else if a.config.BindAddr != "" { } else if a.config.BindAddr != "" {
a.serverSerfAddr = fmt.Sprintf("%v:%v", a.config.BindAddr, a.config.Ports.Serf) a.serverSerfAddr = joinIPPort(a.config.BindAddr, a.config.Ports.Serf)
} else { } else {
a.serverSerfAddr = fmt.Sprintf("%v:%v", "127.0.0.1", a.config.Ports.Serf) a.serverSerfAddr = joinIPPort("127.0.0.1", a.config.Ports.Serf)
} }
addr, err = net.ResolveTCPAddr("tcp", a.serverSerfAddr) addr, err = net.ResolveTCPAddr("tcp", a.serverSerfAddr)
if err != nil { if err != nil {
return nil, fmt.Errorf("error resolving Serf addr %+q: %v", a.serverSerfAddr, err) return nil, fmt.Errorf("error resolving Serf addr %+q: %v", a.serverSerfAddr, err)
} }
a.serverSerfAddr = fmt.Sprintf("%s:%d", addr.IP.String(), addr.Port) a.serverSerfAddr = joinIPPort(addr.IP.String(), addr.Port)
if gcThreshold := a.config.Server.NodeGCThreshold; gcThreshold != "" { if gcThreshold := a.config.Server.NodeGCThreshold; gcThreshold != "" {
dur, err := time.ParseDuration(gcThreshold) dur, err := time.ParseDuration(gcThreshold)
@ -314,17 +314,18 @@ func (a *Agent) clientConfig() (*clientconfig.Config, error) {
if a.config.AdvertiseAddrs.HTTP != "" { if a.config.AdvertiseAddrs.HTTP != "" {
a.clientHTTPAddr = a.config.AdvertiseAddrs.HTTP a.clientHTTPAddr = a.config.AdvertiseAddrs.HTTP
} else if a.config.Addresses.HTTP != "" { } else if a.config.Addresses.HTTP != "" {
a.clientHTTPAddr = fmt.Sprintf("%v:%v", a.config.Addresses.HTTP, a.config.Ports.HTTP) a.clientHTTPAddr = joinIPPort(a.config.Addresses.HTTP, a.config.Ports.HTTP)
} else if a.config.BindAddr != "" { } else if a.config.BindAddr != "" {
a.clientHTTPAddr = fmt.Sprintf("%v:%v", a.config.BindAddr, a.config.Ports.HTTP) a.clientHTTPAddr = joinIPPort(a.config.BindAddr, a.config.Ports.HTTP)
} else { } else {
a.clientHTTPAddr = fmt.Sprintf("%v:%v", "127.0.0.1", a.config.Ports.HTTP) a.clientHTTPAddr = joinIPPort("127.0.0.1", a.config.Ports.HTTP)
} }
addr, err := net.ResolveTCPAddr("tcp", a.clientHTTPAddr) addr, err := net.ResolveTCPAddr("tcp", a.clientHTTPAddr)
if err != nil { if err != nil {
return nil, fmt.Errorf("error resolving HTTP addr %+q: %v", a.clientHTTPAddr, err) return nil, fmt.Errorf("error resolving HTTP addr %+q: %v", a.clientHTTPAddr, err)
} }
httpAddr := fmt.Sprintf("%s:%d", addr.IP.String(), addr.Port) httpAddr := joinIPPort(addr.IP.String(), addr.Port)
conf.Node.HTTPAddr = httpAddr conf.Node.HTTPAddr = httpAddr
a.clientHTTPAddr = httpAddr a.clientHTTPAddr = httpAddr

View File

@ -44,3 +44,23 @@ func ipOfDevice(name string) (net.IP, error) {
} }
return nil, fmt.Errorf("no ips were detected on the interface: %v", name) return nil, fmt.Errorf("no ips were detected on the interface: %v", name)
} }
// isIPV6 checks if the IP address is an IPv6 address
func isIPV6(ip string) bool {
addr := net.ParseIP(ip)
if addr != nil {
// ipv6
if addr.To4() == nil {
return true
}
}
return false
}
// joinIPPort joins ip and port correctly for IPv4 (ip:port) or IPv6 ([ip]:port)
func joinIPPort(ip string, port int) string {
if isIPV6(ip) {
return fmt.Sprintf("[%s]:%d", ip, port)
}
return fmt.Sprintf("%s:%d", ip, port)
}