Skip to content

Commit 6391b95

Browse files
committed
chore: add device to inspect response
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent 0d14fb0 commit 6391b95

File tree

7 files changed

+57
-27
lines changed

7 files changed

+57
-27
lines changed

cmd/nerdctl/container/container_inspect_linux_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ func TestContainerInspectHostConfig(t *testing.T) {
255255
"--shm-size", "256m",
256256
"--runtime", "io.containerd.runtime.v1.linux",
257257
"--sysctl", "net.core.somaxconn=1024",
258+
"--device", "/dev/zero:/dev/null",
258259
testutil.AlpineImage, "sleep", "infinity").AssertOK()
259260

260261
inspect := base.InspectContainer(testContainer)
@@ -268,8 +269,7 @@ func TestContainerInspectHostConfig(t *testing.T) {
268269
expectedExtraHosts := []string{"host1:10.0.0.1", "host2:10.0.0.2"}
269270
assert.DeepEqual(t, expectedExtraHosts, inspect.HostConfig.ExtraHosts)
270271
assert.Equal(t, "host", inspect.HostConfig.IpcMode)
271-
assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Type)
272-
assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Config.Driver)
272+
assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Driver)
273273
assert.Equal(t, int64(536870912), inspect.HostConfig.Memory)
274274
assert.Equal(t, int64(1073741824), inspect.HostConfig.MemorySwap)
275275
assert.Equal(t, bool(true), inspect.HostConfig.OomKillDisable)
@@ -281,6 +281,8 @@ func TestContainerInspectHostConfig(t *testing.T) {
281281
"net.core.somaxconn": "1024",
282282
}
283283
assert.DeepEqual(t, expectedSysctls, inspect.HostConfig.Sysctls)
284+
expectedDevices := []string{"/dev/null:/dev/null"}
285+
assert.DeepEqual(t, expectedDevices, inspect.HostConfig.Devices)
284286
}
285287

286288
func TestContainerInspectHostConfigDefaults(t *testing.T) {
@@ -301,8 +303,7 @@ func TestContainerInspectHostConfigDefaults(t *testing.T) {
301303
assert.Equal(t, 0, len(inspect.HostConfig.GroupAdd))
302304
assert.Equal(t, 0, len(inspect.HostConfig.ExtraHosts))
303305
assert.Equal(t, "", inspect.HostConfig.IpcMode)
304-
assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Type)
305-
assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Config.Driver)
306+
assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Driver)
306307
assert.Equal(t, int64(0), inspect.HostConfig.Memory)
307308
assert.Equal(t, int64(0), inspect.HostConfig.MemorySwap)
308309
assert.Equal(t, bool(false), inspect.HostConfig.OomKillDisable)
@@ -311,6 +312,7 @@ func TestContainerInspectHostConfigDefaults(t *testing.T) {
311312
assert.Equal(t, int64(67108864), inspect.HostConfig.ShmSize)
312313
assert.Equal(t, "io.containerd.runc.v2", inspect.HostConfig.Runtime)
313314
assert.Equal(t, 0, len(inspect.HostConfig.Sysctls))
315+
assert.Equal(t, 0, len(inspect.HostConfig.Devices))
314316
}
315317

316318
func TestContainerInspectHostConfigDNS(t *testing.T) {

pkg/cmd/container/create.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,9 @@ type internalLabels struct {
659659

660660
// label to check if --group-add is set
661661
groupAdd []string
662+
663+
// label for device mapping set by the --device flag
664+
deviceMapping []string
662665
}
663666

664667
// WithInternalLabels sets the internal labels for a container.
@@ -759,7 +762,7 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO
759762
if err != nil {
760763
return nil, err
761764
}
762-
m[labels.DnsServer] = string(dnsServersJSON)
765+
m[labels.DNSServer] = string(dnsServersJSON)
763766
}
764767

765768
if len(internalLabels.dnsSearchDomains) > 0 {
@@ -778,6 +781,14 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO
778781
m[labels.DNSResolvConfOptions] = string(dnsResolvConfOptionsJSON)
779782
}
780783

784+
if len(internalLabels.deviceMapping) > 0 {
785+
devicesJSON, err := json.Marshal(internalLabels.deviceMapping)
786+
if err != nil {
787+
return nil, err
788+
}
789+
m[labels.DeviceMapping] = string(devicesJSON)
790+
}
791+
781792
return containerd.WithAdditionalContainerLabels(m), nil
782793
}
783794

pkg/cmd/container/run_cgroup_linux.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type customMemoryOptions struct {
4141
disableOOMKiller *bool
4242
}
4343

44-
func generateCgroupOpts(id string, options types.ContainerCreateOptions) ([]oci.SpecOpts, error) {
44+
func generateCgroupOpts(id string, options types.ContainerCreateOptions, internalLabels *internalLabels) ([]oci.SpecOpts, error) {
4545
if options.KernelMemory != "" {
4646
log.L.Warnf("The --kernel-memory flag is no longer supported. This flag is a noop.")
4747
}
@@ -206,6 +206,7 @@ func generateCgroupOpts(id string, options types.ContainerCreateOptions) ([]oci.
206206
return nil, fmt.Errorf("failed to parse device %q: %w", f, err)
207207
}
208208
opts = append(opts, oci.WithDevices(devPath, conPath, mode))
209+
internalLabels.deviceMapping = append(internalLabels.deviceMapping, f)
209210
}
210211

211212
return opts, nil

pkg/cmd/container/run_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func setPlatformOptions(ctx context.Context, client *containerd.Client, id, uts
5656
{Type: "cgroup", Source: "cgroup", Destination: "/sys/fs/cgroup", Options: []string{"ro", "nosuid", "noexec", "nodev"}},
5757
}))
5858

