Skip to content

Commit e8aa3cc

Browse files
committed
Merge branch 'ignore-nvidia-visible-devices' into 'master'
Ignore NVIDIA_VISIBLE_DEVICES for containers with insufficent privileges See merge request nvidia/container-toolkit/container-toolkit!25
2 parents 9751646 + fc408a3 commit e8aa3cc

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

pkg/container_config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,8 @@ func getDevices(hookConfig *HookConfig, env map[string]string, mounts []Mount, p
295295
return devices
296296
}
297297

298-
// Error out otherwise
299-
log.Panicln("insufficient privileges to read device list from NVIDIA_VISIBLE_DEVICES envvar")
298+
configName := hookConfig.getConfigOption("AcceptEnvvarUnprivileged")
299+
log.Printf("Ignoring devices specified in NVIDIA_VISIBLE_DEVICES (privileged=%v, %v=%v) ", privileged, configName, hookConfig.AcceptEnvvarUnprivileged)
300300

301301
return nil
302302
}

pkg/container_test.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,6 @@ func TestDeviceListSourcePriority(t *testing.T) {
540540
acceptUnprivileged bool
541541
acceptMounts bool
542542
expectedDevices *string
543-
expectedPanic bool
544543
}{
545544
{
546545
description: "Mount devices, unprivileged, no accept unprivileged",
@@ -567,7 +566,7 @@ func TestDeviceListSourcePriority(t *testing.T) {
567566
privileged: false,
568567
acceptUnprivileged: false,
569568
acceptMounts: true,
570-
expectedPanic: true,
569+
expectedDevices: nil,
571570
},
572571
{
573572
description: "No mount devices, privileged, no accept unprivileged",
@@ -621,7 +620,7 @@ func TestDeviceListSourcePriority(t *testing.T) {
621620
privileged: false,
622621
acceptUnprivileged: false,
623622
acceptMounts: false,
624-
expectedPanic: true,
623+
expectedDevices: nil,
625624
},
626625
}
627626
for _, tc := range tests {
@@ -638,12 +637,6 @@ func TestDeviceListSourcePriority(t *testing.T) {
638637
devices = getDevices(&hookConfig, env, tc.mountDevices, tc.privileged, false)
639638
}
640639

641-
// For any tests that are expected to panic, make sure they do.
642-
if tc.expectedPanic {
643-
mustPanic(t, getDevices)
644-
return
645-
}
646-
647640
// For all other tests, just grab the devices and check the results
648641
getDevices()
649642
if !reflect.DeepEqual(devices, tc.expectedDevices) {

pkg/hook_config.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"log"
55
"os"
66
"path"
7+
"reflect"
78

89
"github.com/BurntSushi/toml"
910
)
@@ -86,3 +87,18 @@ func getHookConfig() (config HookConfig) {
8687

8788
return config
8889
}
90+
91+
// getConfigOption returns the toml config option associated with the
92+
// specified struct field.
93+
func (c HookConfig) getConfigOption(fieldName string) string {
94+
t := reflect.TypeOf(c)
95+
f, ok := t.FieldByName(fieldName)
96+
if !ok {
97+
return fieldName
98+
}
99+
v, ok := f.Tag.Lookup("toml")
100+
if !ok {
101+
return fieldName
102+
}
103+
return v
104+
}

0 commit comments

Comments
 (0)