Skip to content

Commit 97d7d94

Browse files
committed
chore: add Ulimits option
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent d45f8c2 commit 97d7d94

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

api/handlers/container/create.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) {
127127
}
128128
}
129129

130+
ulimits := []string{}
131+
if req.HostConfig.Ulimits != nil {
132+
for _, ulimit := range req.HostConfig.Ulimits {
133+
ulimits = append(ulimits, ulimit.String())
134+
}
135+
}
136+
130137
// Environment vars:
131138
env := []string{}
132139
if req.Env != nil {
@@ -233,6 +240,7 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) {
233240
MemorySwap: memorySwap, // Total memory usage (memory + swap); set `-1` to enable unlimited swap
234241
IPC: req.HostConfig.IpcMode, // IPC namespace to use
235242
ShmSize: shmSize, // ShmSize set the size of /dev/shm
243+
Ulimit: ulimits, // List of ulimits to be set in the container
236244
// #endregion
237245

238246
// #region for user flags

api/handlers/container/create_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,27 @@ var _ = Describe("Container Create API ", func() {
723723
Expect(rr.Body).Should(MatchJSON(jsonResponse))
724724
})
725725

726+
It("should set Ulimit option", func() {
727+
body := []byte(`{
728+
"Image": "test-image",
729+
"HostConfig": {
730+
"Ulimits": [{"Name": "nofile", "Soft": 1024, "Hard": 2048},{"Name": "nproc", "Soft": 1024, "Hard": 4048}]
731+
}
732+
}`)
733+
req, _ := http.NewRequest(http.MethodPost, "/containers/create", bytes.NewReader(body))
734+
735+
// expected create options
736+
createOpt.Ulimit = []string{"nofile=1024:2048", "nproc=1024:4048"}
737+
738+
service.EXPECT().Create(gomock.Any(), "test-image", nil, equalTo(createOpt), equalTo(netOpt)).Return(
739+
cid, nil)
740+
741+
// handler should return success message with 201 status code.
742+
h.create(rr, req)
743+
Expect(rr).Should(HaveHTTPStatus(http.StatusCreated))
744+
Expect(rr.Body).Should(MatchJSON(jsonResponse))
745+
})
746+
726747
It("should return 404 if the image was not found", func() {
727748
body := []byte(`{"Image": "test-image"}`)
728749
req, _ := http.NewRequest(http.MethodPost, "/containers/create", bytes.NewReader(body))
@@ -908,6 +929,7 @@ func getDefaultCreateOpt(conf config.Config) types.ContainerCreateOptions {
908929
MemorySwappiness64: -1, // nerdctl default.
909930
PidsLimit: -1, // nerdctl default.
910931
Cgroupns: defaults.CgroupnsMode(), // nerdctl default.
932+
Ulimit: []string{},
911933
// #endregion
912934

913935
// #region for user flags

api/types/container_types.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/containerd/nerdctl/pkg/inspecttypes/dockercompat"
1212
dockertypes "github.com/docker/docker/api/types"
1313
"github.com/docker/go-connections/nat"
14+
"github.com/docker/go-units"
1415
)
1516

1617
// AttachOptions defines the available options for the container attach call.
@@ -111,7 +112,8 @@ type ContainerHostConfig struct {
111112
MemorySwappiness int64 // MemorySwappiness64 specifies the tune container memory swappiness (0 to 100) (default -1)
112113
// TODO: Resources
113114

114-
BlkioWeight uint16 // Block IO weight (relative weight vs. other containers)
115+
Ulimits []*Ulimit // List of ulimits to be set in the container
116+
BlkioWeight uint16 // Block IO weight (relative weight vs. other containers)
115117
// Mounts specs used by the container
116118
// TODO: Mounts []mount.Mount `json:",omitempty"`
117119

@@ -258,3 +260,5 @@ type StatsJSON struct {
258260
// Networks request version >=1.21
259261
Networks map[string]dockertypes.NetworkStats `json:"networks,omitempty"`
260262
}
263+
264+
type Ulimit = units.Ulimit

0 commit comments

Comments
 (0)