mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
90bd41a74d
This fix tries to address the issue raised in #21976 and allows the options of `--add-host` and `--net=host` to work at the same time. The documentation has been updated and additional tests have been added to cover this change. This fix fixes #21976. Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
107 lines
3 KiB
Go
107 lines
3 KiB
Go
// +build !windows,!solaris
|
|
|
|
package runconfig
|
|
|
|
import (
|
|
"fmt"
|
|
"runtime"
|
|
"strings"
|
|
|
|
"github.com/docker/engine-api/types/container"
|
|
)
|
|
|
|
// DefaultDaemonNetworkMode returns the default network stack the daemon should
|
|
// use.
|
|
func DefaultDaemonNetworkMode() container.NetworkMode {
|
|
return container.NetworkMode("bridge")
|
|
}
|
|
|
|
// IsPreDefinedNetwork indicates if a network is predefined by the daemon
|
|
func IsPreDefinedNetwork(network string) bool {
|
|
n := container.NetworkMode(network)
|
|
return n.IsBridge() || n.IsHost() || n.IsNone() || n.IsDefault()
|
|
}
|
|
|
|
// ValidateNetMode ensures that the various combinations of requested
|
|
// network settings are valid.
|
|
func ValidateNetMode(c *container.Config, hc *container.HostConfig) error {
|
|
// We may not be passed a host config, such as in the case of docker commit
|
|
if hc == nil {
|
|
return nil
|
|
}
|
|
parts := strings.Split(string(hc.NetworkMode), ":")
|
|
if parts[0] == "container" {
|
|
if len(parts) < 2 || parts[1] == "" {
|
|
return fmt.Errorf("--net: invalid net mode: invalid container format container:<name|id>")
|
|
}
|
|
}
|
|
|
|
if hc.NetworkMode.IsContainer() && c.Hostname != "" {
|
|
return ErrConflictNetworkHostname
|
|
}
|
|
|
|
if hc.UTSMode.IsHost() && c.Hostname != "" {
|
|
return ErrConflictUTSHostname
|
|
}
|
|
|
|
if hc.NetworkMode.IsHost() && len(hc.Links) > 0 {
|
|
return ErrConflictHostNetworkAndLinks
|
|
}
|
|
|
|
if hc.NetworkMode.IsContainer() && len(hc.Links) > 0 {
|
|
return ErrConflictContainerNetworkAndLinks
|
|
}
|
|
|
|
if hc.NetworkMode.IsContainer() && len(hc.DNS) > 0 {
|
|
return ErrConflictNetworkAndDNS
|
|
}
|
|
|
|
if hc.NetworkMode.IsContainer() && len(hc.ExtraHosts) > 0 {
|
|
return ErrConflictNetworkHosts
|
|
}
|
|
|
|
if (hc.NetworkMode.IsContainer() || hc.NetworkMode.IsHost()) && c.MacAddress != "" {
|
|
return ErrConflictContainerNetworkAndMac
|
|
}
|
|
|
|
if hc.NetworkMode.IsContainer() && (len(hc.PortBindings) > 0 || hc.PublishAllPorts == true) {
|
|
return ErrConflictNetworkPublishPorts
|
|
}
|
|
|
|
if hc.NetworkMode.IsContainer() && len(c.ExposedPorts) > 0 {
|
|
return ErrConflictNetworkExposePorts
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ValidateIsolation performs platform specific validation of
|
|
// isolation in the hostconfig structure. Linux only supports "default"
|
|
// which is LXC container isolation
|
|
func ValidateIsolation(hc *container.HostConfig) error {
|
|
// We may not be passed a host config, such as in the case of docker commit
|
|
if hc == nil {
|
|
return nil
|
|
}
|
|
if !hc.Isolation.IsValid() {
|
|
return fmt.Errorf("invalid --isolation: %q - %s only supports 'default'", hc.Isolation, runtime.GOOS)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ValidateQoS performs platform specific validation of the QoS settings
|
|
// a disk can be limited by either Bps or IOps, but not both.
|
|
func ValidateQoS(hc *container.HostConfig) error {
|
|
// We may not be passed a host config, such as in the case of docker commit
|
|
if hc == nil {
|
|
return nil
|
|
}
|
|
|
|
if hc.IOMaximumBandwidth != 0 {
|
|
return fmt.Errorf("invalid QoS settings: %s does not support --maximum-bandwidth", runtime.GOOS)
|
|
}
|
|
|
|
if hc.IOMaximumIOps != 0 {
|
|
return fmt.Errorf("invalid QoS settings: %s does not support --maximum-iops", runtime.GOOS)
|
|
}
|
|
return nil
|
|
}
|