59-
cgOpts, err := generateCgroupOpts(id, options)
59+
cgOpts, err := generateCgroupOpts(id, options, internalLabels)
6060
if err != nil {
6161
return nil, err
6262
}

pkg/inspecttypes/dockercompat/dockercompat.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ 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
165166
}
166167

167168
// From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L416-L427
@@ -439,7 +440,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
439440
c.HostConfig.Memory = memorySettings.Limit
440441
c.HostConfig.MemorySwap = memorySettings.Swap
441442

442-
dnsSettings, err := getDnsFromNative(n.Labels)
443+
dnsSettings, err := getDNSFromNative(n.Labels)
443444
if err != nil {
444445
return nil, fmt.Errorf("failed to Decode dns Settings: %v", err)
445446
}
@@ -482,6 +483,12 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
482483
c.Config.Domainname = n.Labels[labels.Domainname]
483484
}
484485

486+
c.HostConfig.Devices = []string{}
487+
if nedctlDeviceMapping := n.Labels[labels.DeviceMapping]; nedctlDeviceMapping != "" {
488+
devices, _ := parseDeviceMapping(nedctlDeviceMapping)
489+
c.HostConfig.Devices = devices
490+
}
491+
485492
return c, nil
486493
}
487494

@@ -731,10 +738,10 @@ func getMemorySettingsFromNative(sp *specs.Spec) (*MemorySetting, error) {
731738
return res, nil
732739
}
733740

734-
func getDnsFromNative(Labels map[string]string) (*DNSSettings, error) {
741+
func getDNSFromNative(Labels map[string]string) (*DNSSettings, error) {
735742
res := &DNSSettings{}
736743

737-
if dnsServers := Labels[labels.DnsServer]; dnsServers != "" {
744+
if dnsServers := Labels[labels.DNSServer]; dnsServers != "" {
738745
if err := json.Unmarshal([]byte(dnsServers), &res.DNSServers); err != nil {
739746
return nil, fmt.Errorf("failed to parse DNS servers: %v", err)
740747
}
@@ -777,7 +784,7 @@ func getUtsModeFromNative(sp *specs.Spec) (string, error) {
777784
func getShmSizeFromNative(sp *specs.Spec) (int64, error) {
778785
var res int64
779786

780-
if sp.Mounts != nil && len(sp.Mounts) > 0 {
787+
if len(sp.Mounts) > 0 {
781788
for _, mount := range sp.Mounts {
782789
if mount.Destination == "/dev/shm" {
783790
for _, option := range mount.Options {
@@ -804,6 +811,15 @@ func getSysctlFromNative(sp *specs.Spec) (map[string]string, error) {
804811
return res, nil
805812
}
806813

814+
func parseDeviceMapping(deviceMappingJSON string) ([]string, error) {
815+
var devices []string
816+
err := json.Unmarshal([]byte(deviceMappingJSON), &devices)
817+
if err != nil {
818+
return nil, fmt.Errorf("failed to parse device mapping: %v", err)
819+
}
820+
return devices, nil
821+
}
822+
807823
type IPAMConfig struct {
808824
Subnet string `json:"Subnet,omitempty"`
809825
Gateway string `json:"Gateway,omitempty"`

pkg/inspecttypes/dockercompat/dockercompat_test.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,12 @@ func TestContainerFromNative(t *testing.T) {
7878
HostConfig: &HostConfig{
7979
PortBindings: nat.PortMap{},
8080
GroupAdd: []string{},
81-
LogConfig: LogConfig{
82-
Config: loggerLogConfig{
83-
Driver: "json-file",
84-
Opts: map[string]string{},
85-
},
81+
LogConfig: loggerLogConfig{
82+
Driver: "json-file",
83+
Opts: map[string]string{},
8684
},
8785
UTSMode: "host",
86+
Devices: []string{},
8887
},
8988
Mounts: []MountPoint{
9089
{
@@ -164,13 +163,12 @@ func TestContainerFromNative(t *testing.T) {
164163
HostConfig: &HostConfig{
165164
PortBindings: nat.PortMap{},
166165
GroupAdd: []string{},
167-
LogConfig: LogConfig{
168-
Config: loggerLogConfig{
169-
Driver: "json-file",
170-
Opts: map[string]string{},
171-
},
166+
LogConfig: loggerLogConfig{
167+
Driver: "json-file",
168+
Opts: map[string]string{},
172169
},
173170
UTSMode: "host",
171+
Devices: []string{},
174172
},
175173
Mounts: []MountPoint{
176174
{
@@ -247,13 +245,12 @@ func TestContainerFromNative(t *testing.T) {
247245
HostConfig: &HostConfig{
248246
PortBindings: nat.PortMap{},
249247
GroupAdd: []string{},
250-
LogConfig: LogConfig{
251-
Config: loggerLogConfig{
252-
Driver: "json-file",
253-
Opts: map[string]string{},
254-
},
248+
LogConfig: loggerLogConfig{
249+
Driver: "json-file",
250+
Opts: map[string]string{},
255251
},
256252
UTSMode: "host",
253+
Devices: []string{},
257254
},
258255
Mounts: []MountPoint{
259256
{

pkg/labels/labels.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,14 @@ const (
117117
CIdFile = Prefix + "cid-file"
118118

119119
// Custom DNS lookup servers.
120-
DnsServer = Prefix + "dns"
120+
DNSServer = Prefix + "dns"
121121

122122
// DNSResolvConfOptions set DNS options
123123
DNSResolvConfOptions = Prefix + "dns-options"
124124

125125
// DNSSearchDomains set custom DNS search domains
126126
DNSSearchDomains = Prefix + "dns-search"
127+
128+
//DeviceMapping specifies mapping host device to the container
129+
DeviceMapping = Prefix + "devices"
127130
)

0 commit comments

Comments
 (0)