Skip to content

Commit

Permalink
chore: add device to inspect response
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 21, 2025
1 parent 42c33cc commit 8563239
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 6 deletions.
10 changes: 6 additions & 4 deletions cmd/nerdctl/container/container_inspect_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ func TestContainerInspectHostConfig(t *testing.T) {
"--shm-size", "256m",
"--runtime", "io.containerd.runtime.v1.linux",
"--sysctl", "net.core.somaxconn=1024",
"--device", "/dev/zero:/dev/null",
testutil.AlpineImage, "sleep", "infinity").AssertOK()

inspect := base.InspectContainer(testContainer)
Expand All @@ -268,8 +269,7 @@ func TestContainerInspectHostConfig(t *testing.T) {
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.Type)
assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Config.Driver)
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)
Expand All @@ -281,6 +281,8 @@ func TestContainerInspectHostConfig(t *testing.T) {
"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 @@ -301,8 +303,7 @@ func TestContainerInspectHostConfigDefaults(t *testing.T) {
assert.Equal(t, 0, 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.Type)
assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Config.Driver)
assert.Equal(t, "json-file", 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)
Expand All @@ -311,6 +312,7 @@ func TestContainerInspectHostConfigDefaults(t *testing.T) {
assert.Equal(t, int64(67108864), inspect.HostConfig.ShmSize)
assert.Equal(t, "io.containerd.runc.v2", inspect.HostConfig.Runtime)
assert.Equal(t, 0, len(inspect.HostConfig.Sysctls))
assert.Equal(t, 0, len(inspect.HostConfig.Devices))
}

func TestContainerInspectHostConfigDNS(t *testing.T) {
Expand Down
11 changes: 11 additions & 0 deletions pkg/cmd/container/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,9 @@ type internalLabels struct {

// label to check if --group-add is set
groupAdd []string

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

// WithInternalLabels sets the internal labels for a container.
Expand Down Expand Up @@ -776,6 +779,14 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO
m[labels.DNSResolvConfOptions] = string(dnsResolvConfOptionsJSON)
}

if len(internalLabels.deviceMapping) > 0 {
devicesJSON, err := json.Marshal(internalLabels.deviceMapping)
if err != nil {
return nil, err
}
m[labels.DeviceMapping] = string(devicesJSON)
}

return containerd.WithAdditionalContainerLabels(m), nil
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/container/run_cgroup_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type customMemoryOptions struct {
disableOOMKiller *bool
}

func generateCgroupOpts(id string, options types.ContainerCreateOptions) ([]oci.SpecOpts, error) {
func generateCgroupOpts(id string, options types.ContainerCreateOptions, internalLabels *internalLabels) ([]oci.SpecOpts, error) {
if options.KernelMemory != "" {
log.L.Warnf("The --kernel-memory flag is no longer supported. This flag is a noop.")
}
Expand Down Expand Up @@ -206,6 +206,7 @@ func generateCgroupOpts(id string, options types.ContainerCreateOptions) ([]oci.
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)
}

return opts, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/container/run_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func setPlatformOptions(ctx context.Context, client *containerd.Client, id, uts
{Type: "cgroup", Source: "cgroup", Destination: "/sys/fs/cgroup", Options: []string{"ro", "nosuid", "noexec", "nodev"}},
}))

cgOpts, err := generateCgroupOpts(id, options)
cgOpts, err := generateCgroupOpts(id, options, internalLabels)
if err != nil {
return nil, err
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/inspecttypes/dockercompat/dockercompat.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ type HostConfig struct {
ShmSize int64 // Size of /dev/shm in bytes. The size must be greater than 0.
Sysctls map[string]string // List of Namespaced sysctls used for the container
Runtime string // Runtime to use with this container
Devices []string // List of devices to map inside the container
}

// From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L416-L427
Expand Down Expand Up @@ -478,6 +479,12 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
}
c.Config.Hostname = hostname

c.HostConfig.Devices = []string{}
if nedctlDeviceMapping := n.Labels[labels.DeviceMapping]; nedctlDeviceMapping != "" {
devices, _ := parseDeviceMapping(nedctlDeviceMapping)
c.HostConfig.Devices = devices
}

return c, nil
}

Expand Down Expand Up @@ -798,6 +805,15 @@ func getSysctlFromNative(sp *specs.Spec) (map[string]string, error) {
return res, nil
}

func parseDeviceMapping(deviceMappingJSON string) ([]string, error) {
var devices []string
err := json.Unmarshal([]byte(deviceMappingJSON), &devices)
if err != nil {
return nil, fmt.Errorf("failed to parse device mapping: %v", err)
}
return devices, nil
}

type IPAMConfig struct {
Subnet string `json:"Subnet,omitempty"`
Gateway string `json:"Gateway,omitempty"`
Expand Down
3 changes: 3 additions & 0 deletions pkg/labels/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,7 @@ const (

// DNSSearchDomains set custom DNS search domains
DNSSearchDomains = Prefix + "dns-search"

//DeviceMapping specifies mapping host device to the container
DeviceMapping = Prefix + "devices"
)

0 comments on commit 8563239

Please sign in to comment.