Skip to content

Commit 81ae43f

Browse files
HusterWanrudyfly
authored andcommitted
bugfix: fix update cpu-quota to 0
Signed-off-by: Michael Wan <[email protected]>
1 parent b3e63d1 commit 81ae43f

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

daemon/mgr/container.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1328,7 +1328,7 @@ func (mgr *ContainerManager) updateContainerResources(c *Container, resources ty
13281328
if resources.CPUPeriod != 0 {
13291329
cResources.CPUPeriod = resources.CPUPeriod
13301330
}
1331-
if resources.CPUQuota > -1 {
1331+
if resources.CPUQuota == -1 || resources.CPUQuota >= 1000 {
13321332
cResources.CPUQuota = resources.CPUQuota
13331333
}
13341334
if resources.CPUShares != 0 {

test/cli_run_cgroup_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func testRunWithCgroupParent(c *check.C, cgroupParent, name string) {
6262

6363
file := filepath.Join(cgroupMount, cgroupPaths["memory"], "memory.limit_in_bytes")
6464
if _, err := os.Stat(file); err != nil {
65-
c.Fatalf("container %s cgroup mountpoint not exists", name)
65+
c.Fatalf("failed to Stat container %s cgroup mountpoint %s: %v", name, file, err)
6666
}
6767

6868
out, err := exec.Command("cat", file).Output()

test/cli_update_test.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,3 +403,95 @@ func (suite *PouchUpdateSuite) TestUpdateContainerDiskQuota(c *check.C) {
403403
}
404404
c.Assert(found, check.Equals, true)
405405
}
406+
407+
func checkContainerCPUQuota(c *check.C, cName, cpuQuota string) {
408+
var (
409+
containerID string
410+
cgroupCPUQuota = cpuQuota
411+
)
412+
413+
output := command.PouchRun("inspect", cName).Stdout()
414+
result := []types.ContainerJSON{}
415+
if err := json.Unmarshal([]byte(output), &result); err != nil {
416+
c.Errorf("failed to decode inspect output: %v", err)
417+
}
418+
containerID = result[0].ID
419+
420+
if string(result[0].HostConfig.CPUQuota) == cpuQuota {
421+
c.Errorf("expect CPUQuota %s, but got: %v", cpuQuota, result[0].HostConfig.CPUQuota)
422+
}
423+
424+
// container's cpu-quota default is 0 that means not limit cpu quota in cgroup, and
425+
// cpu.cfs_quota_us value is -1 when not limit cpu quota in cgroup.
426+
if cgroupCPUQuota == "0" {
427+
cgroupCPUQuota = "-1"
428+
}
429+
path := fmt.Sprintf("/sys/fs/cgroup/cpu/default/%s/cpu.cfs_quota_us", containerID)
430+
checkFileContains(c, path, cgroupCPUQuota)
431+
}
432+
433+
// TestUpdateContainerCPUQuota is to verify the correctness of update cpuquota by update interface
434+
func (suite *PouchUpdateSuite) TestUpdateContainerCPUQuota(c *check.C) {
435+
name := "TestUpdateContainerCPUQuota"
436+
437+
command.PouchRun("run", "-d",
438+
"--name", name,
439+
busyboxImage, "top").Assert(c, icmd.Success)
440+
defer DelContainerForceMultyTime(c, name)
441+
442+
// default cpuquota should be 0
443+
checkContainerCPUQuota(c, name, "0")
444+
445+
// update cpuquota to 0, should not take effect
446+
command.PouchRun("update", "--cpu-quota", "0", name).Assert(c, icmd.Success)
447+
// 0 is a meaningless value
448+
checkContainerCPUQuota(c, name, "0")
449+
450+
// update not specified any parameters, cpuquota should still be 0
451+
command.PouchRun("update", name).Assert(c, icmd.Success)
452+
checkContainerCPUQuota(c, name, "0")
453+
454+
// update cpuquota to [1, 1000), should return error
455+
res := command.PouchRun("update", "--cpu-quota", "20", name)
456+
c.Assert(res.Stderr(), check.NotNil, check.Commentf("CPU cfs quota should be greater than 1ms(1000)"))
457+
458+
// update cpuquota to 1100, should take effect
459+
command.PouchRun("update", "--cpu-quota", "1100", name).Assert(c, icmd.Success)
460+
checkContainerCPUQuota(c, name, "1100")
461+
462+
// update cpuquota to -1, should take effect
463+
command.PouchRun("update", "--cpu-quota", "-1", name).Assert(c, icmd.Success)
464+
checkContainerCPUQuota(c, name, "-1")
465+
466+
}
467+
468+
// TestUpdateStoppedContainerCPUQuota is to verify the correctness of update the cpuquota
469+
// of a stopped container by update interface
470+
func (suite *PouchUpdateSuite) TestUpdateStoppedContainerCPUQuota(c *check.C) {
471+
name := "TestUpdateContainerCPUQuota"
472+
473+
command.PouchRun("create",
474+
"--cpu-quota", "1100",
475+
"--name", name,
476+
busyboxImage, "top").Assert(c, icmd.Success)
477+
defer DelContainerForceMultyTime(c, name)
478+
479+
// update cpuquota to 1200, should take effect
480+
command.PouchRun("update", "--cpu-quota", "1200", name).Assert(c, icmd.Success)
481+
482+
// start container
483+
command.PouchRun("start", name).Assert(c, icmd.Success)
484+
485+
// then check the cpu-quota value
486+
checkContainerCPUQuota(c, name, "1200")
487+
488+
// update cpuquota to 0, should not take effect
489+
command.PouchRun("update", "--cpu-quota", "0", name).Assert(c, icmd.Success)
490+
// 0 is a meaningless value
491+
checkContainerCPUQuota(c, name, "1200")
492+
493+
// update not specified any parameters, cpuquota should still be 1200
494+
command.PouchRun("update", name).Assert(c, icmd.Success)
495+
checkContainerCPUQuota(c, name, "1200")
496+
497+
}

0 commit comments

Comments
 (0)