From c8d0cb9e716585f33dc730911332adbbc458513a Mon Sep 17 00:00:00 2001 From: Alessandro Boch Date: Thu, 10 Nov 2016 13:45:32 -0800 Subject: [PATCH] Add local address autodetection on swarm init - when advertise-addr is not local and listen-addr is not specified Signed-off-by: Alessandro Boch --- daemon/cluster/cluster.go | 31 ++++++++++++++++--------------- daemon/cluster/listen_addr.go | 2 +- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/daemon/cluster/cluster.go b/daemon/cluster/cluster.go index c952dcf531..2dadcafddb 100644 --- a/daemon/cluster/cluster.go +++ b/daemon/cluster/cluster.go @@ -415,31 +415,32 @@ func (c *Cluster) Init(req types.InitRequest) (string, error) { localAddr := listenHost - // If the advertise address is not one of the system's - // addresses, we also require a listen address. - listenAddrIP := net.ParseIP(listenHost) - if listenAddrIP != nil && listenAddrIP.IsUnspecified() { + // If the local address is undetermined, the advertise address + // will be used as local address, if it belongs to this system. + // If the advertise address is not local, then we try to find + // a system address to use as local address. If this fails, + // we give up and ask user to pass the listen address. + if net.ParseIP(localAddr).IsUnspecified() { advertiseIP := net.ParseIP(advertiseHost) - if advertiseIP == nil { - // not an IP - c.Unlock() - return "", errMustSpecifyListenAddr - } - - systemIPs := listSystemIPs() found := false - for _, systemIP := range systemIPs { + for _, systemIP := range listSystemIPs() { if systemIP.Equal(advertiseIP) { + localAddr = advertiseIP.String() found = true break } } + if !found { - c.Unlock() - return "", errMustSpecifyListenAddr + ip, err := c.resolveSystemAddr() + if err != nil { + c.Unlock() + logrus.Warnf("Could not find a local address: %v", err) + return "", errMustSpecifyListenAddr + } + localAddr = ip.String() } - localAddr = advertiseIP.String() } // todo: check current state existing diff --git a/daemon/cluster/listen_addr.go b/daemon/cluster/listen_addr.go index 4b53a3cac0..7b9050dab6 100644 --- a/daemon/cluster/listen_addr.go +++ b/daemon/cluster/listen_addr.go @@ -9,7 +9,7 @@ import ( var ( errNoSuchInterface = errors.New("no such interface") errNoIP = errors.New("could not find the system's IP address") - errMustSpecifyListenAddr = errors.New("must specify a listening address because the address to advertise is not recognized as a system address") + errMustSpecifyListenAddr = errors.New("must specify a listening address because the address to advertise is not recognized as a system address, and a system's IP address to use could not be uniquely identified") errBadListenAddr = errors.New("listen address must be an IP address or network interface (with optional port number)") errBadAdvertiseAddr = errors.New("advertise address must be an IP address or network interface (with optional port number)") errBadDefaultAdvertiseAddr = errors.New("default advertise address must be an IP address or network interface (without a port number)")