|
7 | 7 | "os" |
8 | 8 | "path" |
9 | 9 | "path/filepath" |
10 | | - "strings" |
11 | 10 |
|
12 | 11 | "github.com/opencontainers/runtime-spec/specs-go" |
13 | 12 | "golang.org/x/mod/semver" |
@@ -36,7 +35,7 @@ const ( |
36 | 35 | ) |
37 | 36 |
|
38 | 37 | type nvidiaConfig struct { |
39 | | - Devices string |
| 38 | + Devices []string |
40 | 39 | MigConfigDevices string |
41 | 40 | MigMonitorDevices string |
42 | 41 | ImexChannels string |
@@ -172,34 +171,19 @@ func isPrivileged(s *Spec) bool { |
172 | 171 | return image.IsPrivileged(&fullSpec) |
173 | 172 | } |
174 | 173 |
|
175 | | -func getDevicesFromEnvvar(image image.CUDA, swarmResourceEnvvars []string) *string { |
| 174 | +func getDevicesFromEnvvar(image image.CUDA, swarmResourceEnvvars []string) []string { |
176 | 175 | // We check if the image has at least one of the Swarm resource envvars defined and use this |
177 | 176 | // if specified. |
178 | | - var hasSwarmEnvvar bool |
179 | 177 | for _, envvar := range swarmResourceEnvvars { |
180 | 178 | if image.HasEnvvar(envvar) { |
181 | | - hasSwarmEnvvar = true |
182 | | - break |
| 179 | + return image.DevicesFromEnvvars(swarmResourceEnvvars...).List() |
183 | 180 | } |
184 | 181 | } |
185 | 182 |
|
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() |
200 | 184 | } |
201 | 185 |
|
202 | | -func getDevicesFromMounts(mounts []Mount) *string { |
| 186 | +func getDevicesFromMounts(mounts []Mount) []string { |
203 | 187 | var devices []string |
204 | 188 | for _, m := range mounts { |
205 | 189 | root := filepath.Clean(deviceListAsVolumeMountsRoot) |
@@ -232,22 +216,21 @@ func getDevicesFromMounts(mounts []Mount) *string { |
232 | 216 | return nil |
233 | 217 | } |
234 | 218 |
|
235 | | - ret := strings.Join(devices, ",") |
236 | | - return &ret |
| 219 | + return devices |
237 | 220 | } |
238 | 221 |
|
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 { |
240 | 223 | // If enabled, try and get the device list from volume mounts first |
241 | 224 | if hookConfig.AcceptDeviceListAsVolumeMounts { |
242 | 225 | devices := getDevicesFromMounts(mounts) |
243 | | - if devices != nil { |
| 226 | + if len(devices) > 0 { |
244 | 227 | return devices |
245 | 228 | } |
246 | 229 | } |
247 | 230 |
|
248 | 231 | // Fallback to reading from the environment variable if privileges are correct |
249 | 232 | devices := getDevicesFromEnvvar(image, hookConfig.getSwarmResourceEnvvars()) |
250 | | - if devices == nil { |
| 233 | + if len(devices) == 0 { |
251 | 234 | return nil |
252 | 235 | } |
253 | 236 | if privileged || hookConfig.AcceptEnvvarUnprivileged { |
@@ -314,11 +297,9 @@ func (c *HookConfig) getDriverCapabilities(cudaImage image.CUDA, legacyImage boo |
314 | 297 | func getNvidiaConfig(hookConfig *HookConfig, image image.CUDA, mounts []Mount, privileged bool) *nvidiaConfig { |
315 | 298 | legacyImage := image.IsLegacy() |
316 | 299 |
|
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. |
322 | 303 | return nil |
323 | 304 | } |
324 | 305 |
|
|
0 commit comments