diff --git a/internal/config/image/cuda_image.go b/internal/config/image/cuda_image.go index c295d105..05d669f7 100644 --- a/internal/config/image/cuda_image.go +++ b/internal/config/image/cuda_image.go @@ -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 } diff --git a/internal/modifier/cdi_test.go b/internal/modifier/cdi_test.go index 881e8b2c..a5ca78f5 100644 --- a/internal/modifier/cdi_test.go +++ b/internal/modifier/cdi_test.go @@ -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",