From ab4b1e1f0945419aeeef76911ef91eee92b22233 Mon Sep 17 00:00:00 2001 From: Diptanu Choudhury Date: Fri, 18 Mar 2016 21:44:22 -0700 Subject: [PATCH] Using the interface name to find bind address for network services --- command/agent/agent.go | 10 ++++++++-- command/agent/util.go | 21 +++++++++++++++++++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/command/agent/agent.go b/command/agent/agent.go index 76b7771b6..a8088325e 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -71,7 +71,7 @@ func (a *Agent) serverConfig() (*nomad.Config, error) { conf.LogOutput = a.logOutput conf.DevMode = a.config.DevMode conf.Build = fmt.Sprintf("%s%s", a.config.Version, a.config.VersionPrerelease) - if i.config.Region != "" { + if a.config.Region != "" { conf.Region = a.config.Region } if a.config.Datacenter != "" { @@ -210,11 +210,17 @@ func (a *Agent) clientConfig() (*clientconfig.Config, error) { conf.Node.Meta = a.config.Client.Meta conf.Node.NodeClass = a.config.Client.NodeClass httpAddr := fmt.Sprintf("%s:%d", a.config.BindAddr, a.config.Ports.HTTP) - if a.config.Addresses.HTTP != "" && a.config.AdvertiseAddrs.HTTP == "" { + if a.config.Addresses.HTTP != "" && a.config.AdvertiseAddrs.HTTP == "" && a.config.Interfaces.HTTP == "" { httpAddr = fmt.Sprintf("%s:%d", a.config.Addresses.HTTP, a.config.Ports.HTTP) if _, err := net.ResolveTCPAddr("tcp", httpAddr); err != nil { return nil, fmt.Errorf("error resolving http addr: %v:", err) } + } else if a.config.Interfaces.HTTP != "" && a.config.AdvertiseAddrs.HTTP == "" { + ip, err := ipOfDevice(a.config.Interfaces.HTTP) + if err != nil { + return nil, fmt.Errorf("error finding ip address from interface %q: %v", a.config.Interfaces.HTTP, err) + } + httpAddr = fmt.Sprintf("%s:%d", ip.String(), a.config.Ports.HTTP) } else if a.config.AdvertiseAddrs.HTTP != "" { addr, err := net.ResolveTCPAddr("tcp", a.config.AdvertiseAddrs.HTTP) if err != nil { diff --git a/command/agent/util.go b/command/agent/util.go index 27df5f8f2..6645a0af9 100644 --- a/command/agent/util.go +++ b/command/agent/util.go @@ -13,7 +13,7 @@ func randomStagger(intv time.Duration) time.Duration { } // IpOfDevice returns a routable ip addr of a device -func IpOfDevice(name string) (net.IP, error) { +func ipOfDevice(name string) (net.IP, error) { intf, err := net.InterfaceByName(name) if err != nil { return nil, err @@ -25,13 +25,30 @@ func IpOfDevice(name string) (net.IP, error) { if len(addrs) == 0 { return nil, fmt.Errorf("no ips were detected on the interface: %v", name) } + var ipv4Addrs []net.IP + var ipv6Addrs []net.IP for _, addr := range addrs { + var ip net.IP switch v := (addr).(type) { case *net.IPNet: continue case *net.IPAddr: - return v.IP, nil + ip = v.IP + if ip.To4() != nil { + ipv4Addrs = append(ipv4Addrs, ip) + continue + } + if ip.To16() != nil { + ipv6Addrs = append(ipv6Addrs, ip) + continue + } } } + if len(ipv4Addrs) > 0 { + return ipv4Addrs[0], nil + } + if len(ipv6Addrs) > 0 { + return ipv6Addrs[0], nil + } return nil, fmt.Errorf("no ips were detected on the interface: %v", name) }