From a04907ad91a3132bfd8647ddfb1c7dfbaff65aac Mon Sep 17 00:00:00 2001 From: Wim Date: Mon, 25 Jul 2016 22:52:18 +0200 Subject: [PATCH] Fix IPv6 advertise addresses --- command/agent/agent.go | 33 +++++++++++++++++---------------- command/agent/util.go | 20 ++++++++++++++++++++ 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/command/agent/agent.go b/command/agent/agent.go index 819848d66..a8686c8a7 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -175,49 +175,49 @@ func (a *Agent) serverConfig() (*nomad.Config, error) { if a.config.AdvertiseAddrs.HTTP != "" { a.serverHTTPAddr = a.config.AdvertiseAddrs.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 != "" { - a.serverHTTPAddr = fmt.Sprintf("%v:%v", a.config.BindAddr, a.config.Ports.HTTP) + a.serverHTTPAddr = joinIPPort(a.config.BindAddr, a.config.Ports.HTTP) } 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) if err != nil { 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 if a.config.AdvertiseAddrs.RPC != "" { a.serverRPCAddr = a.config.AdvertiseAddrs.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 != "" { - a.serverRPCAddr = fmt.Sprintf("%v:%v", a.config.BindAddr, a.config.Ports.RPC) + a.serverRPCAddr = joinIPPort(a.config.BindAddr, a.config.Ports.RPC) } 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) if err != nil { 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 if a.config.AdvertiseAddrs.Serf != "" { a.serverSerfAddr = a.config.AdvertiseAddrs.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 != "" { - a.serverSerfAddr = fmt.Sprintf("%v:%v", a.config.BindAddr, a.config.Ports.Serf) + a.serverSerfAddr = joinIPPort(a.config.BindAddr, a.config.Ports.Serf) } 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) if err != nil { 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 != "" { dur, err := time.ParseDuration(gcThreshold) @@ -314,17 +314,18 @@ func (a *Agent) clientConfig() (*clientconfig.Config, error) { if a.config.AdvertiseAddrs.HTTP != "" { a.clientHTTPAddr = a.config.AdvertiseAddrs.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 != "" { - a.clientHTTPAddr = fmt.Sprintf("%v:%v", a.config.BindAddr, a.config.Ports.HTTP) + a.clientHTTPAddr = joinIPPort(a.config.BindAddr, a.config.Ports.HTTP) } 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) if err != nil { 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 a.clientHTTPAddr = httpAddr diff --git a/command/agent/util.go b/command/agent/util.go index c74fe645c..e4b54dcd6 100644 --- a/command/agent/util.go +++ b/command/agent/util.go @@ -44,3 +44,23 @@ func ipOfDevice(name string) (net.IP, error) { } 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) +}