From b3df64c9769e766962b243c566b7415c7a2addc4 Mon Sep 17 00:00:00 2001 From: Wim Date: Mon, 25 Jul 2016 22:52:18 +0200 Subject: [PATCH 1/2] 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 819848d668c..a8686c8a719 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 c74fe645cf6..e4b54dcd686 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) +} From c576c2339b66cd25368501ade1d339e1ae0a020d Mon Sep 17 00:00:00 2001 From: Wim Date: Mon, 25 Jul 2016 23:34:08 +0200 Subject: [PATCH 2/2] Use net.JoinHostPort --- command/agent/agent.go | 32 ++++++++++++++++---------------- command/agent/util.go | 20 -------------------- 2 files changed, 16 insertions(+), 36 deletions(-) diff --git a/command/agent/agent.go b/command/agent/agent.go index a8686c8a719..2f9701ce294 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 = joinIPPort(a.config.Addresses.HTTP, a.config.Ports.HTTP) + a.serverHTTPAddr = net.JoinHostPort(a.config.Addresses.HTTP, strconv.Itoa(a.config.Ports.HTTP)) } else if a.config.BindAddr != "" { - a.serverHTTPAddr = joinIPPort(a.config.BindAddr, a.config.Ports.HTTP) + a.serverHTTPAddr = net.JoinHostPort(a.config.BindAddr, strconv.Itoa(a.config.Ports.HTTP)) } else { - a.serverHTTPAddr = joinIPPort("127.0.0.1", a.config.Ports.HTTP) + a.serverHTTPAddr = net.JoinHostPort("127.0.0.1", strconv.Itoa(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 = joinIPPort(addr.IP.String(), addr.Port) + a.serverHTTPAddr = net.JoinHostPort(addr.IP.String(), strconv.Itoa(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 = joinIPPort(a.config.Addresses.RPC, a.config.Ports.RPC) + a.serverRPCAddr = net.JoinHostPort(a.config.Addresses.RPC, strconv.Itoa(a.config.Ports.RPC)) } else if a.config.BindAddr != "" { - a.serverRPCAddr = joinIPPort(a.config.BindAddr, a.config.Ports.RPC) + a.serverRPCAddr = net.JoinHostPort(a.config.BindAddr, strconv.Itoa(a.config.Ports.RPC)) } else { - a.serverRPCAddr = joinIPPort("127.0.0.1", a.config.Ports.RPC) + a.serverRPCAddr = net.JoinHostPort("127.0.0.1", strconv.Itoa(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 = joinIPPort(addr.IP.String(), addr.Port) + a.serverRPCAddr = net.JoinHostPort(addr.IP.String(), strconv.Itoa(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 = joinIPPort(a.config.Addresses.Serf, a.config.Ports.Serf) + a.serverSerfAddr = net.JoinHostPort(a.config.Addresses.Serf, strconv.Itoa(a.config.Ports.Serf)) } else if a.config.BindAddr != "" { - a.serverSerfAddr = joinIPPort(a.config.BindAddr, a.config.Ports.Serf) + a.serverSerfAddr = net.JoinHostPort(a.config.BindAddr, strconv.Itoa(a.config.Ports.Serf)) } else { - a.serverSerfAddr = joinIPPort("127.0.0.1", a.config.Ports.Serf) + a.serverSerfAddr = net.JoinHostPort("127.0.0.1", strconv.Itoa(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 = joinIPPort(addr.IP.String(), addr.Port) + a.serverSerfAddr = net.JoinHostPort(addr.IP.String(), strconv.Itoa(addr.Port)) if gcThreshold := a.config.Server.NodeGCThreshold; gcThreshold != "" { dur, err := time.ParseDuration(gcThreshold) @@ -314,17 +314,17 @@ 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 = joinIPPort(a.config.Addresses.HTTP, a.config.Ports.HTTP) + a.clientHTTPAddr = net.JoinHostPort(a.config.Addresses.HTTP, strconv.Itoa(a.config.Ports.HTTP)) } else if a.config.BindAddr != "" { - a.clientHTTPAddr = joinIPPort(a.config.BindAddr, a.config.Ports.HTTP) + a.clientHTTPAddr = net.JoinHostPort(a.config.BindAddr, strconv.Itoa(a.config.Ports.HTTP)) } else { - a.clientHTTPAddr = joinIPPort("127.0.0.1", a.config.Ports.HTTP) + a.clientHTTPAddr = net.JoinHostPort("127.0.0.1", strconv.Itoa(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 := joinIPPort(addr.IP.String(), addr.Port) + httpAddr := net.JoinHostPort(addr.IP.String(), strconv.Itoa(addr.Port)) conf.Node.HTTPAddr = httpAddr a.clientHTTPAddr = httpAddr diff --git a/command/agent/util.go b/command/agent/util.go index e4b54dcd686..c74fe645cf6 100644 --- a/command/agent/util.go +++ b/command/agent/util.go @@ -44,23 +44,3 @@ 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) -}