We should support update swap memory without memory.
Signed-off-by: Qiang Huang <h.huangqiang@huawei.com>
... | ... |
@@ -21,7 +21,7 @@ func (daemon *Daemon) ContainerCreate(params types.ContainerCreateConfig) (types |
21 | 21 |
return types.ContainerCreateResponse{}, derr.ErrorCodeEmptyConfig |
22 | 22 |
} |
23 | 23 |
|
24 |
- warnings, err := daemon.verifyContainerSettings(params.HostConfig, params.Config) |
|
24 |
+ warnings, err := daemon.verifyContainerSettings(params.HostConfig, params.Config, false) |
|
25 | 25 |
if err != nil { |
26 | 26 |
return types.ContainerCreateResponse{Warnings: warnings}, err |
27 | 27 |
} |
... | ... |
@@ -1450,7 +1450,7 @@ func setDefaultMtu(config *Config) { |
1450 | 1450 |
|
1451 | 1451 |
// verifyContainerSettings performs validation of the hostconfig and config |
1452 | 1452 |
// structures. |
1453 |
-func (daemon *Daemon) verifyContainerSettings(hostConfig *containertypes.HostConfig, config *containertypes.Config) ([]string, error) { |
|
1453 |
+func (daemon *Daemon) verifyContainerSettings(hostConfig *containertypes.HostConfig, config *containertypes.Config, update bool) ([]string, error) { |
|
1454 | 1454 |
|
1455 | 1455 |
// First perform verification of settings common across all platforms. |
1456 | 1456 |
if config != nil { |
... | ... |
@@ -1487,7 +1487,7 @@ func (daemon *Daemon) verifyContainerSettings(hostConfig *containertypes.HostCon |
1487 | 1487 |
} |
1488 | 1488 |
|
1489 | 1489 |
// Now do platform-specific verification |
1490 |
- return verifyPlatformContainerSettings(daemon, hostConfig, config) |
|
1490 |
+ return verifyPlatformContainerSettings(daemon, hostConfig, config, update) |
|
1491 | 1491 |
} |
1492 | 1492 |
|
1493 | 1493 |
// Checks if the client set configurations for more than one network while creating a container |
... | ... |
@@ -221,7 +221,7 @@ func (daemon *Daemon) adaptContainerSettings(hostConfig *containertypes.HostConf |
221 | 221 |
return nil |
222 | 222 |
} |
223 | 223 |
|
224 |
-func verifyContainerResources(resources *containertypes.Resources, sysInfo *sysinfo.SysInfo) ([]string, error) { |
|
224 |
+func verifyContainerResources(resources *containertypes.Resources, sysInfo *sysinfo.SysInfo, update bool) ([]string, error) { |
|
225 | 225 |
warnings := []string{} |
226 | 226 |
|
227 | 227 |
// memory subsystem checks and adjustments |
... | ... |
@@ -242,7 +242,7 @@ func verifyContainerResources(resources *containertypes.Resources, sysInfo *sysi |
242 | 242 |
if resources.Memory > 0 && resources.MemorySwap > 0 && resources.MemorySwap < resources.Memory { |
243 | 243 |
return warnings, fmt.Errorf("Minimum memoryswap limit should be larger than memory limit, see usage.") |
244 | 244 |
} |
245 |
- if resources.Memory == 0 && resources.MemorySwap > 0 { |
|
245 |
+ if resources.Memory == 0 && resources.MemorySwap > 0 && !update { |
|
246 | 246 |
return warnings, fmt.Errorf("You should always set the Memory limit when using Memoryswap limit, see usage.") |
247 | 247 |
} |
248 | 248 |
if resources.MemorySwappiness != nil && *resources.MemorySwappiness != -1 && !sysInfo.MemorySwappiness { |
... | ... |
@@ -383,7 +383,7 @@ func (daemon *Daemon) usingSystemd() bool { |
383 | 383 |
|
384 | 384 |
// verifyPlatformContainerSettings performs platform-specific validation of the |
385 | 385 |
// hostconfig and config structures. |
386 |
-func verifyPlatformContainerSettings(daemon *Daemon, hostConfig *containertypes.HostConfig, config *containertypes.Config) ([]string, error) { |
|
386 |
+func verifyPlatformContainerSettings(daemon *Daemon, hostConfig *containertypes.HostConfig, config *containertypes.Config, update bool) ([]string, error) { |
|
387 | 387 |
warnings := []string{} |
388 | 388 |
sysInfo := sysinfo.New(true) |
389 | 389 |
|
... | ... |
@@ -392,7 +392,7 @@ func verifyPlatformContainerSettings(daemon *Daemon, hostConfig *containertypes. |
392 | 392 |
return warnings, err |
393 | 393 |
} |
394 | 394 |
|
395 |
- w, err := verifyContainerResources(&hostConfig.Resources, sysInfo) |
|
395 |
+ w, err := verifyContainerResources(&hostConfig.Resources, sysInfo, update) |
|
396 | 396 |
if err != nil { |
397 | 397 |
return warnings, err |
398 | 398 |
} |
... | ... |
@@ -85,7 +85,7 @@ func (daemon *Daemon) adaptContainerSettings(hostConfig *containertypes.HostConf |
85 | 85 |
|
86 | 86 |
// verifyPlatformContainerSettings performs platform-specific validation of the |
87 | 87 |
// hostconfig and config structures. |
88 |
-func verifyPlatformContainerSettings(daemon *Daemon, hostConfig *containertypes.HostConfig, config *containertypes.Config) ([]string, error) { |
|
88 |
+func verifyPlatformContainerSettings(daemon *Daemon, hostConfig *containertypes.HostConfig, config *containertypes.Config, update bool) ([]string, error) { |
|
89 | 89 |
return nil, nil |
90 | 90 |
} |
91 | 91 |
|
... | ... |
@@ -58,7 +58,7 @@ func (daemon *Daemon) ContainerStart(name string, hostConfig *containertypes.Hos |
58 | 58 |
|
59 | 59 |
// check if hostConfig is in line with the current system settings. |
60 | 60 |
// It may happen cgroups are umounted or the like. |
61 |
- if _, err = daemon.verifyContainerSettings(container.HostConfig, nil); err != nil { |
|
61 |
+ if _, err = daemon.verifyContainerSettings(container.HostConfig, nil, false); err != nil { |
|
62 | 62 |
return err |
63 | 63 |
} |
64 | 64 |
// Adapt for old containers in case we have updates in this function and |
... | ... |
@@ -12,7 +12,7 @@ import ( |
12 | 12 |
func (daemon *Daemon) ContainerUpdate(name string, hostConfig *container.HostConfig) ([]string, error) { |
13 | 13 |
var warnings []string |
14 | 14 |
|
15 |
- warnings, err := daemon.verifyContainerSettings(hostConfig, nil) |
|
15 |
+ warnings, err := daemon.verifyContainerSettings(hostConfig, nil, true) |
|
16 | 16 |
if err != nil { |
17 | 17 |
return warnings, err |
18 | 18 |
} |
... | ... |
@@ -139,6 +139,22 @@ func (s *DockerSuite) TestUpdateKernelMemory(c *check.C) { |
139 | 139 |
c.Assert(strings.TrimSpace(out), checker.Equals, "104857600") |
140 | 140 |
} |
141 | 141 |
|
142 |
+func (s *DockerSuite) TestUpdateSwapMemoryOnly(c *check.C) { |
|
143 |
+ testRequires(c, DaemonIsLinux) |
|
144 |
+ testRequires(c, memoryLimitSupport) |
|
145 |
+ testRequires(c, swapMemorySupport) |
|
146 |
+ |
|
147 |
+ name := "test-update-container" |
|
148 |
+ dockerCmd(c, "run", "-d", "--name", name, "--memory", "300M", "--memory-swap", "500M", "busybox", "top") |
|
149 |
+ dockerCmd(c, "update", "--memory-swap", "600M", name) |
|
150 |
+ |
|
151 |
+ c.Assert(inspectField(c, name, "HostConfig.MemorySwap"), checker.Equals, "629145600") |
|
152 |
+ |
|
153 |
+ file := "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes" |
|
154 |
+ out, _ := dockerCmd(c, "exec", name, "cat", file) |
|
155 |
+ c.Assert(strings.TrimSpace(out), checker.Equals, "629145600") |
|
156 |
+} |
|
157 |
+ |
|
142 | 158 |
func (s *DockerSuite) TestUpdateStats(c *check.C) { |
143 | 159 |
testRequires(c, DaemonIsLinux) |
144 | 160 |
testRequires(c, memoryLimitSupport) |