Skip to content

Commit

Permalink
chore: fix compatability errors
Browse files Browse the repository at this point in the history
Signed-off-by: Arjun Raja Yogidas <[email protected]>
  • Loading branch information
coderbirju committed Jan 30, 2025
1 parent 64ad424 commit 7052b62
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 87 deletions.
118 changes: 90 additions & 28 deletions cmd/nerdctl/container/container_inspect_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package container

import (
"fmt"
"os"
"strings"
"testing"

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

inspect := base.InspectContainer(testContainer)

// convert array to map to get by key of Destination
actual := make(map[string]dockercompat.MountPoint)
for i := range inspect.Mounts {
actual[inspect.Mounts[i].Destination] = inspect.Mounts[i]
}

t.Logf("actual in TestContainerInspectContainsMounts: %+v", actual)
const localDriver = "local"

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

inspect := base.InspectContainer(testContainer)
Expand All @@ -265,24 +263,16 @@ func TestContainerInspectHostConfig(t *testing.T) {
assert.Equal(t, uint16(500), inspect.HostConfig.BlkioWeight)
assert.Equal(t, uint64(1024), inspect.HostConfig.CPUShares)
assert.Equal(t, int64(100000), inspect.HostConfig.CPUQuota)
assert.DeepEqual(t, []string{"1000", "2000"}, inspect.HostConfig.GroupAdd)
assert.Assert(t, contains(inspect.HostConfig.GroupAdd, "1000"), "Expected '1000' to be in GroupAdd")
assert.Assert(t, contains(inspect.HostConfig.GroupAdd, "2000"), "Expected '2000' to be in GroupAdd")
expectedExtraHosts := []string{"host1:10.0.0.1", "host2:10.0.0.2"}
assert.DeepEqual(t, expectedExtraHosts, inspect.HostConfig.ExtraHosts)
assert.Equal(t, "host", inspect.HostConfig.IpcMode)
assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Driver)
assert.Equal(t, int64(536870912), inspect.HostConfig.Memory)
assert.Equal(t, int64(1073741824), inspect.HostConfig.MemorySwap)
assert.Equal(t, bool(true), inspect.HostConfig.OomKillDisable)
assert.Equal(t, true, inspect.HostConfig.ReadonlyRootfs)
assert.Equal(t, "host", inspect.HostConfig.UTSMode)
assert.Equal(t, int64(268435456), inspect.HostConfig.ShmSize)
assert.Equal(t, "io.containerd.runtime.v1.linux", inspect.HostConfig.Runtime)
expectedSysctls := map[string]string{
"net.core.somaxconn": "1024",
}
assert.DeepEqual(t, expectedSysctls, inspect.HostConfig.Sysctls)
expectedDevices := []string{"/dev/null:/dev/null"}
assert.DeepEqual(t, expectedDevices, inspect.HostConfig.Devices)
}

func TestContainerInspectHostConfigDefaults(t *testing.T) {
Expand All @@ -291,26 +281,41 @@ func TestContainerInspectHostConfigDefaults(t *testing.T) {
base := testutil.NewBase(t)
defer base.Cmd("rm", "-f", testContainer).Run()

var hc hostConfigValues

if testutil.GetTarget() == testutil.Docker {
hc.Driver = ""
hc.GroupAddSize = 0
hc.ShmSize = int64(67108864)
hc.Runtime = "runc"
} else {
hc.GroupAddSize = 10
hc.Driver = "json-file"
hc.ShmSize = int64(0)
hc.Runtime = "io.containerd.runc.v2"
}

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

inspect := base.InspectContainer(testContainer)
t.Logf("HostConfig in TestContainerInspectHostConfigDefaults: %+v", inspect.HostConfig)
assert.Equal(t, "", inspect.HostConfig.CPUSetCPUs)
assert.Equal(t, "", inspect.HostConfig.CPUSetMems)
assert.Equal(t, uint16(0), inspect.HostConfig.BlkioWeight)
assert.Equal(t, uint64(0), inspect.HostConfig.CPUShares)
assert.Equal(t, int64(0), inspect.HostConfig.CPUQuota)
assert.Equal(t, 0, len(inspect.HostConfig.GroupAdd))
assert.Equal(t, hc.GroupAddSize, len(inspect.HostConfig.GroupAdd))
assert.Equal(t, 0, len(inspect.HostConfig.ExtraHosts))
assert.Equal(t, "", inspect.HostConfig.IpcMode)
assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Driver)
assert.Equal(t, "private", inspect.HostConfig.IpcMode)
assert.Equal(t, hc.Driver, inspect.HostConfig.LogConfig.Driver)
assert.Equal(t, int64(0), inspect.HostConfig.Memory)
assert.Equal(t, int64(0), inspect.HostConfig.MemorySwap)
assert.Equal(t, bool(false), inspect.HostConfig.OomKillDisable)
assert.Equal(t, false, inspect.HostConfig.ReadonlyRootfs)
assert.Equal(t, bool(false), inspect.HostConfig.ReadonlyRootfs)
assert.Equal(t, "", inspect.HostConfig.UTSMode)
assert.Equal(t, int64(67108864), inspect.HostConfig.ShmSize)
assert.Equal(t, "io.containerd.runc.v2", inspect.HostConfig.Runtime)
assert.Equal(t, hc.ShmSize, inspect.HostConfig.ShmSize)
assert.Equal(t, hc.Runtime, inspect.HostConfig.Runtime)
assert.Equal(t, 0, len(inspect.HostConfig.Sysctls))
assert.Equal(t, 0, len(inspect.HostConfig.Devices))
}
Expand Down Expand Up @@ -364,23 +369,32 @@ func TestContainerInspectHostConfigDNSDefaults(t *testing.T) {
}

