Skip to content

Commit 433fde3

Browse files
committed
chore: add PidLimit option
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent 02f96df commit 433fde3

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

api/handlers/container/create.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) {
136136

137137
// devices:
138138
// devices are passed in as a map of DeviceMapping,
139-
// but nerdctl expects an array of strings with format [devices1:VALUE1, devices2:VALUE2, ...].
139+
// but nerdctl expects an array of strings with format [PathOnHost1:PathInContainer1:CgroupPermissions1, PathOnHost2:PathInContainer2:CgroupPermissions2, ...].
140140
devices := []string{}
141141
if req.HostConfig.Devices != nil {
142142
for _, deviceMap := range req.HostConfig.Devices {
@@ -220,6 +220,10 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) {
220220
securityOpt = req.HostConfig.SecurityOpt
221221
}
222222

223+
pidLimit := int64(-1)
224+
if req.HostConfig.PidsLimit > 0 {
225+
pidLimit = req.HostConfig.PidsLimit
226+
}
223227
globalOpt := ncTypes.GlobalCommandOptions(*h.Config)
224228
createOpt := ncTypes.ContainerCreateOptions{
225229
Stdout: nil,
@@ -254,7 +258,7 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) {
254258
Memory: memory, // memory limit (in bytes)
255259
CPUQuota: CpuQuota, // nerdctl default.
256260
MemorySwappiness64: memorySwappiness, // Tuning container memory swappiness behaviour
257-
PidsLimit: -1, // nerdctl default.
261+
PidsLimit: pidLimit, // PidsLimit specifies the tune container pids limit
258262
Cgroupns: defaults.CgroupnsMode(), // nerdctl default.
259263
BlkioWeight: req.HostConfig.BlkioWeight, // block IO weight (relative)
260264
CPUPeriod: uint64(req.HostConfig.CPUPeriod), // CPU CFS (Completely Fair Scheduler) period

api/handlers/container/create_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -744,17 +744,19 @@ var _ = Describe("Container Create API ", func() {
744744
Expect(rr.Body).Should(MatchJSON(jsonResponse))
745745
})
746746

747-
It("should set Devices option", func() {
747+
It("should set Devices and PidLimit option", func() {
748748
body := []byte(`{
749749
"Image": "test-image",
750750
"HostConfig": {
751-
"Devices": [{"PathOnHost": "/dev/null", "PathInContainer": "/dev/null", "CgroupPermissions": "rwm"},{"PathOnHost": "/var/lib", "CgroupPermissions": "ro"}]
751+
"Devices": [{"PathOnHost": "/dev/null", "PathInContainer": "/dev/null", "CgroupPermissions": "rwm"},{"PathOnHost": "/var/lib", "CgroupPermissions": "ro"}],
752+
"PidsLimit": 200
752753
}
753754
}`)
754755
req, _ := http.NewRequest(http.MethodPost, "/containers/create", bytes.NewReader(body))
755756

756757
// expected create options
757758
createOpt.Device = []string{"/dev/null:/dev/null:rwm", "/var/lib:ro"}
759+
createOpt.PidsLimit = 200
758760

759761
service.EXPECT().Create(gomock.Any(), "test-image", nil, equalTo(createOpt), equalTo(netOpt)).Return(
760762
cid, nil)

api/types/container_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ type ContainerHostConfig struct {
115115
Ulimits []*Ulimit // List of ulimits to be set in the container
116116
BlkioWeight uint16 // Block IO weight (relative weight vs. other containers)
117117
Devices []DeviceMapping // List of devices to map inside the container
118+
PidsLimit int64 // Setting PIDs limit for a container; Set `0` or `-1` for unlimited, or `null` to not change.
118119
// Mounts specs used by the container
119120
// TODO: Mounts []mount.Mount `json:",omitempty"`
120121

0 commit comments

Comments
 (0)