Browse code

Do not set -1 for swappiness

Do not set a default value for swappiness as the default value should be
`nil`

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>

Michael Crosby authored on 2017/07/01 02:34:40
Showing 5 changed files
... ...
@@ -1243,3 +1243,11 @@ func (daemon *Daemon) checkpointAndSave(container *container.Container) error {
1243 1243
 	}
1244 1244
 	return nil
1245 1245
 }
1246
+
1247
+// because the CLI sends a -1 when it wants to unset the swappiness value
1248
+// we need to clear it on the server side
1249
+func fixMemorySwappiness(resources *containertypes.Resources) {
1250
+	if resources.MemorySwappiness != nil && *resources.MemorySwappiness == -1 {
1251
+		resources.MemorySwappiness = nil
1252
+	}
1253
+}
... ...
@@ -143,6 +143,7 @@ func UsingSystemd(config *Config) bool {
143 143
 // verifyPlatformContainerSettings performs platform-specific validation of the
144 144
 // hostconfig and config structures.
145 145
 func verifyPlatformContainerSettings(daemon *Daemon, hostConfig *containertypes.HostConfig, config *containertypes.Config, update bool) ([]string, error) {
146
+	fixMemorySwappiness(resources)
146 147
 	warnings := []string{}
147 148
 	sysInfo := sysinfo.New(true)
148 149
 	// NOTE: We do not enforce a minimum value for swap limits for zones on Solaris and
... ...
@@ -163,7 +164,7 @@ func verifyPlatformContainerSettings(daemon *Daemon, hostConfig *containertypes.
163 163
 	}
164 164
 	// Solaris NOTE: We allow and encourage setting the swap without setting the memory limit.
165 165
 
166
-	if hostConfig.MemorySwappiness != nil && *hostConfig.MemorySwappiness != -1 && !sysInfo.MemorySwappiness {
166
+	if hostConfig.MemorySwappiness != nil && !sysInfo.MemorySwappiness {
167 167
 		warnings = append(warnings, "Your kernel does not support memory swappiness capabilities, memory swappiness discarded.")
168 168
 		logrus.Warnf("Your kernel does not support memory swappiness capabilities, memory swappiness discarded.")
169 169
 		hostConfig.MemorySwappiness = nil
... ...
@@ -282,10 +282,6 @@ func (daemon *Daemon) adaptContainerSettings(hostConfig *containertypes.HostConf
282 282
 		return err
283 283
 	}
284 284
 	hostConfig.SecurityOpt = append(hostConfig.SecurityOpt, opts...)
285
-	if hostConfig.MemorySwappiness == nil {
286
-		defaultSwappiness := int64(-1)
287
-		hostConfig.MemorySwappiness = &defaultSwappiness
288
-	}
289 285
 	if hostConfig.OomKillDisable == nil {
290 286
 		defaultOomKillDisable := false
291 287
 		hostConfig.OomKillDisable = &defaultOomKillDisable
... ...
@@ -296,6 +292,7 @@ func (daemon *Daemon) adaptContainerSettings(hostConfig *containertypes.HostConf
296 296
 
297 297
 func verifyContainerResources(resources *containertypes.Resources, sysInfo *sysinfo.SysInfo, update bool) ([]string, error) {
298 298
 	warnings := []string{}
299
+	fixMemorySwappiness(resources)
299 300
 
300 301
 	// memory subsystem checks and adjustments
301 302
 	if resources.Memory != 0 && resources.Memory < linuxMinMemory {
... ...
@@ -318,14 +315,14 @@ func verifyContainerResources(resources *containertypes.Resources, sysInfo *sysi
318 318
 	if resources.Memory == 0 && resources.MemorySwap > 0 && !update {
319 319
 		return warnings, fmt.Errorf("You should always set the Memory limit when using Memoryswap limit, see usage")
320 320
 	}
321
-	if resources.MemorySwappiness != nil && *resources.MemorySwappiness != -1 && !sysInfo.MemorySwappiness {
321
+	if resources.MemorySwappiness != nil && !sysInfo.MemorySwappiness {
322 322
 		warnings = append(warnings, "Your kernel does not support memory swappiness capabilities or the cgroup is not mounted. Memory swappiness discarded.")
323 323
 		logrus.Warn("Your kernel does not support memory swappiness capabilities, or the cgroup is not mounted. Memory swappiness discarded.")
324 324
 		resources.MemorySwappiness = nil
325 325
 	}
326 326
 	if resources.MemorySwappiness != nil {
327 327
 		swappiness := *resources.MemorySwappiness
328
-		if swappiness < -1 || swappiness > 100 {
328
+		if swappiness < 0 || swappiness > 100 {
329 329
 			return warnings, fmt.Errorf("Invalid value: %v, valid memory swappiness range is 0-100", swappiness)
330 330
 		}
331 331
 	}
... ...
@@ -100,7 +100,7 @@ func (daemon *Daemon) adaptContainerSettings(hostConfig *containertypes.HostConf
100 100
 
101 101
 func verifyContainerResources(resources *containertypes.Resources, isHyperv bool) ([]string, error) {
102 102
 	warnings := []string{}
103
-
103
+	fixMemorySwappiness(resources)
104 104
 	if !isHyperv {
105 105
 		// The processor resource controls are mutually exclusive on
106 106
 		// Windows Server Containers, the order of precedence is
... ...
@@ -197,7 +197,7 @@ func verifyContainerResources(resources *containertypes.Resources, isHyperv bool
197 197
 	if resources.MemorySwap != 0 {
198 198
 		return warnings, fmt.Errorf("invalid option: Windows does not support MemorySwap")
199 199
 	}
200
-	if resources.MemorySwappiness != nil && *resources.MemorySwappiness != -1 {
200
+	if resources.MemorySwappiness != nil {
201 201
 		return warnings, fmt.Errorf("invalid option: Windows does not support MemorySwappiness")
202 202
 	}
203 203
 	if resources.OomKillDisable != nil && *resources.OomKillDisable {
... ...
@@ -1448,7 +1448,7 @@ func (s *DockerSuite) TestPostContainersCreateMemorySwappinessHostConfigOmitted(
1448 1448
 	var containerJSON types.ContainerJSON
1449 1449
 	c.Assert(json.Unmarshal(body, &containerJSON), check.IsNil)
1450 1450
 
1451
-	c.Assert(*containerJSON.HostConfig.MemorySwappiness, check.Equals, int64(-1))
1451
+	c.Assert(containerJSON.HostConfig.MemorySwappiness, check.IsNil)
1452 1452
 }
1453 1453
 
1454 1454
 // check validation is done daemon side and not only in cli