Skip to content

Commit 5d73d27

Browse files
committed
Deduplicate requested device IDs
This change ensures that the incoming device IDs are deduplicated before updating the AllocateResponse. This avoids cases where the NVIDIA_VISIBLE_DEVICES envvar or CDI annotations contain repeated device UUIDs or INDICES that do not add additional modifications to the container. Signed-off-by: Evan Lezar <[email protected]>
1 parent af276bf commit 5d73d27

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

internal/plugin/server.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ func (plugin *nvidiaDevicePlugin) Allocate(ctx context.Context, reqs *pluginapi.
319319
}
320320

321321
func (plugin *nvidiaDevicePlugin) getAllocateResponse(requestIds []string) (*pluginapi.ContainerAllocateResponse, error) {
322-
deviceIDs := plugin.deviceIDsFromAnnotatedDeviceIDs(requestIds)
322+
deviceIDs := plugin.uniqueDeviceIDsFromAnnotatedDeviceIDs(requestIds)
323323

324324
// Create an empty response that will be updated as required below.
325325
response := &pluginapi.ContainerAllocateResponse{
@@ -452,15 +452,24 @@ func (plugin *nvidiaDevicePlugin) dial(unixSocketPath string, timeout time.Durat
452452
return c, nil
453453
}
454454

455-
func (plugin *nvidiaDevicePlugin) deviceIDsFromAnnotatedDeviceIDs(ids []string) []string {
455+
func (plugin *nvidiaDevicePlugin) uniqueDeviceIDsFromAnnotatedDeviceIDs(ids []string) []string {
456456
var deviceIDs []string
457457
if *plugin.config.Flags.Plugin.DeviceIDStrategy == spec.DeviceIDStrategyUUID {
458458
deviceIDs = rm.AnnotatedIDs(ids).GetIDs()
459459
}
460460
if *plugin.config.Flags.Plugin.DeviceIDStrategy == spec.DeviceIDStrategyIndex {
461461
deviceIDs = plugin.rm.Devices().Subset(ids).GetIndices()
462462
}
463-
return deviceIDs
463+
var uniqueIDs []string
464+
seen := make(map[string]bool)
465+
for _, id := range deviceIDs {
466+
if seen[id] {
467+
continue
468+
}
469+
seen[id] = true
470+
uniqueIDs = append(uniqueIDs, id)
471+
}
472+
return uniqueIDs
464473
}
465474

466475
func (plugin *nvidiaDevicePlugin) apiDevices() []*pluginapi.Device {

0 commit comments

Comments
 (0)