Skip to content

Commit 5d5246a

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

File tree

5 files changed

+113
-83
lines changed

5 files changed

+113
-83
lines changed

cmd/nerdctl/container/container_inspect_linux_test.go

Lines changed: 45 additions & 24 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)
@@ -265,24 +262,17 @@ func TestContainerInspectHostConfig(t *testing.T) {
265262
assert.Equal(t, uint16(500), inspect.HostConfig.BlkioWeight)
266263
assert.Equal(t, uint64(1024), inspect.HostConfig.CPUShares)
267264
assert.Equal(t, int64(100000), inspect.HostConfig.CPUQuota)
268-
assert.DeepEqual(t, []string{"1000", "2000"}, inspect.HostConfig.GroupAdd)
265+
assert.Assert(t, contains(inspect.HostConfig.GroupAdd, "1000"), "Expected '1000' to be in GroupAdd")
266+
assert.Assert(t, contains(inspect.HostConfig.GroupAdd, "2000"), "Expected '2000' to be in GroupAdd")
269267
expectedExtraHosts := []string{"host1:10.0.0.1", "host2:10.0.0.2"}
270268
assert.DeepEqual(t, expectedExtraHosts, inspect.HostConfig.ExtraHosts)
271269
assert.Equal(t, "host", inspect.HostConfig.IpcMode)
272270
assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Driver)
273271
assert.Equal(t, int64(536870912), inspect.HostConfig.Memory)
274272
assert.Equal(t, int64(1073741824), inspect.HostConfig.MemorySwap)
275-
assert.Equal(t, bool(true), inspect.HostConfig.OomKillDisable)
276273
assert.Equal(t, true, inspect.HostConfig.ReadonlyRootfs)
277274
assert.Equal(t, "host", inspect.HostConfig.UTSMode)
278275
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)
286276
}
287277

288278
func TestContainerInspectHostConfigDefaults(t *testing.T) {
@@ -295,19 +285,20 @@ func TestContainerInspectHostConfigDefaults(t *testing.T) {
295285
base.Cmd("run", "-d", "--name", testContainer, testutil.AlpineImage, "sleep", "infinity").AssertOK()
296286

297287
inspect := base.InspectContainer(testContainer)
288+
t.Logf("HostConfig in TestContainerInspectHostConfigDefaults: %+v", inspect.HostConfig)
298289
assert.Equal(t, "", inspect.HostConfig.CPUSetCPUs)
299290
assert.Equal(t, "", inspect.HostConfig.CPUSetMems)
300291
assert.Equal(t, uint16(0), inspect.HostConfig.BlkioWeight)
301292
assert.Equal(t, uint64(0), inspect.HostConfig.CPUShares)
302293
assert.Equal(t, int64(0), inspect.HostConfig.CPUQuota)
303-
assert.Equal(t, 0, len(inspect.HostConfig.GroupAdd))
294+
assert.Equal(t, 10, len(inspect.HostConfig.GroupAdd))
304295
assert.Equal(t, 0, len(inspect.HostConfig.ExtraHosts))
305-
assert.Equal(t, "", inspect.HostConfig.IpcMode)
296+
assert.Equal(t, "private", inspect.HostConfig.IpcMode)
306297
assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Driver)
307298
assert.Equal(t, int64(0), inspect.HostConfig.Memory)
308299
assert.Equal(t, int64(0), inspect.HostConfig.MemorySwap)
309300
assert.Equal(t, bool(false), inspect.HostConfig.OomKillDisable)
310-
assert.Equal(t, false, inspect.HostConfig.ReadonlyRootfs)
301+
assert.Equal(t, bool(false), inspect.HostConfig.ReadonlyRootfs)
311302
assert.Equal(t, "", inspect.HostConfig.UTSMode)
312303
assert.Equal(t, int64(67108864), inspect.HostConfig.ShmSize)
313304
assert.Equal(t, "io.containerd.runc.v2", inspect.HostConfig.Runtime)
@@ -364,23 +355,24 @@ func TestContainerInspectHostConfigDNSDefaults(t *testing.T) {
364355
}
365356

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

370361
base := testutil.NewBase(t)
371362
defer base.Cmd("rm", "-f", testContainer1, testContainer2).Run()
372363

373364
// Run the first container
374365
base.Cmd("run", "-d", "--name", testContainer1, testutil.AlpineImage, "sleep", "infinity").AssertOK()
375366

376-
// Run a container with PID namespace options
367+
containerID1 := strings.TrimSpace(base.Cmd("inspect", "-f", "{{.Id}}", testContainer1).Out())
368+
377369
base.Cmd("run", "-d", "--name", testContainer2,
378370
"--pid", fmt.Sprintf("container:%s", testContainer1),
379371
testutil.AlpineImage, "sleep", "infinity").AssertOK()
380372

381373
inspect := base.InspectContainer(testContainer2)
382374

383-
assert.Equal(t, fmt.Sprintf("container:%s", testContainer1), inspect.HostConfig.PidMode)
375+
assert.Equal(t, containerID1, inspect.HostConfig.PidMode)
384376

385377
}
386378

