Explorar o código

Add local address autodetection on swarm init

- when advertise-addr is not local and listen-addr is
  not specified

Signed-off-by: Alessandro Boch <aboch@docker.com>
Alessandro Boch %!s(int64=8) %!d(string=hai) anos
pai
achega
c8d0cb9e71
Modificáronse 2 ficheiros con 17 adicións e 16 borrados
  1. 16 15
      daemon/cluster/cluster.go
  2. 1 1
      daemon/cluster/listen_addr.go

+ 16 - 15
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

+ 1 - 1
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)")