Skip to content

Commit 7645a5f

Browse files
committed
chore: add CPUQuota option
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent 8716b82 commit 7645a5f

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

api/handlers/container/create.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) {
119119
capAdd = req.HostConfig.CapAdd
120120
}
121121

122+
CpuQuota := int64(-1)
123+
if req.HostConfig.CPUQuota != 0 {
124+
CpuQuota = int64(req.HostConfig.CPUQuota)
125+
}
126+
122127
globalOpt := ncTypes.GlobalCommandOptions(*h.Config)
123128
createOpt := ncTypes.ContainerCreateOptions{
124129
Stdout: nil,
@@ -148,7 +153,7 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) {
148153
// #region for resource flags
149154
CPUShares: uint64(req.HostConfig.CPUShares), // CPU shares (relative weight)
150155
Memory: memory, // memory limit (in bytes)
151-
CPUQuota: -1, // nerdctl default.
156+
CPUQuota: CpuQuota, // nerdctl default.
152157
MemorySwappiness64: -1, // nerdctl default.
153158
PidsLimit: -1, // nerdctl default.
154159
Cgroupns: defaults.CgroupnsMode(), // nerdctl default.

api/handlers/container/create_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,43 @@ var _ = Describe("Container Create API ", func() {
458458
Expect(rr.Body).Should(MatchJSON(jsonResponse))
459459
})
460460

461+
It("should set CpuQuota create options for resources", func() {
462+
body := []byte(`{
463+
"Image": "test-image",
464+
"HostConfig": {
465+
"CpuQuota": 50000
466+
}
467+
}`)
468+
req, _ := http.NewRequest(http.MethodPost, "/containers/create", bytes.NewReader(body))
469+
470+
// expected create options
471+
createOpt.CPUQuota = 50000
472+
service.EXPECT().Create(gomock.Any(), "test-image", nil, equalTo(createOpt), equalTo(netOpt)).Return(
473+
cid, nil)
474+
475+
// handler should return success message with 201 status code.
476+
h.create(rr, req)
477+
Expect(rr).Should(HaveHTTPStatus(http.StatusCreated))
478+
Expect(rr.Body).Should(MatchJSON(jsonResponse))
479+
})
480+
481+
It("should set CpuQuota to -1 by default", func() {
482+
body := []byte(`{
483+
"Image": "test-image"
484+
}`)
485+
req, _ := http.NewRequest(http.MethodPost, "/containers/create", bytes.NewReader(body))
486+
487+
// expected create options
488+
createOpt.CPUQuota = -1
489+
service.EXPECT().Create(gomock.Any(), "test-image", nil, equalTo(createOpt), equalTo(netOpt)).Return(
490+
cid, nil)
491+
492+
// handler should return success message with 201 status code.
493+
h.create(rr, req)
494+
Expect(rr).Should(HaveHTTPStatus(http.StatusCreated))
495+
Expect(rr.Body).Should(MatchJSON(jsonResponse))
496+
})
497+
461498
It("should return 404 if the image was not found", func() {
462499
body := []byte(`{"Image": "test-image"}`)
463500
req, _ := http.NewRequest(http.MethodPost, "/containers/create", bytes.NewReader(body))

api/types/container_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ type ContainerHostConfig struct {
103103
CPUShares int64 `json:"CpuShares"` // CPU shares (relative weight vs. other containers)
104104
Memory int64 // Memory limit (in bytes)
105105
CPUPeriod int64 `json:"CpuPeriod"` // CPU CFS (Completely Fair Scheduler) period
106+
CPUQuota int64 `json:"CpuQuota"` // CPU CFS (Completely Fair Scheduler) quota
106107
// TODO: Resources
107108

108109
BlkioWeight uint16 // Block IO weight (relative weight vs. other containers)

0 commit comments

Comments
 (0)