Skip to content

Commit

Permalink
feat(vi-vd): get newest default StorageClass (#499)
Browse files Browse the repository at this point in the history
Align the method for getting the default StorageClass with how the default StorageClass is defined in k8s

Signed-off-by: Daniil Antoshin <[email protected]>
  • Loading branch information
danilrwx authored and universal-itengineer committed Nov 6, 2024
1 parent 3f6c227 commit 6e0c7e7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"errors"
"fmt"
"slices"
"sort"
"strconv"
"strings"

Expand Down Expand Up @@ -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) {
Expand Down
24 changes: 19 additions & 5 deletions tests/e2e/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"net"
"net/netip"
"os"
"sort"
"strings"
"time"

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 6e0c7e7

Please sign in to comment.