Skip to content

Commit e7b1e10

Browse files
committed
chore: add Tmpfs and UTSMode option
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent e7f9e86 commit e7b1e10

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

api/handlers/container/create.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) {
107107
}
108108
}
109109

110+
// Tmpfs:
111+
// Tmpfs are passed in as a map of strings,
112+
// but nerdctl expects an array of strings with format [TMPFS1:VALUE1, TMPFS2:VALUE2, ...].
113+
tmpfs := []string{}
114+
if req.HostConfig.Tmpfs != nil {
115+
for key, val := range req.HostConfig.Tmpfs {
116+
tmpfs = append(tmpfs, fmt.Sprintf("%s:%s", key, val))
117+
}
118+
}
119+
110120
// Environment vars:
111121
env := []string{}
112122
if req.Env != nil {
@@ -223,6 +233,7 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) {
223233
// #region for volume flags
224234
Volume: volumes,
225235
VolumesFrom: volumesFrom,
236+
Tmpfs: tmpfs,
226237
// #endregion
227238

228239
// #region for env flags
@@ -282,6 +293,7 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) {
282293
PortMappings: portMappings,
283294
AddHost: req.HostConfig.ExtraHosts, // Extra hosts.
284295
MACAddress: req.MacAddress,
296+
UTSNamespace: req.HostConfig.UTSMode,
285297
}
286298

287299
ctx := namespaces.WithNamespace(r.Context(), h.Config.Namespace)

api/handlers/container/create_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,29 @@ var _ = Describe("Container Create API ", func() {
675675
Expect(rr.Body).Should(MatchJSON(jsonResponse))
676676
})
677677

678+
It("should set Tmpfs and UTSMode option", func() {
679+
body := []byte(`{
680+
"Image": "test-image",
681+
"HostConfig": {
682+
"Tmpfs": { "/run": "rw,noexec,nosuid,size=65536k" },
683+
"UTSMode": "host"
684+
}
685+
}`)
686+
req, _ := http.NewRequest(http.MethodPost, "/containers/create", bytes.NewReader(body))
687+
688+
// expected create options
689+
createOpt.Tmpfs = []string{"/run:rw,noexec,nosuid,size=65536k"}
690+
netOpt.UTSNamespace = "host"
691+
692+
service.EXPECT().Create(gomock.Any(), "test-image", nil, equalTo(createOpt), equalTo(netOpt)).Return(
693+
cid, nil)
694+
695+
// handler should return success message with 201 status code.
696+
h.create(rr, req)
697+
Expect(rr).Should(HaveHTTPStatus(http.StatusCreated))
698+
Expect(rr.Body).Should(MatchJSON(jsonResponse))
699+
})
700+
678701
It("should return 404 if the image was not found", func() {
679702
body := []byte(`{"Image": "test-image"}`)
680703
req, _ := http.NewRequest(http.MethodPost, "/containers/create", bytes.NewReader(body))
@@ -881,6 +904,7 @@ func getDefaultCreateOpt(conf config.Config) types.ContainerCreateOptions {
881904
// #region for volume flags
882905
Volume: nil,
883906
VolumesFrom: []string{}, // nerdctl default.
907+
Tmpfs: []string{},
884908
// #endregion
885909

886910
// #region for env flags

api/types/container_types.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,16 @@ type ContainerHostConfig struct {
8181
IpcMode string // IPC namespace to use for the container
8282
// TODO: Cgroup CgroupSpec // Cgroup to use for the container
8383
// TODO: Links []string // List of links (in the name:alias form)
84-
OomKillDisable bool // specifies whether to disable OOM Killer
85-
OomScoreAdj int // specifies the tune container’s OOM preferences (-1000 to 1000, rootless: 100 to 1000)
86-
PidMode string // PID namespace to use for the container
87-
Privileged bool // Is the container in privileged mode
88-
ReadonlyRootfs bool // Is the container root filesystem in read-only
89-
SecurityOpt []string // List of string values to customize labels for MLS systems, such as SELinux. (["key=value"])
84+
OomKillDisable bool // specifies whether to disable OOM Killer
85+
OomScoreAdj int // specifies the tune container’s OOM preferences (-1000 to 1000, rootless: 100 to 1000)
86+
PidMode string // PID namespace to use for the container
87+
Privileged bool // Is the container in privileged mode
88+
ReadonlyRootfs bool // Is the container root filesystem in read-only
89+
SecurityOpt []string // List of string values to customize labels for MLS systems, such as SELinux. (["key=value"])
90+
Tmpfs map[string]string `json:",omitempty"` // List of tmpfs (mounts) used for the container
91+
UTSMode string // UTS namespace to use for the container
9092
// TODO: PublishAllPorts bool // Should docker publish all exposed port for the container
9193
// TODO: StorageOpt map[string]string `json:",omitempty"` // Storage driver options per container.
92-
// TODO: Tmpfs map[string]string `json:",omitempty"` // List of tmpfs (mounts) used for the container
93-
// TODO: UTSMode UTSMode // UTS namespace to use for the container
9494
// TODO: UsernsMode UsernsMode // The user namespace to use for the container
9595
// TODO: ShmSize int64 // Total shm memory usage
9696
// TODO: Sysctls map[string]string `json:",omitempty"` // List of Namespaced sysctls used for the container

0 commit comments

Comments
 (0)