Using the interface name to find bind address for network services

This commit is contained in:
Diptanu Choudhury
2016-03-18 21:44:22 -07:00
parent c51db58509
commit ab4b1e1f09
2 changed files with 27 additions and 4 deletions

View File

@@ -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 {

View File

@@ -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)
}