[no-relnote] new helper func visibleEnvVars

Signed-off-by: Carlos Eduardo Arango Gutierrez <eduardoa@nvidia.com>
This commit is contained in:
Carlos Eduardo Arango Gutierrez 2025-06-05 13:21:25 +02:00
parent 0a3146f74f
commit 6fc6f2c594
No known key found for this signature in database
GPG Key ID: 42D9CB42F300A852

View File

@ -228,6 +228,21 @@ func (i CUDA) OnlyFullyQualifiedCDIDevices() bool {
return hasCDIdevice return hasCDIdevice
} }
// visibleEnvVars returns the environment variables that are used to determine device visibility.
// It returns the preferred environment variables that are set, or NVIDIA_VISIBLE_DEVICES if none are set.
func (i CUDA) visibleEnvVars() []string {
var envVars []string
for _, envVar := range i.preferredVisibleDeviceEnvVars {
if i.HasEnvvar(envVar) {
envVars = append(envVars, envVar)
}
}
if len(envVars) == 0 {
envVars = append(envVars, EnvVarNvidiaVisibleDevices)
}
return envVars
}
// VisibleDevices returns a list of devices requested in the container image. // VisibleDevices returns a list of devices requested in the container image.
// If volume mount requests are enabled these are returned if requested, // If volume mount requests are enabled these are returned if requested,
// otherwise device requests through environment variables are considered. // otherwise device requests through environment variables are considered.
@ -255,7 +270,10 @@ func (i CUDA) VisibleDevices() []string {
} }
// We log a warning if we are ignoring the environment variable requests. // We log a warning if we are ignoring the environment variable requests.
i.logger.Warningf("Ignoring devices specified in NVIDIA_VISIBLE_DEVICES in unprivileged container") envVars := i.visibleEnvVars()
if len(envVars) > 0 {
i.logger.Warningf("Ignoring devices requested by environment variable(s) in unprivileged container: %v", envVars)
}
return nil return nil
} }
@ -265,12 +283,8 @@ func (i CUDA) VisibleDevices() []string {
// are used to determine the visible devices. If this is not the case, the // are used to determine the visible devices. If this is not the case, the
// NVIDIA_VISIBLE_DEVICES environment variable is used. // NVIDIA_VISIBLE_DEVICES environment variable is used.
func (i CUDA) VisibleDevicesFromEnvVar() []string { func (i CUDA) VisibleDevicesFromEnvVar() []string {
for _, envVar := range i.preferredVisibleDeviceEnvVars { envVars := i.visibleEnvVars()
if i.HasEnvvar(envVar) { return i.DevicesFromEnvvars(envVars...).List()
return i.DevicesFromEnvvars(i.preferredVisibleDeviceEnvVars...).List()
}
}
return i.DevicesFromEnvvars(EnvVarNvidiaVisibleDevices).List()
} }
// visibleDevicesFromMounts returns the set of visible devices requested as mounts. // visibleDevicesFromMounts returns the set of visible devices requested as mounts.