2015-06-23 17:13:42 +00:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package runconfig
|
|
|
|
|
|
|
|
import (
|
2015-11-01 01:49:14 +00:00
|
|
|
"fmt"
|
|
|
|
"runtime"
|
2015-06-23 17:13:42 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2015-09-19 01:21:57 +00:00
|
|
|
// IsValid indicates is an isolation level is valid
|
|
|
|
func (i IsolationLevel) IsValid() bool {
|
|
|
|
return i.IsDefault()
|
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// IsPrivate indicates whether container uses it's private network stack.
|
2015-06-23 17:13:42 +00:00
|
|
|
func (n NetworkMode) IsPrivate() bool {
|
|
|
|
return !(n.IsHost() || n.IsContainer())
|
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// IsDefault indicates whether container uses the default network stack.
|
2015-06-23 17:13:42 +00:00
|
|
|
func (n NetworkMode) IsDefault() bool {
|
|
|
|
return n == "default"
|
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// DefaultDaemonNetworkMode returns the default network stack the daemon should
|
|
|
|
// use.
|
2015-06-23 17:13:42 +00:00
|
|
|
func DefaultDaemonNetworkMode() NetworkMode {
|
|
|
|
return NetworkMode("bridge")
|
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// NetworkName returns the name of the network stack.
|
2015-06-23 17:13:42 +00:00
|
|
|
func (n NetworkMode) NetworkName() string {
|
|
|
|
if n.IsBridge() {
|
|
|
|
return "bridge"
|
|
|
|
} else if n.IsHost() {
|
|
|
|
return "host"
|
|
|
|
} else if n.IsContainer() {
|
|
|
|
return "container"
|
|
|
|
} else if n.IsNone() {
|
|
|
|
return "none"
|
|
|
|
} else if n.IsDefault() {
|
|
|
|
return "default"
|
2015-09-25 10:19:17 +00:00
|
|
|
} else if n.IsUserDefined() {
|
|
|
|
return n.UserDefined()
|
2015-06-23 17:13:42 +00:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// IsBridge indicates whether container uses the bridge network stack
|
2015-06-23 17:13:42 +00:00
|
|
|
func (n NetworkMode) IsBridge() bool {
|
|
|
|
return n == "bridge"
|
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// IsHost indicates whether container uses the host network stack.
|
2015-06-23 17:13:42 +00:00
|
|
|
func (n NetworkMode) IsHost() bool {
|
|
|
|
return n == "host"
|
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// IsContainer indicates whether container uses a container network stack.
|
2015-06-23 17:13:42 +00:00
|
|
|
func (n NetworkMode) IsContainer() bool {
|
|
|
|
parts := strings.SplitN(string(n), ":", 2)
|
|
|
|
return len(parts) > 1 && parts[0] == "container"
|
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// IsNone indicates whether container isn't using a network stack.
|
2015-06-23 17:13:42 +00:00
|
|
|
func (n NetworkMode) IsNone() bool {
|
|
|
|
return n == "none"
|
|
|
|
}
|
2015-07-09 22:12:36 +00:00
|
|
|
|
2015-11-03 18:25:09 +00:00
|
|
|
// ConnectedContainer is the id of the container which network this container is connected to.
|
|
|
|
func (n NetworkMode) ConnectedContainer() string {
|
|
|
|
parts := strings.SplitN(string(n), ":", 2)
|
|
|
|
if len(parts) > 1 {
|
|
|
|
return parts[1]
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-09-25 10:19:17 +00:00
|
|
|
// IsUserDefined indicates user-created network
|
|
|
|
func (n NetworkMode) IsUserDefined() bool {
|
|
|
|
return !n.IsDefault() && !n.IsBridge() && !n.IsHost() && !n.IsNone() && !n.IsContainer()
|
|
|
|
}
|
|
|
|
|
2015-10-25 23:09:54 +00:00
|
|
|
// IsPreDefinedNetwork indicates if a network is predefined by the daemon
|
|
|
|
func IsPreDefinedNetwork(network string) bool {
|
|
|
|
n := NetworkMode(network)
|
|
|
|
return n.IsBridge() || n.IsHost() || n.IsNone()
|
|
|
|
}
|
|
|
|
|
2015-09-25 10:19:17 +00:00
|
|
|
//UserDefined indicates user-created network
|
|
|
|
func (n NetworkMode) UserDefined() string {
|
|
|
|
if n.IsUserDefined() {
|
|
|
|
return string(n)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-07-09 22:12:36 +00:00
|
|
|
// MergeConfigs merges the specified container Config and HostConfig.
|
|
|
|
// It creates a ContainerConfigWrapper.
|
|
|
|
func MergeConfigs(config *Config, hostConfig *HostConfig) *ContainerConfigWrapper {
|
|
|
|
return &ContainerConfigWrapper{
|
|
|
|
config,
|
|
|
|
hostConfig,
|
|
|
|
"", nil,
|
|
|
|
}
|
|
|
|
}
|
2015-11-01 01:49:14 +00:00
|
|
|
|
|
|
|
// ValidateNetMode ensures that the various combinations of requested
|
|
|
|
// network settings are valid.
|
|
|
|
func ValidateNetMode(c *Config, hc *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.IsHost() || hc.NetworkMode.IsContainer()) && c.Hostname != "" {
|
|
|
|
return ErrConflictNetworkHostname
|
|
|
|
}
|
|
|
|
|
|
|
|
if hc.NetworkMode.IsHost() && len(hc.Links) > 0 {
|
|
|
|
return ErrConflictHostNetworkAndLinks
|
|
|
|
}
|
|
|
|
|
|
|
|
if hc.NetworkMode.IsContainer() && len(hc.Links) > 0 {
|
|
|
|
return ErrConflictContainerNetworkAndLinks
|
|
|
|
}
|
|
|
|
|
|
|
|
if hc.NetworkMode.IsUserDefined() && len(hc.Links) > 0 {
|
|
|
|
return ErrConflictUserDefinedNetworkAndLinks
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hc.NetworkMode.IsHost() || hc.NetworkMode.IsContainer()) && len(hc.DNS) > 0 {
|
|
|
|
return ErrConflictNetworkAndDNS
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hc.NetworkMode.IsContainer() || hc.NetworkMode.IsHost()) && 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateIsolationLevel performs platform specific validation of the
|
|
|
|
// isolation level in the hostconfig structure. Linux only supports "default"
|
|
|
|
// which is LXC container isolation
|
|
|
|
func ValidateIsolationLevel(hc *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
|
|
|
|
}
|