Skip to content

Commit

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

Why do we need it, and what problem does it solve?
Currently if there are multiple default StorageClasses, we take the random one, the behavior in k8s is different, the last annotated default StorageClass is taken

What is the expected result?
Take the last annotated default StorageClass
  • Loading branch information
danilrwx authored Nov 5, 2024
2 parents 3f6c227 + 20644c1 commit d0f447d
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 d0f447d

Please sign in to comment.