@@ -390,11 +382,40 @@ func TestContainerInspectHostConfigPIDDefaults(t *testing.T) {
390382
base := testutil.NewBase(t)
391383
defer base.Cmd("rm", "-f", testContainer).Run()
392384

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

396387
inspect := base.InspectContainer(testContainer)
397388

398-
// Check that PID mode is empty (private) by default
399389
assert.Equal(t, "", inspect.HostConfig.PidMode)
400390
}
391+
392+
func TestContainerInspectDevices(t *testing.T) {
393+
testContainer := testutil.Identifier(t)
394+
395+
base := testutil.NewBase(t)
396+
defer base.Cmd("rm", "-f", testContainer).Run()
397+
398+
base.Cmd("run", "-d", "--name", testContainer,
399+
"--device", "/dev/zero:/dev/xvda",
400+
testutil.AlpineImage, "sleep", "infinity").AssertOK()
401+
402+
inspect := base.InspectContainer(testContainer)
403+
404+
expectedDevices := []dockercompat.DeviceMapping{
405+
{
406+
PathOnHost: "/dev/zero",
407+
PathInContainer: "/dev/xvda",
408+
CgroupPermissions: "rwm",
409+
},
410+
}
411+
assert.DeepEqual(t, expectedDevices, inspect.HostConfig.Devices)
412+
}
413+
414+
func contains(slice []string, item string) bool {
415+
for _, s := range slice {
416+
if s == item {
417+
return true
418+
}
419+
}
420+
return false
421+
}

pkg/cmd/container/create.go

Lines changed: 5 additions & 2 deletions
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 {
@@ -660,7 +663,7 @@ type internalLabels struct {
660663
groupAdd []string
661664

662665
// label for device mapping set by the --device flag
663-
deviceMapping []string
666+
deviceMapping []dockercompat.DeviceMapping
664667
}
665668

666669
// WithInternalLabels sets the internal labels for a container.
@@ -770,7 +773,7 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO
770773
}
771774

772775
if len(internalLabels.deviceMapping) > 0 {
773-
hostConfigLabel.DeviceMapping = internalLabels.deviceMapping
776+
hostConfigLabel.Devices = append(hostConfigLabel.Devices, internalLabels.deviceMapping...)
774777
}
775778

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

pkg/cmd/container/run_cgroup_linux.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232

3333
"github.com/containerd/nerdctl/v2/pkg/api/types"
3434
"github.com/containerd/nerdctl/v2/pkg/infoutil"
35+
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat"
3536
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
3637
)
3738

@@ -206,7 +207,11 @@ func generateCgroupOpts(id string, options types.ContainerCreateOptions, interna
206207
return nil, fmt.Errorf("failed to parse device %q: %w", f, err)
207208
}
208209
opts = append(opts, oci.WithDevices(devPath, conPath, mode))
209-
internalLabels.deviceMapping = append(internalLabels.deviceMapping, f)
210+
var deviceMap dockercompat.DeviceMapping
211+
deviceMap.PathOnHost = devPath
212+
deviceMap.PathInContainer = conPath
213+
deviceMap.CgroupPermissions = mode
214+
internalLabels.deviceMapping = append(internalLabels.deviceMapping, deviceMap)
210215
}
211216

212217
return opts, nil

0 commit comments

Comments
 (0)