Skip to content

Commit cd63a9d

Browse files
committed
[no-relnote] Use string slice for devices in hook
Signed-off-by: Evan Lezar <[email protected]>
1 parent f794d09 commit cd63a9d

File tree

3 files changed

+67
-98
lines changed

3 files changed

+67
-98
lines changed

cmd/nvidia-container-runtime-hook/container_config.go

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"os"
88
"path"
99
"path/filepath"
10-
"strings"
1110

1211
"github.com/opencontainers/runtime-spec/specs-go"
1312
"golang.org/x/mod/semver"
@@ -36,7 +35,7 @@ const (
3635
)
3736

3837
type nvidiaConfig struct {
39-
Devices string
38+
Devices []string
4039
MigConfigDevices string
4140
MigMonitorDevices string
4241
ImexChannels string
@@ -172,34 +171,19 @@ func isPrivileged(s *Spec) bool {
172171
return image.IsPrivileged(&fullSpec)
173172
}
174173

175-
func getDevicesFromEnvvar(image image.CUDA, swarmResourceEnvvars []string) *string {
174+
func getDevicesFromEnvvar(image image.CUDA, swarmResourceEnvvars []string) []string {
176175
// We check if the image has at least one of the Swarm resource envvars defined and use this
177176
// if specified.
178-
var hasSwarmEnvvar bool
179177
for _, envvar := range swarmResourceEnvvars {
180178
if image.HasEnvvar(envvar) {
181-
hasSwarmEnvvar = true
182-
break
179+
return image.DevicesFromEnvvars(swarmResourceEnvvars...).List()
183180
}
184181
}
185182

186-
var devices []string
187-
if hasSwarmEnvvar {
188-
devices = image.DevicesFromEnvvars(swarmResourceEnvvars...).List()
189-
} else {
190-
devices = image.DevicesFromEnvvars(envNVVisibleDevices).List()
191-
}
192-
193-
if len(devices) == 0 {
194-
return nil
195-
}
196-
197-
devicesString := strings.Join(devices, ",")
198-
199-
return &devicesString
183+
return image.DevicesFromEnvvars(envNVVisibleDevices).List()
200184
}
201185

202-
func getDevicesFromMounts(mounts []Mount) *string {
186+
func getDevicesFromMounts(mounts []Mount) []string {
203187
var devices []string
204188
for _, m := range mounts {
205189
root := filepath.Clean(deviceListAsVolumeMountsRoot)
@@ -232,22 +216,21 @@ func getDevicesFromMounts(mounts []Mount) *string {
232216
return nil
233217
}
234218

235-
ret := strings.Join(devices, ",")
236-
return &ret
219+
return devices
237220
}
238221

239-
func getDevices(hookConfig *HookConfig, image image.CUDA, mounts []Mount, privileged bool) *string {
222+
func getDevices(hookConfig *HookConfig, image image.CUDA, mounts []Mount, privileged bool) []string {
240223
// If enabled, try and get the device list from volume mounts first
241224
if hookConfig.AcceptDeviceListAsVolumeMounts {
242225
devices := getDevicesFromMounts(mounts)
243-
if devices != nil {
226+
if len(devices) > 0 {
244227
return devices
245228
}
246229
}
247230

248231
// Fallback to reading from the environment variable if privileges are correct
249232
devices := getDevicesFromEnvvar(image, hookConfig.getSwarmResourceEnvvars())
250-
if devices == nil {
233+
if len(devices) == 0 {
251234
return nil
252235
}
253236
if privileged || hookConfig.AcceptEnvvarUnprivileged {
@@ -314,11 +297,9 @@ func (c *HookConfig) getDriverCapabilities(cudaImage image.CUDA, legacyImage boo
314297
func getNvidiaConfig(hookConfig *HookConfig, image image.CUDA, mounts []Mount, privileged bool) *nvidiaConfig {
315298
legacyImage := image.IsLegacy()
316299

317-
var devices string
318-
if d := getDevices(hookConfig, image, mounts, privileged); d != nil {
319-
devices = *d
320-
} else {
321-
// 'nil' devices means this is not a GPU container.
300+
devices := getDevices(hookConfig, image, mounts, privileged)
301+
if len(devices) == 0 {
302+
// empty devices means this is not a GPU container.
322303
return nil
323304
}
324305

0 commit comments

Comments
 (0)