Skip to content

Commit d45f8c2

Browse files
committed
chore: add ShmSize, Sysctl and Runtime option
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent e7b1e10 commit d45f8c2

File tree

3 files changed

+56
-7
lines changed

3 files changed

+56
-7
lines changed

api/handlers/container/create.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) {
117117
}
118118
}
119119

120+
// Sysctls:
121+
// Sysctls are passed in as a map of strings,
122+
// but nerdctl expects an array of strings with format [Sysctls1=VALUE1, Sysctls2=VALUE2, ...].
123+
sysctls := []string{}
124+
if req.HostConfig.Sysctls != nil {
125+
for key, val := range req.HostConfig.Sysctls {
126+
sysctls = append(sysctls, fmt.Sprintf("%s=%s", key, val))
127+
}
128+
}
129+
120130
// Environment vars:
121131
env := []string{}
122132
if req.Env != nil {
@@ -141,19 +151,29 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) {
141151

142152
memoryReservation := ""
143153
if req.HostConfig.MemoryReservation != 0 {
144-
memoryReservation = strconv.FormatInt(req.HostConfig.MemoryReservation, 10)
154+
memoryReservation = fmt.Sprint(req.HostConfig.MemoryReservation)
145155
}
146156

147157
memorySwap := ""
148158
if req.HostConfig.MemorySwap != 0 {
149-
memorySwap = strconv.FormatInt(req.HostConfig.MemorySwap, 10)
159+
memorySwap = fmt.Sprint(req.HostConfig.MemorySwap)
150160
}
151161

152162
memorySwappiness := int64(-1)
153-
if req.HostConfig.MemorySwappiness != 0 && req.HostConfig.MemorySwappiness > -1 {
163+
if req.HostConfig.MemorySwappiness > 0 {
154164
memorySwappiness = req.HostConfig.MemorySwappiness
155165
}
156166

167+
shmSize := ""
168+
if req.HostConfig.ShmSize > 0 {
169+
shmSize = fmt.Sprint(req.HostConfig.ShmSize)
170+
}
171+
172+
runtime := defaults.Runtime
173+
if req.HostConfig.Runtime != "" {
174+
runtime = req.HostConfig.Runtime
175+
}
176+
157177
volumesFrom := []string{}
158178
if req.HostConfig.VolumesFrom != nil {
159179
volumesFrom = req.HostConfig.VolumesFrom
@@ -212,6 +232,7 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) {
212232
MemoryReservation: memoryReservation, // Memory soft limit (in bytes)
213233
MemorySwap: memorySwap, // Total memory usage (memory + swap); set `-1` to enable unlimited swap
214234
IPC: req.HostConfig.IpcMode, // IPC namespace to use
235+
ShmSize: shmSize, // ShmSize set the size of /dev/shm
215236
// #endregion
216237

217238
// #region for user flags
@@ -227,7 +248,8 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) {
227248
// #endregion
228249

229250
// #region for runtime flags
230-
Runtime: defaults.Runtime, // nerdctl default.
251+
Runtime: runtime, // Runtime to use for this container, e.g. "crun", or "io.containerd.runc.v2".
252+
Sysctl: sysctls, // Sysctl set sysctl options, e.g "net.ipv4.ip_forward=1"
231253
// #endregion
232254

233255
// #region for volume flags
@@ -265,6 +287,7 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) {
265287

266288
// #region for rootfs flags
267289
ReadOnly: req.HostConfig.ReadonlyRootfs, // Is the container root filesystem in read-only
290+
// #endregion
268291
}
269292

270293
portMappings, err := translatePortMappings(req.HostConfig.PortBindings)

api/handlers/container/create_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,31 @@ var _ = Describe("Container Create API ", func() {
698698
Expect(rr.Body).Should(MatchJSON(jsonResponse))
699699
})
700700

701+
It("should set ShmSize, Sysctl and Runtime option", func() {
702+
body := []byte(`{
703+
"Image": "test-image",
704+
"HostConfig": {
705+
"Sysctls": { "net.ipv4.ip_forward": "1" },
706+
"ShmSize": 302348,
707+
"Runtime": "crun"
708+
}
709+
}`)
710+
req, _ := http.NewRequest(http.MethodPost, "/containers/create", bytes.NewReader(body))
711+
712+
// expected create options
713+
createOpt.ShmSize = "302348"
714+
createOpt.Sysctl = []string{"net.ipv4.ip_forward=1"}
715+
createOpt.Runtime = "crun"
716+
717+
service.EXPECT().Create(gomock.Any(), "test-image", nil, equalTo(createOpt), equalTo(netOpt)).Return(
718+
cid, nil)
719+
720+
// handler should return success message with 201 status code.
721+
h.create(rr, req)
722+
Expect(rr).Should(HaveHTTPStatus(http.StatusCreated))
723+
Expect(rr.Body).Should(MatchJSON(jsonResponse))
724+
})
725+
701726
It("should return 404 if the image was not found", func() {
702727
body := []byte(`{"Image": "test-image"}`)
703728
req, _ := http.NewRequest(http.MethodPost, "/containers/create", bytes.NewReader(body))
@@ -899,6 +924,7 @@ func getDefaultCreateOpt(conf config.Config) types.ContainerCreateOptions {
899924

900925
// #region for runtime flags
901926
Runtime: defaults.Runtime, // nerdctl default.
927+
Sysctl: []string{},
902928
// #endregion
903929

904930
// #region for volume flags

api/types/container_types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ type ContainerHostConfig struct {
8989
SecurityOpt []string // List of string values to customize labels for MLS systems, such as SELinux. (["key=value"])
9090
Tmpfs map[string]string `json:",omitempty"` // List of tmpfs (mounts) used for the container
9191
UTSMode string // UTS namespace to use for the container
92+
ShmSize int64 // Size of /dev/shm in bytes. The size must be greater than 0.
93+
Sysctls map[string]string `json:",omitempty"` // List of Namespaced sysctls used for the container
94+
Runtime string `json:",omitempty"` // Runtime to use with this container
9295
// TODO: PublishAllPorts bool // Should docker publish all exposed port for the container
9396
// TODO: StorageOpt map[string]string `json:",omitempty"` // Storage driver options per container.
9497
// TODO: UsernsMode UsernsMode // The user namespace to use for the container
95-
// TODO: ShmSize int64 // Total shm memory usage
96-
// TODO: Sysctls map[string]string `json:",omitempty"` // List of Namespaced sysctls used for the container
97-
// TODO: Runtime string `json:",omitempty"` // Runtime to use with this container
9898

9999
// Applicable to Windows
100100
// TODO: Isolation Isolation // Isolation technology of the container (e.g. default, hyperv)

0 commit comments

Comments
 (0)