Skip to content

Commit 42a83c8

Browse files
committed
chore: add device to inspect response
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent 230b659 commit 42a83c8

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
@@ -658,6 +658,9 @@ type internalLabels struct {
658658

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

663666
// WithInternalLabels sets the internal labels for a container.
@@ -757,7 +760,7 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO
757760
if err != nil {
758761
return nil, err
759762
}
760-
m[labels.DnsServer] = string(dnsServersJSON)
763+
m[labels.DNSServer] = string(dnsServersJSON)
761764
}
762765

763766
if len(internalLabels.dnsSearchDomains) > 0 {
@@ -776,6 +779,14 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO
776779
m[labels.DNSResolvConfOptions] = string(dnsResolvConfOptionsJSON)
777780
}
778781

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

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
}
@@ -478,6 +479,12 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
478479
}
479480
c.Config.Hostname = hostname
480481

482+
c.HostConfig.Devices = []string{}
483+
if nedctlDeviceMapping := n.Labels[labels.DeviceMapping]; nedctlDeviceMapping != "" {
484+
devices, _ := parseDeviceMapping(nedctlDeviceMapping)
485+
c.HostConfig.Devices = devices
486+
}
487+
481488
return c, nil
482489
}
483490

@@ -727,10 +734,10 @@ func getMemorySettingsFromNative(sp *specs.Spec) (*MemorySetting, error) {
727734
return res, nil
728735
}
729736

730-
func getDnsFromNative(Labels map[string]string) (*DNSSettings, error) {
737+
func getDNSFromNative(Labels map[string]string) (*DNSSettings, error) {
731738
res := &DNSSettings{}
732739

733-
if dnsServers := Labels[labels.DnsServer]; dnsServers != "" {
740+
if dnsServers := Labels[labels.DNSServer]; dnsServers != "" {
734741
if err := json.Unmarshal([]byte(dnsServers), &res.DNSServers); err != nil {
735742
return nil, fmt.Errorf("failed to parse DNS servers: %v", err)
736743
}
@@ -773,7 +780,7 @@ func getUtsModeFromNative(sp *specs.Spec) (string, error) {
773780
func getShmSizeFromNative(sp *specs.Spec) (int64, error) {
774781
var res int64
775782

776-
if sp.Mounts != nil && len(sp.Mounts) > 0 {
783+
if len(sp.Mounts) > 0 {
777784
for _, mount := range sp.Mounts {
778785
if mount.Destination == "/dev/shm" {
779786
for _, option := range mount.Options {
@@ -800,6 +807,15 @@ func getSysctlFromNative(sp *specs.Spec) (map[string]string, error) {
800807
return res, nil
801808
}
802809

810+
func parseDeviceMapping(deviceMappingJSON string) ([]string, error) {
811+
var devices []string
812+
err := json.Unmarshal([]byte(deviceMappingJSON), &devices)
813+
if err != nil {
814+
return nil, fmt.Errorf("failed to parse device mapping: %v", err)
815+
}
816+
return devices, nil
817+
}
818+
803819
type IPAMConfig struct {
804820
Subnet string `json:"Subnet,omitempty"`
805821
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
@@ -114,11 +114,14 @@ const (
114114
CIdFile = Prefix + "cid-file"
115115

116116
// Custom DNS lookup servers.
117-
DnsServer = Prefix + "dns"
117+
DNSServer = Prefix + "dns"
118118

119119
// DNSResolvConfOptions set DNS options
120120
DNSResolvConfOptions = Prefix + "dns-options"
121121

122122
// DNSSearchDomains set custom DNS search domains
123123
DNSSearchDomains = Prefix + "dns-search"
124+
125+
//DeviceMapping specifies mapping host device to the container
126+
DeviceMapping = Prefix + "devices"
124127
)

0 commit comments

Comments
 (0)