diff --git a/images/virtualization-artifact/pkg/controller/service/disk_service.go b/images/virtualization-artifact/pkg/controller/service/disk_service.go index cba868a4f..32a86b2c1 100644 --- a/images/virtualization-artifact/pkg/controller/service/disk_service.go +++ b/images/virtualization-artifact/pkg/controller/service/disk_service.go @@ -22,6 +22,7 @@ import ( "errors" "fmt" "slices" + "sort" "strconv" "strings" @@ -509,13 +510,27 @@ func (s DiskService) getDefaultStorageClass(ctx context.Context) (*storev1.Stora return nil, err } - for _, sc := range scs.Items { - if sc.Annotations[common.AnnDefaultStorageClass] == "true" { - return &sc, nil + var defaultClasses []*storev1.StorageClass + for idx := range scs.Items { + if scs.Items[idx].Annotations[common.AnnDefaultStorageClass] == "true" { + defaultClasses = append(defaultClasses, &scs.Items[idx]) } } - return nil, ErrDefaultStorageClassNotFound + if len(defaultClasses) == 0 { + return nil, ErrDefaultStorageClassNotFound + } + + // Primary sort by creation timestamp, newest first + // Secondary sort by class name, ascending order + sort.Slice(defaultClasses, func(i, j int) bool { + if defaultClasses[i].CreationTimestamp.UnixNano() == defaultClasses[j].CreationTimestamp.UnixNano() { + return defaultClasses[i].Name < defaultClasses[j].Name + } + return defaultClasses[i].CreationTimestamp.UnixNano() > defaultClasses[j].CreationTimestamp.UnixNano() + }) + + return defaultClasses[0], nil } func (s DiskService) getStorageClass(ctx context.Context, storageClassName string) (*storev1.StorageClass, error) { diff --git a/tests/e2e/util_test.go b/tests/e2e/util_test.go index 74c56e956..e48ae08f1 100644 --- a/tests/e2e/util_test.go +++ b/tests/e2e/util_test.go @@ -23,6 +23,7 @@ import ( "net" "net/netip" "os" + "sort" "strings" "time" @@ -251,14 +252,27 @@ func GetDefaultStorageClass() (*storagev1.StorageClass, error) { return nil, err } - for _, sc := range scList.Items { - isDefault, ok := sc.Annotations["storageclass.kubernetes.io/is-default-class"] - if ok && isDefault == "true" { - return &sc, nil + var defaultClasses []*storagev1.StorageClass + for idx := range scList.Items { + if scList.Items[idx].Annotations["storageclass.kubernetes.io/is-default-class"] == "true" { + defaultClasses = append(defaultClasses, &scList.Items[idx]) } } - return nil, fmt.Errorf("Default StorageClass not found in the cluster: please set a default StorageClass.") + if len(defaultClasses) == 0 { + return nil, fmt.Errorf("Default StorageClass not found in the cluster: please set a default StorageClass.") + } + + // Primary sort by creation timestamp, newest first + // Secondary sort by class name, ascending order + sort.Slice(defaultClasses, func(i, j int) bool { + if defaultClasses[i].CreationTimestamp.UnixNano() == defaultClasses[j].CreationTimestamp.UnixNano() { + return defaultClasses[i].Name < defaultClasses[j].Name + } + return defaultClasses[i].CreationTimestamp.UnixNano() > defaultClasses[j].CreationTimestamp.UnixNano() + }) + + return defaultClasses[0], nil } func toIPNet(prefix netip.Prefix) *net.IPNet {