mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
optimize code to clarify logic
Signed-off-by: Ma Shimiao <mashimiao.fnst@cn.fujitsu.com>
This commit is contained in:
parent
1899b2f41a
commit
93cdb0071b
2 changed files with 33 additions and 25 deletions
|
@ -2,7 +2,6 @@ package daemon
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/docker/docker/graph"
|
"github.com/docker/docker/graph"
|
||||||
"github.com/docker/docker/image"
|
"github.com/docker/docker/image"
|
||||||
|
@ -12,27 +11,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (daemon *Daemon) ContainerCreate(name string, config *runconfig.Config, hostConfig *runconfig.HostConfig) (string, []string, error) {
|
func (daemon *Daemon) ContainerCreate(name string, config *runconfig.Config, hostConfig *runconfig.HostConfig) (string, []string, error) {
|
||||||
var warnings []string
|
warnings, err := daemon.verifyHostConfig(hostConfig)
|
||||||
|
if err != nil {
|
||||||
if hostConfig.LxcConf.Len() > 0 && !strings.Contains(daemon.ExecutionDriver().Name(), "lxc") {
|
return "", warnings, err
|
||||||
return "", warnings, fmt.Errorf("Cannot use --lxc-conf with execdriver: %s", daemon.ExecutionDriver().Name())
|
|
||||||
}
|
|
||||||
if hostConfig.Memory != 0 && hostConfig.Memory < 4194304 {
|
|
||||||
return "", warnings, fmt.Errorf("Minimum memory limit allowed is 4MB")
|
|
||||||
}
|
|
||||||
if hostConfig.Memory > 0 && !daemon.SystemConfig().MemoryLimit {
|
|
||||||
warnings = append(warnings, "Your kernel does not support memory limit capabilities. Limitation discarded.\n")
|
|
||||||
hostConfig.Memory = 0
|
|
||||||
}
|
|
||||||
if hostConfig.Memory > 0 && hostConfig.MemorySwap != -1 && !daemon.SystemConfig().SwapLimit {
|
|
||||||
warnings = append(warnings, "Your kernel does not support swap limit capabilities. Limitation discarded.\n")
|
|
||||||
hostConfig.MemorySwap = -1
|
|
||||||
}
|
|
||||||
if hostConfig.Memory > 0 && hostConfig.MemorySwap > 0 && hostConfig.MemorySwap < hostConfig.Memory {
|
|
||||||
return "", warnings, fmt.Errorf("Minimum memoryswap limit should be larger than memory limit, see usage.\n")
|
|
||||||
}
|
|
||||||
if hostConfig.Memory == 0 && hostConfig.MemorySwap > 0 {
|
|
||||||
return "", warnings, fmt.Errorf("You should always set the Memory limit when using Memoryswap limit, see usage.\n")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
container, buildWarnings, err := daemon.Create(config, hostConfig, name)
|
container, buildWarnings, err := daemon.Create(config, hostConfig, name)
|
||||||
|
@ -46,9 +27,6 @@ func (daemon *Daemon) ContainerCreate(name string, config *runconfig.Config, hos
|
||||||
}
|
}
|
||||||
return "", warnings, err
|
return "", warnings, err
|
||||||
}
|
}
|
||||||
if !container.Config.NetworkDisabled && daemon.SystemConfig().IPv4ForwardingDisabled {
|
|
||||||
warnings = append(warnings, "IPv4 forwarding is disabled.\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
container.LogEvent("create")
|
container.LogEvent("create")
|
||||||
warnings = append(warnings, buildWarnings...)
|
warnings = append(warnings, buildWarnings...)
|
||||||
|
@ -80,6 +58,9 @@ func (daemon *Daemon) Create(config *runconfig.Config, hostConfig *runconfig.Hos
|
||||||
if warnings, err = daemon.mergeAndVerifyConfig(config, img); err != nil {
|
if warnings, err = daemon.mergeAndVerifyConfig(config, img); err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
if !config.NetworkDisabled && daemon.SystemConfig().IPv4ForwardingDisabled {
|
||||||
|
warnings = append(warnings, "IPv4 forwarding is disabled.\n")
|
||||||
|
}
|
||||||
if hostConfig == nil {
|
if hostConfig == nil {
|
||||||
hostConfig = &runconfig.HostConfig{}
|
hostConfig = &runconfig.HostConfig{}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1224,3 +1224,30 @@ func checkKernel() error {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (daemon *Daemon) verifyHostConfig(hostConfig *runconfig.HostConfig) ([]string, error) {
|
||||||
|
var warnings []string
|
||||||
|
|
||||||
|
if hostConfig.LxcConf.Len() > 0 && !strings.Contains(daemon.ExecutionDriver().Name(), "lxc") {
|
||||||
|
return warnings, fmt.Errorf("Cannot use --lxc-conf with execdriver: %s", daemon.ExecutionDriver().Name())
|
||||||
|
}
|
||||||
|
if hostConfig.Memory != 0 && hostConfig.Memory < 4194304 {
|
||||||
|
return warnings, fmt.Errorf("Minimum memory limit allowed is 4MB")
|
||||||
|
}
|
||||||
|
if hostConfig.Memory > 0 && !daemon.SystemConfig().MemoryLimit {
|
||||||
|
warnings = append(warnings, "Your kernel does not support memory limit capabilities. Limitation discarded.\n")
|
||||||
|
hostConfig.Memory = 0
|
||||||
|
}
|
||||||
|
if hostConfig.Memory > 0 && hostConfig.MemorySwap != -1 && !daemon.SystemConfig().SwapLimit {
|
||||||
|
warnings = append(warnings, "Your kernel does not support swap limit capabilities. Limitation discarded.\n")
|
||||||
|
hostConfig.MemorySwap = -1
|
||||||
|
}
|
||||||
|
if hostConfig.Memory > 0 && hostConfig.MemorySwap > 0 && hostConfig.MemorySwap < hostConfig.Memory {
|
||||||
|
return warnings, fmt.Errorf("Minimum memoryswap limit should be larger than memory limit, see usage.\n")
|
||||||
|
}
|
||||||
|
if hostConfig.Memory == 0 && hostConfig.MemorySwap > 0 {
|
||||||
|
return warnings, fmt.Errorf("You should always set the Memory limit when using Memoryswap limit, see usage.\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
return warnings, nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue