Browse code

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 authored on 2015/12/02 11:53:52
Showing 5 changed files
... ...
@@ -296,11 +296,7 @@ func (daemon *Daemon) populateCommand(c *Container, env []string) error {
296 296
 		Rlimits:           rlimits,
297 297
 		BlkioWeightDevice: weightDevices,
298 298
 		OomKillDisable:    c.hostConfig.OomKillDisable,
299
-		MemorySwappiness:  -1,
300
-	}
301
-
302
-	if c.hostConfig.MemorySwappiness != nil {
303
-		resources.MemorySwappiness = *c.hostConfig.MemorySwappiness
299
+		MemorySwappiness:  *c.hostConfig.MemorySwappiness,
304 300
 	}
305 301
 
306 302
 	processConfig := execdriver.ProcessConfig{
... ...
@@ -138,6 +138,10 @@ func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig, a
138 138
 			return err
139 139
 		}
140 140
 	}
141
+	if hostConfig.MemorySwappiness == nil {
142
+		defaultSwappiness := int64(-1)
143
+		hostConfig.MemorySwappiness = &defaultSwappiness
144
+	}
141 145
 
142 146
 	return nil
143 147
 }
... ...
@@ -48,9 +48,9 @@ func checkKernel() error {
48 48
 
49 49
 // adaptContainerSettings is called during container creation to modify any
50 50
 // settings necessary in the HostConfig structure.
51
-func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig, adjustCPUShares bool) {
51
+func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig, adjustCPUShares bool) error {
52 52
 	if hostConfig == nil {
53
-		return
53
+		return nil
54 54
 	}
55 55
 
56 56
 	if hostConfig.CPUShares < 0 {
... ...
@@ -60,6 +60,8 @@ func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig, a
60 60
 		logrus.Warnf("Changing requested CPUShares of %d to maximum allowed of %d", hostConfig.CPUShares, windowsMaxCPUShares)
61 61
 		hostConfig.CPUShares = windowsMaxCPUShares
62 62
 	}
63
+
64
+	return nil
63 65
 }
64 66
 
65 67
 // verifyPlatformContainerSettings performs platform-specific validation of the
... ...
@@ -36,6 +36,9 @@ func (daemon *Daemon) ContainerStart(name string, hostConfig *runconfig.HostConf
36 36
 				return err
37 37
 			}
38 38
 			container.Unlock()
39
+			if err := daemon.adaptContainerSettings(hostConfig, false); err != nil {
40
+				return err
41
+			}
39 42
 			if err := daemon.setHostConfig(container, hostConfig); err != nil {
40 43
 				return err
41 44
 			}
... ...
@@ -1434,7 +1434,7 @@ func (s *DockerSuite) TestPostContainersCreateShmSizeHostConfigOmitted(c *check.
1434 1434
 	var containerJSON types.ContainerJSON
1435 1435
 	c.Assert(json.Unmarshal(body, &containerJSON), check.IsNil)
1436 1436
 
1437
-	c.Assert(containerJSON.HostConfig.ShmSize, check.IsNil)
1437
+	c.Assert(*containerJSON.HostConfig.ShmSize, check.Equals, runconfig.DefaultSHMSize)
1438 1438
 
1439 1439
 	out, _ := dockerCmd(c, "start", "-i", containerJSON.ID)
1440 1440
 	shmRegexp := regexp.MustCompile(`shm on /dev/shm type tmpfs(.*)size=65536k`)
... ...
@@ -1522,5 +1522,5 @@ func (s *DockerSuite) TestPostContainersCreateMemorySwappinessHostConfigOmitted(
1522 1522
 	var containerJSON types.ContainerJSON
1523 1523
 	c.Assert(json.Unmarshal(body, &containerJSON), check.IsNil)
1524 1524
 
1525
-	c.Assert(containerJSON.HostConfig.MemorySwappiness, check.IsNil)
1525
+	c.Assert(*containerJSON.HostConfig.MemorySwappiness, check.Equals, int64(-1))
1526 1526
 }