From 4089b4e4400d44f7c0a5b15065c70228f10ebf0c Mon Sep 17 00:00:00 2001 From: Qiang Huang Date: Wed, 2 Dec 2015 10:53:52 +0800 Subject: [PATCH] Set default MemorySwappiness when adapt It makes the inspect result consistent between cli and REST api when MemorySwappiness is not set. Signed-off-by: Qiang Huang --- daemon/container_unix.go | 6 +----- daemon/daemon_unix.go | 4 ++++ daemon/daemon_windows.go | 6 ++++-- daemon/start.go | 3 +++ integration-cli/docker_api_containers_test.go | 4 ++-- 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/daemon/container_unix.go b/daemon/container_unix.go index cd2c5d22c2..54efcf6e8c 100644 --- a/daemon/container_unix.go +++ b/daemon/container_unix.go @@ -296,11 +296,7 @@ func (daemon *Daemon) populateCommand(c *Container, env []string) error { Rlimits: rlimits, BlkioWeightDevice: weightDevices, OomKillDisable: c.hostConfig.OomKillDisable, - MemorySwappiness: -1, - } - - if c.hostConfig.MemorySwappiness != nil { - resources.MemorySwappiness = *c.hostConfig.MemorySwappiness + MemorySwappiness: *c.hostConfig.MemorySwappiness, } processConfig := execdriver.ProcessConfig{ diff --git a/daemon/daemon_unix.go b/daemon/daemon_unix.go index 2a4e7dcae6..1b76757883 100644 --- a/daemon/daemon_unix.go +++ b/daemon/daemon_unix.go @@ -138,6 +138,10 @@ func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig, a return err } } + if hostConfig.MemorySwappiness == nil { + defaultSwappiness := int64(-1) + hostConfig.MemorySwappiness = &defaultSwappiness + } return nil } diff --git a/daemon/daemon_windows.go b/daemon/daemon_windows.go index 2b887b98a9..d4a894d5fa 100644 --- a/daemon/daemon_windows.go +++ b/daemon/daemon_windows.go @@ -48,9 +48,9 @@ func checkKernel() error { // adaptContainerSettings is called during container creation to modify any // settings necessary in the HostConfig structure. -func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig, adjustCPUShares bool) { +func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig, adjustCPUShares bool) error { if hostConfig == nil { - return + return nil } if hostConfig.CPUShares < 0 { @@ -60,6 +60,8 @@ func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig, a logrus.Warnf("Changing requested CPUShares of %d to maximum allowed of %d", hostConfig.CPUShares, windowsMaxCPUShares) hostConfig.CPUShares = windowsMaxCPUShares } + + return nil } // verifyPlatformContainerSettings performs platform-specific validation of the diff --git a/daemon/start.go b/daemon/start.go index a8545de122..6edc19191b 100644 --- a/daemon/start.go +++ b/daemon/start.go @@ -36,6 +36,9 @@ func (daemon *Daemon) ContainerStart(name string, hostConfig *runconfig.HostConf return err } container.Unlock() + if err := daemon.adaptContainerSettings(hostConfig, false); err != nil { + return err + } if err := daemon.setHostConfig(container, hostConfig); err != nil { return err } diff --git a/integration-cli/docker_api_containers_test.go b/integration-cli/docker_api_containers_test.go index 22e30e4b67..b1b5488822 100644 --- a/integration-cli/docker_api_containers_test.go +++ b/integration-cli/docker_api_containers_test.go @@ -1434,7 +1434,7 @@ func (s *DockerSuite) TestPostContainersCreateShmSizeHostConfigOmitted(c *check. var containerJSON types.ContainerJSON c.Assert(json.Unmarshal(body, &containerJSON), check.IsNil) - c.Assert(containerJSON.HostConfig.ShmSize, check.IsNil) + c.Assert(*containerJSON.HostConfig.ShmSize, check.Equals, runconfig.DefaultSHMSize) out, _ := dockerCmd(c, "start", "-i", containerJSON.ID) shmRegexp := regexp.MustCompile(`shm on /dev/shm type tmpfs(.*)size=65536k`) @@ -1522,5 +1522,5 @@ func (s *DockerSuite) TestPostContainersCreateMemorySwappinessHostConfigOmitted( var containerJSON types.ContainerJSON c.Assert(json.Unmarshal(body, &containerJSON), check.IsNil) - c.Assert(containerJSON.HostConfig.MemorySwappiness, check.IsNil) + c.Assert(*containerJSON.HostConfig.MemorySwappiness, check.Equals, int64(-1)) }