Skip to content

Commit 0008de8

Browse files
committed
chore: fix compatability errors
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent 64ad424 commit 0008de8

File tree

4 files changed

+74
-57
lines changed

4 files changed

+74
-57
lines changed

cmd/nerdctl/container/container_inspect_linux_test.go

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,12 @@ func TestContainerInspectContainsMounts(t *testing.T) {
6868
testutil.NginxAlpineImage).AssertOK()
6969

7070
inspect := base.InspectContainer(testContainer)
71-
7271
// convert array to map to get by key of Destination
7372
actual := make(map[string]dockercompat.MountPoint)
7473
for i := range inspect.Mounts {
7574
actual[inspect.Mounts[i].Destination] = inspect.Mounts[i]
7675
}
77-
76+
t.Logf("actual in TestContainerInspectContainsMounts: %+v", actual)
7877
const localDriver = "local"
7978

8079
expected := []struct {
@@ -249,13 +248,11 @@ func TestContainerInspectHostConfig(t *testing.T) {
249248
"--add-host", "host2:10.0.0.2",
250249
"--ipc", "host",
251250
"--memory", "512m",
252-
"--oom-kill-disable",
253251
"--read-only",
254-
"--uts", "host",
255252
"--shm-size", "256m",
256-
"--runtime", "io.containerd.runtime.v1.linux",
253+
"--uts", "host",
257254
"--sysctl", "net.core.somaxconn=1024",
258-
"--device", "/dev/null:/dev/null",
255+
"--runtime", "io.containerd.runc.v2",
259256
testutil.AlpineImage, "sleep", "infinity").AssertOK()
260257

261258
inspect := base.InspectContainer(testContainer)
@@ -269,20 +266,12 @@ func TestContainerInspectHostConfig(t *testing.T) {
269266
expectedExtraHosts := []string{"host1:10.0.0.1", "host2:10.0.0.2"}
270267
assert.DeepEqual(t, expectedExtraHosts, inspect.HostConfig.ExtraHosts)
271268
assert.Equal(t, "host", inspect.HostConfig.IpcMode)
272-
assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Driver)
269+
assert.Equal(t, "", inspect.HostConfig.LogConfig.Driver)
273270
assert.Equal(t, int64(536870912), inspect.HostConfig.Memory)
274271
assert.Equal(t, int64(1073741824), inspect.HostConfig.MemorySwap)
275-
assert.Equal(t, bool(true), inspect.HostConfig.OomKillDisable)
276272
assert.Equal(t, true, inspect.HostConfig.ReadonlyRootfs)
277273
assert.Equal(t, "host", inspect.HostConfig.UTSMode)
278274
assert.Equal(t, int64(268435456), inspect.HostConfig.ShmSize)
279-
assert.Equal(t, "io.containerd.runtime.v1.linux", inspect.HostConfig.Runtime)
280-
expectedSysctls := map[string]string{
281-
"net.core.somaxconn": "1024",
282-
}
283-
assert.DeepEqual(t, expectedSysctls, inspect.HostConfig.Sysctls)
284-
expectedDevices := []string{"/dev/null:/dev/null"}
285-
assert.DeepEqual(t, expectedDevices, inspect.HostConfig.Devices)
286275
}
287276

