Skip to content

Commit 7052b62

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

File tree

5 files changed

+158
-87
lines changed

5 files changed

+158
-87
lines changed

cmd/nerdctl/container/container_inspect_linux_test.go

Lines changed: 90 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package container
1818

1919
import (
2020
"fmt"
21+
"os"
2122
"strings"
2223
"testing"
2324

@@ -68,13 +69,12 @@ func TestContainerInspectContainsMounts(t *testing.T) {
6869
testutil.NginxAlpineImage).AssertOK()
6970

7071
inspect := base.InspectContainer(testContainer)
71-
7272
// convert array to map to get by key of Destination
7373
actual := make(map[string]dockercompat.MountPoint)
7474
for i := range inspect.Mounts {
7575
actual[inspect.Mounts[i].Destination] = inspect.Mounts[i]
7676
}
77-
77+
t.Logf("actual in TestContainerInspectContainsMounts: %+v", actual)
7878
const localDriver = "local"
7979

8080
expected := []struct {
@@ -249,13 +249,11 @@ func TestContainerInspectHostConfig(t *testing.T) {
249249
"--add-host", "host2:10.0.0.2",
250250
"--ipc", "host",
251251
"--memory", "512m",
252-
"--oom-kill-disable",
253252
"--read-only",
254-
"--uts", "host",
255253
"--shm-size", "256m",
256-
"--runtime", "io.containerd.runtime.v1.linux",
254+
"--uts", "host",
257255
"--sysctl", "net.core.somaxconn=1024",
258-
"--device", "/dev/null:/dev/null",
256+
"--runtime", "io.containerd.runc.v2",
259257
testutil.AlpineImage, "sleep", "infinity").AssertOK()
260258

261259
inspect := base.InspectContainer(testContainer)
@@ -265,24 +263,16 @@ func TestContainerInspectHostConfig(t *testing.T) {
265263
assert.Equal(t, uint16(500), inspect.HostConfig.BlkioWeight)
266264
assert.Equal(t, uint64(1024), inspect.HostConfig.CPUShares)
267265
assert.Equal(t, int64(100000), inspect.HostConfig.CPUQuota)
268-
assert.DeepEqual(t, []string{"1000", "2000"}, inspect.HostConfig.GroupAdd)
266+
assert.Assert(t, contains(inspect.HostConfig.GroupAdd, "1000"), "Expected '1000' to be in GroupAdd")
267+
assert.Assert(t, contains(inspect.HostConfig.GroupAdd, "2000"), "Expected '2000' to be in GroupAdd")
269268
expectedExtraHosts := []string{"host1:10.0.0.1", "host2:10.0.0.2"}
270269
assert.DeepEqual(t, expectedExtraHosts, inspect.HostConfig.ExtraHosts)
271270
assert.Equal(t, "host", inspect.HostConfig.IpcMode)
272-
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) {
@@ -291,26 +281,41 @@ func TestContainerInspectHostConfigDefaults(t *testing.T) {
291281
base := testutil.NewBase(t)
292282
defer base.Cmd("rm", "-f", testContainer).Run()
293283

284+
var hc hostConfigValues
285+
286+
if testutil.GetTarget() == testutil.Docker {
287+
hc.Driver = ""
288+
hc.GroupAddSize = 0
289+
hc.ShmSize = int64(67108864)
290+
hc.Runtime = "runc"
291+
} else {
292+
hc.GroupAddSize = 10
293+
hc.Driver = "json-file"
294+
hc.ShmSize = int64(0)
295+
hc.Runtime = "io.containerd.runc.v2"
296+
}
297+
294298
// Run a container without specifying HostConfig options
295299
base.Cmd("run", "-d", "--name", testContainer, testutil.AlpineImage, "sleep", "infinity").AssertOK()
296300

297301
inspect := base.InspectContainer(testContainer)
302+
t.Logf("HostConfig in TestContainerInspectHostConfigDefaults: %+v", inspect.HostConfig)
298303
assert.Equal(t, "", inspect.HostConfig.CPUSetCPUs)
299304
assert.Equal(t, "", inspect.HostConfig.CPUSetMems)
300305
assert.Equal(t, uint16(0), inspect.HostConfig.BlkioWeight)
301306
assert.Equal(t, uint64(0), inspect.HostConfig.CPUShares)
302307
assert.Equal(t, int64(0), inspect.HostConfig.CPUQuota)
303-
assert.Equal(t, 0, len(inspect.HostConfig.GroupAdd))
308+
assert.Equal(t, hc.GroupAddSize, len(inspect.HostConfig.GroupAdd))
304309
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)
310+
assert.Equal(t, "private", inspect.HostConfig.IpcMode)
311+
assert.Equal(t, hc.Driver, inspect.HostConfig.LogConfig.Driver)
307312
assert.Equal(t, int64(0), inspect.HostConfig.Memory)
308313
assert.Equal(t, int64(0), inspect.HostConfig.MemorySwap)
309314
assert.Equal(t, bool(false), inspect.HostConfig.OomKillDisable)
310-
assert.Equal(t, false, inspect.HostConfig.ReadonlyRootfs)
315+
assert.Equal(t, bool(false), inspect.HostConfig.ReadonlyRootfs)
311316
assert.Equal(t, "", inspect.HostConfig.UTSMode)
312-
assert.Equal(t, int64(67108864), inspect.HostConfig.ShmSize)
313-
assert.Equal(t, "io.containerd.runc.v2", inspect.HostConfig.Runtime)
317+
assert.Equal(t, hc.ShmSize, inspect.HostConfig.ShmSize)
318+
assert.Equal(t, hc.Runtime, inspect.HostConfig.Runtime)
314319
assert.Equal(t, 0, len(inspect.HostConfig.Sysctls))
315320
assert.Equal(t, 0, len(inspect.HostConfig.Devices))
316321
}
@@ -364,23 +369,32 @@ func TestContainerInspectHostConfigDNSDefaults(t *testing.T) {
364369
}
365370

366371
func TestContainerInspectHostConfigPID(t *testing.T) {
367-
testContainer1 := testutil.Identifier(t)
368-
testContainer2 := testutil.Identifier(t)
372+
testContainer1 := testutil.Identifier(t) + "-container1"
373+
testContainer2 := testutil.Identifier(t) + "-container2"
369374

370375
base := testutil.NewBase(t)
371376
defer base.Cmd("rm", "-f", testContainer1, testContainer2).Run()
372377

373378
// Run the first container
374379
base.Cmd("run", "-d", "--name", testContainer1, testutil.AlpineImage, "sleep", "infinity").AssertOK()
375380

376-
// Run a container with PID namespace options
381+
containerID1 := strings.TrimSpace(base.Cmd("inspect", "-f", "{{.Id}}", testContainer1).Out())
382+
383+
var hc hostConfigValues
384+
385+
if testutil.GetTarget() == testutil.Docker {
386+
hc.PidMode = "container:" + containerID1
387+
} else {
388+
hc.PidMode = containerID1
389+
}
390+
377391
base.Cmd("run", "-d", "--name", testContainer2,
378392
"--pid", fmt.Sprintf("container:%s", testContainer1),
379393
testutil.AlpineImage, "sleep", "infinity").AssertOK()
380394

381395
inspect := base.InspectContainer(testContainer2)
382396

383-
assert.Equal(t, fmt.Sprintf("container:%s", testContainer1), inspect.HostConfig.PidMode)
397+
assert.Equal(t, hc.PidMode, inspect.HostConfig.PidMode)
384398

385399
}
386400

@@ -390,11 +404,59 @@ func TestContainerInspectHostConfigPIDDefaults(t *testing.T) {
390404
base := testutil.NewBase(t)
391405
defer base.Cmd("rm", "-f", testContainer).Run()
392406

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

396409
inspect := base.InspectContainer(testContainer)
397410

398-
// Check that PID mode is empty (private) by default
399411
assert.Equal(t, "", inspect.HostConfig.PidMode)
400412
}
413+
414+
func TestContainerInspectDevices(t *testing.T) {
415+
testContainer := testutil.Identifier(t)
416+
417+
base := testutil.NewBase(t)
418+
defer base.Cmd("rm", "-f", testContainer).Run()
419+
420+
// Create a temporary directory
421+
dir, err := os.MkdirTemp(t.TempDir(), "device-dir")
422+
if err != nil {
423+
t.Fatal(err)
424+
}
425+
426+
if testutil.GetTarget() == testutil.Docker {
427+
dir = "/dev/zero"
428+
}
429+
430+
// Run the container with the directory mapped as a device
431+
base.Cmd("run", "-d", "--name", testContainer,
432+
"--device", dir+":/dev/xvda",
433+
testutil.AlpineImage, "sleep", "infinity").AssertOK()
434+
435+
inspect := base.InspectContainer(testContainer)
436+
437+
expectedDevices := []dockercompat.DeviceMapping{
438+
{
439+
PathOnHost: dir,
440+
PathInContainer: "/dev/xvda",
441+
CgroupPermissions: "rwm",
442+
},
443+
}
444+
assert.DeepEqual(t, expectedDevices, inspect.HostConfig.Devices)
445+
}
446+
447+
func contains(slice []string, item string) bool {
448+
for _, s := range slice {
449+
if s == item {
450+
return true
451+
}
452+
}
453+
return false
454+
}
455+
456+
type hostConfigValues struct {
457+
Driver string
458+
ShmSize int64
459+
PidMode string
460+
GroupAddSize int
461+
Runtime string
462+
}

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)