func TestContainerInspectHostConfigPID(t *testing.T) {
testContainer1 := testutil.Identifier(t)
testContainer2 := testutil.Identifier(t)
testContainer1 := testutil.Identifier(t) + "-container1"
testContainer2 := testutil.Identifier(t) + "-container2"

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

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

// Run a container with PID namespace options
containerID1 := strings.TrimSpace(base.Cmd("inspect", "-f", "{{.Id}}", testContainer1).Out())

var hc hostConfigValues

if testutil.GetTarget() == testutil.Docker {
hc.PidMode = "container:" + containerID1
} else {
hc.PidMode = containerID1
}

base.Cmd("run", "-d", "--name", testContainer2,
"--pid", fmt.Sprintf("container:%s", testContainer1),
testutil.AlpineImage, "sleep", "infinity").AssertOK()

inspect := base.InspectContainer(testContainer2)

assert.Equal(t, fmt.Sprintf("container:%s", testContainer1), inspect.HostConfig.PidMode)
assert.Equal(t, hc.PidMode, inspect.HostConfig.PidMode)

}

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

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

inspect := base.InspectContainer(testContainer)

// Check that PID mode is empty (private) by default
assert.Equal(t, "", inspect.HostConfig.PidMode)
}

func TestContainerInspectDevices(t *testing.T) {
testContainer := testutil.Identifier(t)

base := testutil.NewBase(t)
defer base.Cmd("rm", "-f", testContainer).Run()

// Create a temporary directory
dir, err := os.MkdirTemp(t.TempDir(), "device-dir")
if err != nil {
t.Fatal(err)
}

if testutil.GetTarget() == testutil.Docker {
dir = "/dev/zero"
}

// Run the container with the directory mapped as a device
base.Cmd("run", "-d", "--name", testContainer,
"--device", dir+":/dev/xvda",
testutil.AlpineImage, "sleep", "infinity").AssertOK()

inspect := base.InspectContainer(testContainer)

expectedDevices := []dockercompat.DeviceMapping{
{
PathOnHost: dir,
PathInContainer: "/dev/xvda",
CgroupPermissions: "rwm",
},
}
assert.DeepEqual(t, expectedDevices, inspect.HostConfig.Devices)
}

func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}

type hostConfigValues struct {
Driver string
ShmSize int64
PidMode string
GroupAddSize int
Runtime string
}
7 changes: 5 additions & 2 deletions pkg/cmd/container/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa
}
internalLabels.logURI = logConfig.LogURI
internalLabels.logConfig = logConfig
if logConfig.Driver == "" && logConfig.Address == options.GOptions.Address {
internalLabels.logConfig.Driver = "json-file"
}

restartOpts, err := generateRestartOpts(ctx, client, options.Restart, logConfig.LogURI, options.InRun)
if err != nil {
Expand Down Expand Up @@ -660,7 +663,7 @@ type internalLabels struct {
groupAdd []string

// label for device mapping set by the --device flag
deviceMapping []string
deviceMapping []dockercompat.DeviceMapping
}

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

if len(internalLabels.deviceMapping) > 0 {
hostConfigLabel.DeviceMapping = internalLabels.deviceMapping
hostConfigLabel.Devices = append(hostConfigLabel.Devices, internalLabels.deviceMapping...)
}

hostConfigJSON, err := json.Marshal(hostConfigLabel)
Expand Down
7 changes: 6 additions & 1 deletion pkg/cmd/container/run_cgroup_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

"github.com/containerd/nerdctl/v2/pkg/api/types"
"github.com/containerd/nerdctl/v2/pkg/infoutil"
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat"
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
)

Expand Down Expand Up @@ -206,7 +207,11 @@ func generateCgroupOpts(id string, options types.ContainerCreateOptions, interna
return nil, fmt.Errorf("failed to parse device %q: %w", f, err)
}
opts = append(opts, oci.WithDevices(devPath, conPath, mode))
internalLabels.deviceMapping = append(internalLabels.deviceMapping, f)
var deviceMap dockercompat.DeviceMapping
deviceMap.PathOnHost = devPath
deviceMap.PathInContainer = conPath
deviceMap.CgroupPermissions = mode
internalLabels.deviceMapping = append(internalLabels.deviceMapping, deviceMap)
}

return opts, nil
Expand Down
Loading

0 comments on commit 7052b62

Please sign in to comment.