Ensure consistent sorting of annotation devices

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2025-06-13 16:25:11 +02:00
parent 8f514be1e8
commit 7fe0aad96b
No known key found for this signature in database
2 changed files with 12 additions and 4 deletions

View File

@ -19,6 +19,7 @@ package image
import (
"fmt"
"path/filepath"
"slices"
"strconv"
"strings"
@ -281,17 +282,24 @@ func (i CUDA) cdiDeviceRequestsFromAnnotations() []string {
return nil
}
var devices []string
for key, value := range i.annotations {
var annotationKeys []string
for key := range i.annotations {
for _, prefix := range i.annotationsPrefixes {
if strings.HasPrefix(key, prefix) {
devices = append(devices, strings.Split(value, ",")...)
annotationKeys = append(annotationKeys, key)
// There is no need to check additional prefixes since we
// typically deduplicate devices in any case.
break
}
}
}
// We sort the annotationKeys for consistent results.
slices.Sort(annotationKeys)
var devices []string
for _, key := range annotationKeys {
devices = append(devices, strings.Split(i.annotations[key], ",")...)
}
return devices
}

View File

@ -98,7 +98,7 @@ func TestDeviceRequests(t *testing.T) {
"another-prefix/bar": "example.com/device=baz",
},
},
expectedDevices: []string{"example.com/device=bar", "example.com/device=baz"},
expectedDevices: []string{"example.com/device=baz", "example.com/device=bar"},
},
{
description: "multiple matching annotations with duplicate devices",