Browse Source

Merge pull request #38910 from thaJeztah/remove_hot_pluggable_check

Remove some redundant code and cleanup validation
Sebastiaan van Stijn 6 years ago
parent
commit
c522dd45d9

+ 0 - 5
container/container_unix.go

@@ -444,11 +444,6 @@ func (container *Container) TmpfsMounts() ([]Mount, error) {
 	return mounts, nil
 }
 
-// EnableServiceDiscoveryOnDefaultNetwork Enable service discovery on default network
-func (container *Container) EnableServiceDiscoveryOnDefaultNetwork() bool {
-	return false
-}
-
 // GetMountPoints gives a platform specific transformation to types.MountPoint. Callers must hold a Container lock.
 func (container *Container) GetMountPoints() []types.MountPoint {
 	mountPoints := make([]types.MountPoint, 0, len(container.MountPoints))

+ 0 - 5
container/container_windows.go

@@ -182,11 +182,6 @@ func (container *Container) BuildHostnameFile() error {
 	return nil
 }
 
-// EnableServiceDiscoveryOnDefaultNetwork Enable service discovery on default network
-func (container *Container) EnableServiceDiscoveryOnDefaultNetwork() bool {
-	return true
-}
-
 // GetMountPoints gives a platform specific transformation to types.MountPoint. Callers must hold a Container lock.
 func (container *Container) GetMountPoints() []types.MountPoint {
 	mountPoints := make([]types.MountPoint, 0, len(container.MountPoints))

+ 15 - 19
daemon/container_operations.go

@@ -6,7 +6,6 @@ import (
 	"net"
 	"os"
 	"path"
-	"runtime"
 	"strings"
 	"time"
 
@@ -593,9 +592,9 @@ func (daemon *Daemon) getNetworkSandbox(container *container.Container) libnetwo
 	return sb
 }
 
-// hasUserDefinedIPAddress returns whether the passed endpoint configuration contains IP address configuration
-func hasUserDefinedIPAddress(epConfig *networktypes.EndpointSettings) bool {
-	return epConfig != nil && epConfig.IPAMConfig != nil && (len(epConfig.IPAMConfig.IPv4Address) > 0 || len(epConfig.IPAMConfig.IPv6Address) > 0)
+// hasUserDefinedIPAddress returns whether the passed IPAM configuration contains IP address configuration
+func hasUserDefinedIPAddress(ipamConfig *networktypes.EndpointIPAMConfig) bool {
+	return ipamConfig != nil && (len(ipamConfig.IPv4Address) > 0 || len(ipamConfig.IPv6Address) > 0)
 }
 
 // User specified ip address is acceptable only for networks with user specified subnets.
@@ -603,9 +602,18 @@ func validateNetworkingConfig(n libnetwork.Network, epConfig *networktypes.Endpo
 	if n == nil || epConfig == nil {
 		return nil
 	}
-	if !hasUserDefinedIPAddress(epConfig) {
+	if !containertypes.NetworkMode(n.Name()).IsUserDefined() {
+		if hasUserDefinedIPAddress(epConfig.IPAMConfig) && !enableIPOnPredefinedNetwork() {
+			return runconfig.ErrUnsupportedNetworkAndIP
+		}
+		if len(epConfig.Aliases) > 0 && !serviceDiscoveryOnDefaultNetwork() {
+			return runconfig.ErrUnsupportedNetworkAndAlias
+		}
+	}
+	if !hasUserDefinedIPAddress(epConfig.IPAMConfig) {
 		return nil
 	}
+
 	_, _, nwIPv4Configs, nwIPv6Configs := n.Info().IpamConfig()
 	for _, s := range []struct {
 		ipConfigured  bool
@@ -654,14 +662,7 @@ func cleanOperationalData(es *network.EndpointSettings) {
 
 func (daemon *Daemon) updateNetworkConfig(container *container.Container, n libnetwork.Network, endpointConfig *networktypes.EndpointSettings, updateSettings bool) error {
 
-	if !containertypes.NetworkMode(n.Name()).IsUserDefined() {
-		if hasUserDefinedIPAddress(endpointConfig) && !enableIPOnPredefinedNetwork() {
-			return runconfig.ErrUnsupportedNetworkAndIP
-		}
-		if endpointConfig != nil && len(endpointConfig.Aliases) > 0 && !container.EnableServiceDiscoveryOnDefaultNetwork() {
-			return runconfig.ErrUnsupportedNetworkAndAlias
-		}
-	} else {
+	if containertypes.NetworkMode(n.Name()).IsUserDefined() {
 		addShortID := true
 		shortID := stringid.TruncateID(container.ID)
 		for _, alias := range endpointConfig.Aliases {
@@ -725,8 +726,7 @@ func (daemon *Daemon) connectToNetwork(container *container.Container, idOrName
 		}
 	}
 
-	err = daemon.updateNetworkConfig(container, n, endpointConfig, updateSettings)
-	if err != nil {
+	if err := daemon.updateNetworkConfig(container, n, endpointConfig, updateSettings); err != nil {
 		return err
 	}
 
@@ -1040,8 +1040,6 @@ func (daemon *Daemon) ConnectToNetwork(container *container.Container, idOrName
 				EndpointSettings: endpointConfig,
 			}
 		}
-	} else if !daemon.isNetworkHotPluggable() {
-		return fmt.Errorf(runtime.GOOS + " does not support connecting a running container to a network")
 	} else {
 		if err := daemon.connectToNetwork(container, idOrName, endpointConfig, true); err != nil {
 			return err
@@ -1070,8 +1068,6 @@ func (daemon *Daemon) DisconnectFromNetwork(container *container.Container, netw
 			return fmt.Errorf("container %s is not connected to the network %s", container.ID, networkName)
 		}
 		delete(container.NetworkSettings.Networks, networkName)
-	} else if err == nil && !daemon.isNetworkHotPluggable() {
-		return fmt.Errorf(runtime.GOOS + " does not support connecting a running container to a network")
 	} else if err == nil {
 		if container.HostConfig.NetworkMode.IsHost() && containertypes.NetworkMode(n.Type()).IsHost() {
 			return runconfig.ErrConflictHostNetwork

+ 3 - 2
daemon/container_operations_unix.go

@@ -368,8 +368,9 @@ func enableIPOnPredefinedNetwork() bool {
 	return false
 }
 
-func (daemon *Daemon) isNetworkHotPluggable() bool {
-	return true
+// serviceDiscoveryOnDefaultNetwork indicates if service discovery is supported on the default network
+func serviceDiscoveryOnDefaultNetwork() bool {
+	return false
 }
 
 func (daemon *Daemon) setupPathsAndSandboxOptions(container *container.Container, sboxOptions *[]libnetwork.SandboxOption) error {

+ 2 - 1
daemon/container_operations_windows.go

@@ -154,7 +154,8 @@ func enableIPOnPredefinedNetwork() bool {
 	return true
 }
 
-func (daemon *Daemon) isNetworkHotPluggable() bool {
+// serviceDiscoveryOnDefaultNetwork indicates if service discovery is supported on the default network
+func serviceDiscoveryOnDefaultNetwork() bool {
 	return true
 }
 

+ 22 - 21
daemon/create.go

@@ -305,30 +305,31 @@ func verifyNetworkingConfig(nwConfig *networktypes.NetworkingConfig) error {
 	if nwConfig == nil || len(nwConfig.EndpointsConfig) == 0 {
 		return nil
 	}
-	if len(nwConfig.EndpointsConfig) == 1 {
-		for k, v := range nwConfig.EndpointsConfig {
-			if v == nil {
-				return errdefs.InvalidParameter(errors.Errorf("no EndpointSettings for %s", k))
+	if len(nwConfig.EndpointsConfig) > 1 {
+		l := make([]string, 0, len(nwConfig.EndpointsConfig))
+		for k := range nwConfig.EndpointsConfig {
+			l = append(l, k)
+		}
+		return errors.Errorf("Container cannot be connected to network endpoints: %s", strings.Join(l, ", "))
+	}
+
+	for k, v := range nwConfig.EndpointsConfig {
+		if v == nil {
+			return errdefs.InvalidParameter(errors.Errorf("no EndpointSettings for %s", k))
+		}
+		if v.IPAMConfig != nil {
+			if v.IPAMConfig.IPv4Address != "" && net.ParseIP(v.IPAMConfig.IPv4Address).To4() == nil {
+				return errors.Errorf("invalid IPv4 address: %s", v.IPAMConfig.IPv4Address)
 			}
-			if v.IPAMConfig != nil {
-				if v.IPAMConfig.IPv4Address != "" && net.ParseIP(v.IPAMConfig.IPv4Address).To4() == nil {
-					return errors.Errorf("invalid IPv4 address: %s", v.IPAMConfig.IPv4Address)
-				}
-				if v.IPAMConfig.IPv6Address != "" {
-					n := net.ParseIP(v.IPAMConfig.IPv6Address)
-					// if the address is an invalid network address (ParseIP == nil) or if it is
-					// an IPv4 address (To4() != nil), then it is an invalid IPv6 address
-					if n == nil || n.To4() != nil {
-						return errors.Errorf("invalid IPv6 address: %s", v.IPAMConfig.IPv6Address)
-					}
+			if v.IPAMConfig.IPv6Address != "" {
+				n := net.ParseIP(v.IPAMConfig.IPv6Address)
+				// if the address is an invalid network address (ParseIP == nil) or if it is
+				// an IPv4 address (To4() != nil), then it is an invalid IPv6 address
+				if n == nil || n.To4() != nil {
+					return errors.Errorf("invalid IPv6 address: %s", v.IPAMConfig.IPv6Address)
 				}
 			}
 		}
-		return nil
 	}
-	l := make([]string, 0, len(nwConfig.EndpointsConfig))
-	for k := range nwConfig.EndpointsConfig {
-		l = append(l, k)
-	}
-	return errors.Errorf("Container cannot be connected to network endpoints: %s", strings.Join(l, ", "))
+	return nil
 }

+ 1 - 1
daemon/network.go

@@ -796,7 +796,7 @@ func buildCreateEndpointOptions(c *container.Container, n libnetwork.Network, ep
 
 	defaultNetName := runconfig.DefaultDaemonNetworkMode().NetworkName()
 
-	if (!c.EnableServiceDiscoveryOnDefaultNetwork() && n.Name() == defaultNetName) ||
+	if (!serviceDiscoveryOnDefaultNetwork() && n.Name() == defaultNetName) ||
 		c.NetworkSettings.IsAnonymousEndpoint {
 		createOptions = append(createOptions, libnetwork.CreateOptionAnonymous())
 	}