From 1eed458a1d529fdc476d3f413426c722fcdbd074 Mon Sep 17 00:00:00 2001 From: Daniil Antoshin Date: Tue, 5 Nov 2024 15:02:12 +0200 Subject: [PATCH 1/4] feat(vi-vd): get newest default StorageClass in e2e tests Signed-off-by: Daniil Antoshin --- .../pkg/controller/service/disk_service.go | 17 ++++++++++++++++- tests/e2e/util_test.go | 17 ++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/images/virtualization-artifact/pkg/controller/service/disk_service.go b/images/virtualization-artifact/pkg/controller/service/disk_service.go index cba868a4f..39a8e8802 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,12 +510,26 @@ func (s DiskService) getDefaultStorageClass(ctx context.Context) (*storev1.Stora return nil, err } + defaultClasses := []storev1.StorageClass{} for _, sc := range scs.Items { if sc.Annotations[common.AnnDefaultStorageClass] == "true" { - return &sc, nil + defaultClasses = append(defaultClasses, sc) } } + 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 nil, ErrDefaultStorageClassNotFound } diff --git a/tests/e2e/util_test.go b/tests/e2e/util_test.go index 74c56e956..34a1e3230 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,13 +252,27 @@ func GetDefaultStorageClass() (*storagev1.StorageClass, error) { return nil, err } + defaultClasses := []storagev1.StorageClass{} for _, sc := range scList.Items { isDefault, ok := sc.Annotations["storageclass.kubernetes.io/is-default-class"] if ok && isDefault == "true" { - return &sc, nil + defaultClasses = append(defaultClasses, sc) } } + 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 nil, fmt.Errorf("Default StorageClass not found in the cluster: please set a default StorageClass.") } From d21bc4f5d34710bdad82e7cc0c03cfeabea6b664 Mon Sep 17 00:00:00 2001 From: Daniil Antoshin Date: Tue, 5 Nov 2024 16:19:47 +0200 Subject: [PATCH 2/4] feat(vi-vd): get newest default StorageClass in e2e tests Signed-off-by: Daniil Antoshin --- .../pkg/controller/service/disk_service.go | 6 +++--- tests/e2e/util_test.go | 9 ++++----- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/images/virtualization-artifact/pkg/controller/service/disk_service.go b/images/virtualization-artifact/pkg/controller/service/disk_service.go index 39a8e8802..acce86453 100644 --- a/images/virtualization-artifact/pkg/controller/service/disk_service.go +++ b/images/virtualization-artifact/pkg/controller/service/disk_service.go @@ -510,10 +510,10 @@ func (s DiskService) getDefaultStorageClass(ctx context.Context) (*storev1.Stora return nil, err } - defaultClasses := []storev1.StorageClass{} + var defaultClasses []*storev1.StorageClass for _, sc := range scs.Items { if sc.Annotations[common.AnnDefaultStorageClass] == "true" { - defaultClasses = append(defaultClasses, sc) + defaultClasses = append(defaultClasses, &sc) } } @@ -530,7 +530,7 @@ func (s DiskService) getDefaultStorageClass(ctx context.Context) (*storev1.Stora return defaultClasses[i].CreationTimestamp.UnixNano() > defaultClasses[j].CreationTimestamp.UnixNano() }) - return nil, ErrDefaultStorageClassNotFound + 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 34a1e3230..f2072ae2b 100644 --- a/tests/e2e/util_test.go +++ b/tests/e2e/util_test.go @@ -252,11 +252,10 @@ func GetDefaultStorageClass() (*storagev1.StorageClass, error) { return nil, err } - defaultClasses := []storagev1.StorageClass{} + var defaultClasses []*storagev1.StorageClass for _, sc := range scList.Items { - isDefault, ok := sc.Annotations["storageclass.kubernetes.io/is-default-class"] - if ok && isDefault == "true" { - defaultClasses = append(defaultClasses, sc) + if sc.Annotations["storageclass.kubernetes.io/is-default-class"] == "true" { + defaultClasses = append(defaultClasses, &sc) } } @@ -273,7 +272,7 @@ func GetDefaultStorageClass() (*storagev1.StorageClass, error) { return defaultClasses[i].CreationTimestamp.UnixNano() > defaultClasses[j].CreationTimestamp.UnixNano() }) - return nil, fmt.Errorf("Default StorageClass not found in the cluster: please set a default StorageClass.") + return defaultClasses[0], nil } func toIPNet(prefix netip.Prefix) *net.IPNet { From 3823f5309442c3763ba5a43b789b26a7230609ec Mon Sep 17 00:00:00 2001 From: Daniil Antoshin Date: Tue, 5 Nov 2024 17:24:56 +0200 Subject: [PATCH 3/4] feat(vi-vd): fix ling errors Signed-off-by: Daniil Antoshin --- .../pkg/controller/service/disk_service.go | 8 ++++---- tests/e2e/util_test.go | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/images/virtualization-artifact/pkg/controller/service/disk_service.go b/images/virtualization-artifact/pkg/controller/service/disk_service.go index acce86453..05ff9b799 100644 --- a/images/virtualization-artifact/pkg/controller/service/disk_service.go +++ b/images/virtualization-artifact/pkg/controller/service/disk_service.go @@ -26,6 +26,7 @@ import ( "strconv" "strings" + virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" vsv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1" corev1 "k8s.io/api/core/v1" storev1 "k8s.io/api/storage/v1" @@ -44,7 +45,6 @@ import ( "github.com/deckhouse/virtualization-controller/pkg/dvcr" "github.com/deckhouse/virtualization-controller/pkg/sdk/framework/helper" "github.com/deckhouse/virtualization-controller/pkg/util" - virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" ) type DiskService struct { @@ -511,9 +511,9 @@ func (s DiskService) getDefaultStorageClass(ctx context.Context) (*storev1.Stora } var defaultClasses []*storev1.StorageClass - for _, sc := range scs.Items { - if sc.Annotations[common.AnnDefaultStorageClass] == "true" { - defaultClasses = append(defaultClasses, &sc) + for idx := range scs.Items { + if scs.Items[idx].Annotations[common.AnnDefaultStorageClass] == "true" { + defaultClasses = append(defaultClasses, &scs.Items[idx]) } } diff --git a/tests/e2e/util_test.go b/tests/e2e/util_test.go index f2072ae2b..e48ae08f1 100644 --- a/tests/e2e/util_test.go +++ b/tests/e2e/util_test.go @@ -253,9 +253,9 @@ func GetDefaultStorageClass() (*storagev1.StorageClass, error) { } var defaultClasses []*storagev1.StorageClass - for _, sc := range scList.Items { - if sc.Annotations["storageclass.kubernetes.io/is-default-class"] == "true" { - defaultClasses = append(defaultClasses, &sc) + for idx := range scList.Items { + if scList.Items[idx].Annotations["storageclass.kubernetes.io/is-default-class"] == "true" { + defaultClasses = append(defaultClasses, &scList.Items[idx]) } } From 20644c1afaaebbc2220584006f353a08000d2fd6 Mon Sep 17 00:00:00 2001 From: Daniil Antoshin Date: Tue, 5 Nov 2024 18:15:50 +0200 Subject: [PATCH 4/4] feat(vi-vd): fix lint error Signed-off-by: Daniil Antoshin --- .../pkg/controller/service/disk_service.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/images/virtualization-artifact/pkg/controller/service/disk_service.go b/images/virtualization-artifact/pkg/controller/service/disk_service.go index 05ff9b799..32a86b2c1 100644 --- a/images/virtualization-artifact/pkg/controller/service/disk_service.go +++ b/images/virtualization-artifact/pkg/controller/service/disk_service.go @@ -26,7 +26,6 @@ import ( "strconv" "strings" - virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" vsv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1" corev1 "k8s.io/api/core/v1" storev1 "k8s.io/api/storage/v1" @@ -45,6 +44,7 @@ import ( "github.com/deckhouse/virtualization-controller/pkg/dvcr" "github.com/deckhouse/virtualization-controller/pkg/sdk/framework/helper" "github.com/deckhouse/virtualization-controller/pkg/util" + virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" ) type DiskService struct {