Skip to content

Commit 12fa203

Browse files
committed
chore: add Memory options
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent 7645a5f commit 12fa203

File tree

3 files changed

+76
-6
lines changed

3 files changed

+76
-6
lines changed

api/handlers/container/create.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,21 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) {
124124
CpuQuota = int64(req.HostConfig.CPUQuota)
125125
}
126126

127+
memoryReservation := ""
128+
if req.HostConfig.MemoryReservation != 0 {
129+
memoryReservation = strconv.FormatInt(req.HostConfig.MemoryReservation, 10)
130+
}
131+
132+
memorySwap := ""
133+
if req.HostConfig.MemorySwap != 0 {
134+
memorySwap = strconv.FormatInt(req.HostConfig.MemorySwap, 10)
135+
}
136+
137+
memorySwappiness := int64(-1)
138+
if req.HostConfig.MemorySwappiness != 0 && req.HostConfig.MemorySwappiness > -1 {
139+
memorySwappiness = req.HostConfig.MemorySwappiness
140+
}
141+
127142
globalOpt := ncTypes.GlobalCommandOptions(*h.Config)
128143
createOpt := ncTypes.ContainerCreateOptions{
129144
Stdout: nil,
@@ -154,11 +169,15 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) {
154169
CPUShares: uint64(req.HostConfig.CPUShares), // CPU shares (relative weight)
155170
Memory: memory, // memory limit (in bytes)
156171
CPUQuota: CpuQuota, // nerdctl default.
157-
MemorySwappiness64: -1, // nerdctl default.
172+
MemorySwappiness64: memorySwappiness, // Tuning container memory swappiness behaviour
158173
PidsLimit: -1, // nerdctl default.
159174
Cgroupns: defaults.CgroupnsMode(), // nerdctl default.
160175
BlkioWeight: req.HostConfig.BlkioWeight, // block IO weight (relative)
161-
CPUPeriod: uint64(req.HostConfig.CPUPeriod),
176+
CPUPeriod: uint64(req.HostConfig.CPUPeriod), // CPU CFS (Completely Fair Scheduler) period
177+
CPUSetCPUs: req.HostConfig.CPUSetCPUs, // CpusetCpus 0-2, 0,1
178+
CPUSetMems: req.HostConfig.CPUSetMems, // CpusetMems 0-2, 0,1
179+
MemoryReservation: memoryReservation, // Memory soft limit (in bytes)
180+
MemorySwap: memorySwap, // Total memory usage (memory + swap); set `-1` to enable unlimited swap
162181
// #endregion
163182

164183
// #region for user flags

api/handlers/container/create_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,52 @@ var _ = Describe("Container Create API ", func() {
495495
Expect(rr.Body).Should(MatchJSON(jsonResponse))
496496
})
497497

498+
It("should set CpuSet create options for resources", func() {
499+
body := []byte(`{
500+
"Image": "test-image",
501+
"HostConfig": {
502+
"CpusetCpus": "0,1",
503+
"CpusetMems": "0,3"
504+
}
505+
}`)
506+
req, _ := http.NewRequest(http.MethodPost, "/containers/create", bytes.NewReader(body))
507+
508+
// expected create options
509+
createOpt.CPUSetCPUs = "0,1"
510+
createOpt.CPUSetMems = "0,3"
511+
service.EXPECT().Create(gomock.Any(), "test-image", nil, equalTo(createOpt), equalTo(netOpt)).Return(
512+
cid, nil)
513+
514+
// handler should return success message with 201 status code.
515+
h.create(rr, req)
516+
Expect(rr).Should(HaveHTTPStatus(http.StatusCreated))
517+
Expect(rr.Body).Should(MatchJSON(jsonResponse))
518+
})
519+
520+
It("should set MemoryReservation, MemorySwap and MemorySwappiness create options for resources", func() {
521+
body := []byte(`{
522+
"Image": "test-image",
523+
"HostConfig": {
524+
"MemoryReservation": 209715200,
525+
"MemorySwap": 514288000,
526+
"MemorySwappiness": 25
527+
}
528+
}`)
529+
req, _ := http.NewRequest(http.MethodPost, "/containers/create", bytes.NewReader(body))
530+
531+
// expected create options
532+
createOpt.MemoryReservation = "209715200"
533+
createOpt.MemorySwap = "514288000"
534+
createOpt.MemorySwappiness64 = 25
535+
service.EXPECT().Create(gomock.Any(), "test-image", nil, equalTo(createOpt), equalTo(netOpt)).Return(
536+
cid, nil)
537+
538+
// handler should return success message with 201 status code.
539+
h.create(rr, req)
540+
Expect(rr).Should(HaveHTTPStatus(http.StatusCreated))
541+
Expect(rr.Body).Should(MatchJSON(jsonResponse))
542+
})
543+
498544
It("should return 404 if the image was not found", func() {
499545
body := []byte(`{"Image": "test-image"}`)
500546
req, _ := http.NewRequest(http.MethodPost, "/containers/create", bytes.NewReader(body))

api/types/container_types.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,15 @@ type ContainerHostConfig struct {
100100
// TODO: Isolation Isolation // Isolation technology of the container (e.g. default, hyperv)
101101

102102
// Contains container's resources (cgroups, ulimits)
103-
CPUShares int64 `json:"CpuShares"` // CPU shares (relative weight vs. other containers)
104-
Memory int64 // Memory limit (in bytes)
105-
CPUPeriod int64 `json:"CpuPeriod"` // CPU CFS (Completely Fair Scheduler) period
106-
CPUQuota int64 `json:"CpuQuota"` // CPU CFS (Completely Fair Scheduler) quota
103+
CPUShares int64 `json:"CpuShares"` // CPU shares (relative weight vs. other containers)
104+
Memory int64 // Memory limit (in bytes)
105+
CPUPeriod int64 `json:"CpuPeriod"` // CPU CFS (Completely Fair Scheduler) period
106+
CPUQuota int64 `json:"CpuQuota"` // CPU CFS (Completely Fair Scheduler) quota
107+
CPUSetCPUs string `json:"CpusetCpus"` // CPUSetCPUs specifies the CPUs in which to allow execution (0-3, 0,1)
108+
CPUSetMems string `json:"CpusetMems"` // CPUSetMems specifies the memory nodes (MEMs) in which to allow execution (0-3, 0,1). Only effective on NUMA systems.
109+
MemoryReservation int64 // MemoryReservation specifies the memory soft limit (in bytes)
110+
MemorySwap int64 // Total memory usage (memory + swap); set `-1` to enable unlimited swap
111+
MemorySwappiness int64 // MemorySwappiness64 specifies the tune container memory swappiness (0 to 100) (default -1)
107112
// TODO: Resources
108113

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

0 commit comments

Comments
 (0)