Skip to content

Commit f4f7da6

Browse files
committed
Ensure consistent sorting of annotation devices
Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent 5fe7b06 commit f4f7da6

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

internal/config/image/cuda_image.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package image
1919
import (
2020
"fmt"
2121
"path/filepath"
22+
"slices"
2223
"strconv"
2324
"strings"
2425

@@ -300,17 +301,24 @@ func (i CUDA) cdiDeviceRequestsFromAnnotations() []string {
300301
return nil
301302
}
302303

303-
var devices []string
304-
for key, value := range i.annotations {
304+
var annotationKeys []string
305+
for key := range i.annotations {
305306
for _, prefix := range i.annotationsPrefixes {
306307
if strings.HasPrefix(key, prefix) {
307-
devices = append(devices, strings.Split(value, ",")...)
308+
annotationKeys = append(annotationKeys, key)
308309
// There is no need to check additional prefixes since we
309310
// typically deduplicate devices in any case.
310311
break
311312
}
312313
}
313314
}
315+
// We sort the annotationKeys for consistent results.
316+
slices.Sort(annotationKeys)
317+
318+
var devices []string
319+
for _, key := range annotationKeys {
320+
devices = append(devices, strings.Split(i.annotations[key], ",")...)
321+
}
314322
return devices
315323
}
316324

internal/modifier/cdi_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func TestDeviceRequests(t *testing.T) {
9898
"another-prefix/bar": "example.com/device=baz",
9999
},
100100
},
101-
expectedDevices: []string{"example.com/device=bar", "example.com/device=baz"},
101+
expectedDevices: []string{"example.com/device=baz", "example.com/device=bar"},
102102
},
103103
{
104104
description: "multiple matching annotations with duplicate devices",

0 commit comments

Comments
 (0)