Bläddra i källkod

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 <h.huangqiang@huawei.com>
Qiang Huang 9 år sedan
förälder
incheckning
4089b4e440

+ 1 - 5
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{

+ 4 - 0
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
 }

+ 4 - 2
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

+ 3 - 0
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
 			}

+ 2 - 2
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))
 }