288277
func TestContainerInspectHostConfigDefaults(t *testing.T) {
@@ -295,22 +284,23 @@ func TestContainerInspectHostConfigDefaults(t *testing.T) {
295284
base.Cmd("run", "-d", "--name", testContainer, testutil.AlpineImage, "sleep", "infinity").AssertOK()
296285

297286
inspect := base.InspectContainer(testContainer)
287+
t.Logf("HostConfig in TestContainerInspectHostConfigDefaults: %+v", inspect.HostConfig)
298288
assert.Equal(t, "", inspect.HostConfig.CPUSetCPUs)
299289
assert.Equal(t, "", inspect.HostConfig.CPUSetMems)
300290
assert.Equal(t, uint16(0), inspect.HostConfig.BlkioWeight)
301291
assert.Equal(t, uint64(0), inspect.HostConfig.CPUShares)
302292
assert.Equal(t, int64(0), inspect.HostConfig.CPUQuota)
303293
assert.Equal(t, 0, len(inspect.HostConfig.GroupAdd))
304294
assert.Equal(t, 0, len(inspect.HostConfig.ExtraHosts))
305-
assert.Equal(t, "", inspect.HostConfig.IpcMode)
306-
assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Driver)
295+
assert.Equal(t, "private", inspect.HostConfig.IpcMode)
296+
assert.Equal(t, "", inspect.HostConfig.LogConfig.Driver)
307297
assert.Equal(t, int64(0), inspect.HostConfig.Memory)
308298
assert.Equal(t, int64(0), inspect.HostConfig.MemorySwap)
309299
assert.Equal(t, bool(false), inspect.HostConfig.OomKillDisable)
310-
assert.Equal(t, false, inspect.HostConfig.ReadonlyRootfs)
300+
assert.Equal(t, bool(false), inspect.HostConfig.ReadonlyRootfs)
311301
assert.Equal(t, "", inspect.HostConfig.UTSMode)
312302
assert.Equal(t, int64(67108864), inspect.HostConfig.ShmSize)
313-
assert.Equal(t, "io.containerd.runc.v2", inspect.HostConfig.Runtime)
303+
assert.Equal(t, "runc", inspect.HostConfig.Runtime)
314304
assert.Equal(t, 0, len(inspect.HostConfig.Sysctls))
315305
assert.Equal(t, 0, len(inspect.HostConfig.Devices))
316306
}
@@ -364,23 +354,24 @@ func TestContainerInspectHostConfigDNSDefaults(t *testing.T) {
364354
}
365355

366356
func TestContainerInspectHostConfigPID(t *testing.T) {
367-
testContainer1 := testutil.Identifier(t)
368-
testContainer2 := testutil.Identifier(t)
357+
testContainer1 := testutil.Identifier(t) + "-container1"
358+
testContainer2 := testutil.Identifier(t) + "-container2"
369359

370360
base := testutil.NewBase(t)
371361
defer base.Cmd("rm", "-f", testContainer1, testContainer2).Run()
372362

373363
// Run the first container
374364
base.Cmd("run", "-d", "--name", testContainer1, testutil.AlpineImage, "sleep", "infinity").AssertOK()
375365

376-
// Run a container with PID namespace options
366+
container1_ID := strings.TrimSpace(base.Cmd("inspect", "-f", "{{.Id}}", testContainer1).Out())
367+
377368
base.Cmd("run", "-d", "--name", testContainer2,
378369
"--pid", fmt.Sprintf("container:%s", testContainer1),
379370
testutil.AlpineImage, "sleep", "infinity").AssertOK()
380371

381372
inspect := base.InspectContainer(testContainer2)
382373

383-
assert.Equal(t, fmt.Sprintf("container:%s", testContainer1), inspect.HostConfig.PidMode)
374+
assert.Equal(t, fmt.Sprintf("container:%s", container1_ID), inspect.HostConfig.PidMode)
384375

385376
}
386377

@@ -390,11 +381,31 @@ func TestContainerInspectHostConfigPIDDefaults(t *testing.T) {
390381
base := testutil.NewBase(t)
391382
defer base.Cmd("rm", "-f", testContainer).Run()
392383

393-
// Run a container without specifying PID options
394384
base.Cmd("run", "-d", "--name", testContainer, testutil.AlpineImage, "sleep", "infinity").AssertOK()
395385

396386
inspect := base.InspectContainer(testContainer)
397387

398-
// Check that PID mode is empty (private) by default
399388
assert.Equal(t, "", inspect.HostConfig.PidMode)
400389
}
390+
391+
func TestContainerInspectDevices(t *testing.T) {
392+
testContainer := testutil.Identifier(t)
393+
394+
base := testutil.NewBase(t)
395+
defer base.Cmd("rm", "-f", testContainer).Run()
396+
397+
base.Cmd("run", "-d", "--name", testContainer,
398+
"--device", "/dev/null:/dev/null",
399+
testutil.AlpineImage, "sleep", "infinity").AssertOK()
400+
401+
inspect := base.InspectContainer(testContainer)
402+
403+
expectedDevices := []dockercompat.DeviceMapping{
404+
{
405+
PathOnHost: "/dev/null",
406+
PathInContainer: "/dev/null",
407+
CgroupPermissions: "rwm",
408+
},
409+
}
410+
assert.DeepEqual(t, expectedDevices, inspect.HostConfig.Devices)
411+
}

