Merge pull request #4115 from sub-mod/master_changes

Use discovery heuristics in ChooseHostInterface method to fetch PublicAddress
pull/6/head
Tim Hockin 2015-03-16 14:31:15 -07:00
commit 9eebaa8168
1 changed files with 5 additions and 25 deletions

View File

@ -206,34 +206,14 @@ func setDefaults(c *Config) {
c.CacheTimeout = 5 * time.Second
}
for c.PublicAddress == nil {
// Find and use the first non-loopback address.
// TODO: potentially it'd be useful to skip the docker interface if it
// somehow is first in the list.
addrs, err := net.InterfaceAddrs()
hostIP, err := util.ChooseHostInterface()
if err != nil {
glog.Fatalf("Unable to get network interfaces: error='%v'", err)
}
found := false
for i := range addrs {
ip, _, err := net.ParseCIDR(addrs[i].String())
if err != nil {
glog.Errorf("Error parsing '%v': %v", addrs[i], err)
continue
}
if ip.IsLoopback() {
glog.V(5).Infof("'%v' (%v) is a loopback address, ignoring.", ip, addrs[i])
continue
}
found = true
c.PublicAddress = ip
glog.V(2).Infof("Will report %v as public IP address.", ip)
break
}
if !found {
glog.Errorf("Unable to find suitable network address in list: '%v'\n"+
"Will try again in 5 seconds. Set the public address directly to avoid this wait.", addrs)
glog.Fatalf("Unable to find suitable network address.error='%v' . "+
"Will try again in 5 seconds. Set the public address directly to avoid this wait.", err)
time.Sleep(5 * time.Second)
}
c.PublicAddress = hostIP
glog.Infof("Will report %v as public IP address.", c.PublicAddress)
}
if c.RequestContextMapper == nil {
c.RequestContextMapper = api.NewRequestContextMapper()