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 8bfce9488d
commit 7f5ad9c5b2
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 ( import (
"fmt" "fmt"
"path/filepath" "path/filepath"
"slices"
"strconv" "strconv"
"strings" "strings"
@ -281,17 +282,24 @@ func (i CUDA) cdiDeviceRequestsFromAnnotations() []string {
return nil return nil
} }
var devices []string var annotationKeys []string
for key, value := range i.annotations { for key := range i.annotations {
for _, prefix := range i.annotationsPrefixes { for _, prefix := range i.annotationsPrefixes {
if strings.HasPrefix(key, prefix) { 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 // There is no need to check additional prefixes since we
// typically deduplicate devices in any case. // typically deduplicate devices in any case.
break 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 return devices
} }

View File

@ -98,7 +98,7 @@ func TestDeviceRequests(t *testing.T) {
"another-prefix/bar": "example.com/device=baz", "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", description: "multiple matching annotations with duplicate devices",