pkg/cmd/container/create.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa
224224
}
225225
internalLabels.logURI = logConfig.LogURI
226226
internalLabels.logConfig = logConfig
227+
if logConfig.Driver == "" && logConfig.Address == options.GOptions.Address {
228+
internalLabels.logConfig.Driver = "json-file"
229+
}
227230

228231
restartOpts, err := generateRestartOpts(ctx, client, options.Restart, logConfig.LogURI, options.InRun)
229232
if err != nil {
@@ -770,7 +773,18 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO
770773
}
771774

772775
if len(internalLabels.deviceMapping) > 0 {
773-
hostConfigLabel.DeviceMapping = internalLabels.deviceMapping
776+
for i := range internalLabels.deviceMapping {
777+
var deviceMapping dockercompat.DeviceMapping
778+
device := internalLabels.deviceMapping[i]
779+
devPath, conPath, mode, err := ParseDevice(device)
780+
if err != nil {
781+
return nil, err
782+
}
783+
deviceMapping.CgroupPermissions = mode
784+
deviceMapping.PathInContainer = conPath
785+
deviceMapping.PathOnHost = devPath
786+
hostConfigLabel.Devices = append(hostConfigLabel.Devices, deviceMapping)
787+
}
774788
}
775789

776790
hostConfigJSON, err := json.Marshal(hostConfigLabel)

pkg/inspecttypes/dockercompat/dockercompat.go

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ type ImageMetadata struct {
9696
LastTagTime time.Time `json:",omitempty"`
9797
}
9898

