@@ -232,6 +232,22 @@ func (i CUDA) OnlyFullyQualifiedCDIDevices() bool {
232232 return hasCDIdevice
233233}
234234
235+ // visibleEnvVars returns the environment variables that are used to determine device visibility.
236+ // It returns the preferred environment variables that are set, or NVIDIA_VISIBLE_DEVICES if none are set.
237+ func (i CUDA ) visibleEnvVars () []string {
238+ var envVars []string
239+ for _ , envVar := range i .preferredVisibleDeviceEnvVars {
240+ if ! i .HasEnvvar (envVar ) {
241+ continue
242+ }
243+ envVars = append (envVars , envVar )
244+ }
245+ if len (envVars ) > 0 {
246+ return envVars
247+ }
248+ return []string {EnvVarNvidiaVisibleDevices }
249+ }
250+
235251// VisibleDevices returns a list of devices requested in the container image.
236252// If volume mount requests are enabled these are returned if requested,
237253// otherwise device requests through environment variables are considered.
@@ -265,7 +281,10 @@ func (i CUDA) VisibleDevices() []string {
265281 }
266282
267283 // We log a warning if we are ignoring the environment variable requests.
268- i .logger .Warningf ("Ignoring devices specified in NVIDIA_VISIBLE_DEVICES in unprivileged container" )
284+ envVars := i .visibleEnvVars ()
285+ if len (envVars ) > 0 {
286+ i .logger .Warningf ("Ignoring devices requested by environment variable(s) in unprivileged container: %v" , envVars )
287+ }
269288
270289 return nil
271290}
@@ -300,12 +319,8 @@ func (i CUDA) cdiDeviceRequestsFromAnnotations() []string {
300319// are used to determine the visible devices. If this is not the case, the
301320// NVIDIA_VISIBLE_DEVICES environment variable is used.
302321func (i CUDA ) VisibleDevicesFromEnvVar () []string {
303- for _ , envVar := range i .preferredVisibleDeviceEnvVars {
304- if i .HasEnvvar (envVar ) {
305- return i .DevicesFromEnvvars (i .preferredVisibleDeviceEnvVars ... ).List ()
306- }
307- }
308- return i .DevicesFromEnvvars (EnvVarNvidiaVisibleDevices ).List ()
322+ envVars := i .visibleEnvVars ()
323+ return i .DevicesFromEnvvars (envVars ... ).List ()
309324}
310325
311326// visibleDevicesFromMounts returns the set of visible devices requested as mounts.
0 commit comments