99-
type loggerLogConfig struct {
99+
type LoggerLogConfig struct {
100100
Driver string `json:"driver"`
101101
Opts map[string]string `json:"opts,omitempty"`
102102
LogURI string `json:"-"`
@@ -140,7 +140,7 @@ type Container struct {
140140
type HostConfig struct {
141141
ExtraHosts []string // List of extra hosts
142142
PortBindings nat.PortMap // Port mapping between the exposed port (container) and the host
143-
LogConfig loggerLogConfig // Configuration of the logs for this container
143+
LogConfig LoggerLogConfig // Configuration of the logs for this container
144144
BlkioWeight uint16 // Block IO weight (relative weight vs. other containers)
145145
CPUSetMems string `json:"CpusetMems"` // CpusetMems 0-2, 0,1
146146
CPUSetCPUs string `json:"CpusetCpus"` // CpusetCpus 0-2, 0,1
@@ -162,9 +162,9 @@ type HostConfig struct {
162162
ShmSize int64 // Size of /dev/shm in bytes. The size must be greater than 0.
163163
Sysctls map[string]string // List of Namespaced sysctls used for the container
164164
Runtime string // Runtime to use with this container
165-
Devices []string // List of devices to map inside the container
165+
Devices []DeviceMapping // List of devices to map inside the container
166166
PidMode string // PID namespace to use for the container
167-
Tmpfs []MountPoint `json:",omitempty"` // List of tmpfs (mounts) used for the container
167+
Tmpfs map[string]string `json:"Tmpfs,omitempty"` // List of tmpfs (mounts) used for the container
168168
}
169169

170170
// From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L416-L427
@@ -239,9 +239,15 @@ type DNSSettings struct {
239239
}
240240

241241
type HostConfigLabel struct {
242-
BlkioWeight uint16
243-
CidFile string
244-
DeviceMapping []string
242+
BlkioWeight uint16
243+
CidFile string
244+
Devices []DeviceMapping
245+
}
246+
247+
type DeviceMapping struct {
248+
PathOnHost string
249+
PathInContainer string
250+
CgroupPermissions string
245251
}
246252

247253
type CPUSettings struct {
@@ -335,19 +341,18 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
335341
}
336342
}
337343

338-
var tmpfsMounts []MountPoint
339-
340344
if nerdctlMounts := n.Labels[labels.Mounts]; nerdctlMounts != "" {
341345
mounts, err := parseMounts(nerdctlMounts)
342346
if err != nil {
343347
return nil, err
344348
}
345349
c.Mounts = mounts
346-
if len(mounts) > 0 {
347-
tmpfsMounts = filterTmpfsMounts(mounts)
350+
for _, mount := range mounts {
351+
if mount.Type == "tmpfs" {
352+
c.HostConfig.Tmpfs[mount.Destination] = mount.Mode
353+
}
348354
}
349355
}
350-
c.HostConfig.Tmpfs = tmpfsMounts
351356

352357
if nedctlExtraHosts := n.Labels[labels.ExtraHosts]; nedctlExtraHosts != "" {
353358
c.HostConfig.ExtraHosts = parseExtraHosts(nedctlExtraHosts)
@@ -357,7 +362,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
357362
c.HostConfig.LogConfig.LogURI = nerdctlLoguri
358363
}
359364
if logConfigJSON, ok := n.Labels[labels.LogConfig]; ok {
360-
var logConfig loggerLogConfig
365+
var logConfig LoggerLogConfig
361366
err := json.Unmarshal([]byte(logConfigJSON), &logConfig)
362367
if err != nil {
363368
return nil, fmt.Errorf("failed to unmarshal log config: %v", err)
@@ -367,7 +372,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
367372
c.HostConfig.LogConfig = logConfig
368373
} else {
369374
// If LogConfig label is not present, set default values
370-
c.HostConfig.LogConfig = loggerLogConfig{
375+
c.HostConfig.LogConfig = LoggerLogConfig{
371376
Driver: "json-file",
372377
Opts: make(map[string]string),
373378
}
@@ -486,7 +491,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
486491
}
487492
c.Config.Hostname = hostname
488493

489-
c.HostConfig.Devices = hostConfigLabel.DeviceMapping
494+
c.HostConfig.Devices = hostConfigLabel.Devices
490495

491496
var pidMode string
492497
if n.Labels[labels.PIDContainer] != "" {
@@ -563,18 +568,6 @@ func mountsFromNative(spMounts []specs.Mount) []MountPoint {
563568
return mountpoints
564569
}
565570

566-
// filterTmpfsMounts filters the tmpfs mounts
567-
func filterTmpfsMounts(spMounts []MountPoint) []MountPoint {
568-
mountpoints := make([]MountPoint, 0, len(spMounts))
569-
for _, m := range spMounts {
570-
if m.Type == "tmpfs" {
571-
mountpoints = append(mountpoints, m)
572-
}
573-
}
574-
575-
return mountpoints
576-
}
577-
578571
func statusFromNative(x containerd.Status, labels map[string]string) string {
579572
switch s := x.Status; s {
580573
case containerd.Stopped:

pkg/inspecttypes/dockercompat/dockercompat_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,11 @@ func TestContainerFromNative(t *testing.T) {
7878
HostConfig: &HostConfig{
7979
PortBindings: nat.PortMap{},
8080
GroupAdd: []string{},
81-
LogConfig: loggerLogConfig{
81+
LogConfig: LoggerLogConfig{
8282
Driver: "json-file",
8383
Opts: map[string]string{},
8484
},
8585
UTSMode: "host",
86-
Tmpfs: []MountPoint{},
8786
},
8887
Mounts: []MountPoint{
8988
{
@@ -163,7 +162,7 @@ func TestContainerFromNative(t *testing.T) {
163162
HostConfig: &HostConfig{
164163
PortBindings: nat.PortMap{},
165164
GroupAdd: []string{},
166-
LogConfig: loggerLogConfig{
165+
LogConfig: LoggerLogConfig{
167166
Driver: "json-file",
168167
Opts: map[string]string{},
169168
},
@@ -244,7 +243,7 @@ func TestContainerFromNative(t *testing.T) {
244243
HostConfig: &HostConfig{
245244
PortBindings: nat.PortMap{},
246245
GroupAdd: []string{},
247-
LogConfig: loggerLogConfig{
246+
LogConfig: LoggerLogConfig{
248247
Driver: "json-file",
249248
Opts: map[string]string{},
250249
},

0 commit comments

Comments
 (0)