diff --git a/pkg/volume/util/volumeattributesclass.go b/pkg/volume/util/volumeattributesclass.go deleted file mode 100644 index 0c1e68dc7e8..00000000000 --- a/pkg/volume/util/volumeattributesclass.go +++ /dev/null @@ -1,72 +0,0 @@ -/* -Copyright 2023 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package util - -import ( - "sort" - - storagev1beta1 "k8s.io/api/storage/v1beta1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/labels" - storagev1beta1listers "k8s.io/client-go/listers/storage/v1beta1" - "k8s.io/klog/v2" -) - -const ( - // AlphaIsDefaultVolumeAttributesClassAnnotation is the alpha version of IsDefaultVolumeAttributesClassAnnotation. - AlphaIsDefaultVolumeAttributesClassAnnotation = "volumeattributesclass.alpha.kubernetes.io/is-default-class" -) - -// GetDefaultVolumeAttributesClass returns the default VolumeAttributesClass from the store, or nil. -func GetDefaultVolumeAttributesClass(lister storagev1beta1listers.VolumeAttributesClassLister, driverName string) (*storagev1beta1.VolumeAttributesClass, error) { - list, err := lister.List(labels.Everything()) - if err != nil { - return nil, err - } - - defaultClasses := []*storagev1beta1.VolumeAttributesClass{} - for _, class := range list { - if IsDefaultVolumeAttributesClassAnnotation(class.ObjectMeta) && class.DriverName == driverName { - defaultClasses = append(defaultClasses, class) - klog.V(4).Infof("GetDefaultVolumeAttributesClass added: %s", class.Name) - } - } - - if len(defaultClasses) == 0 { - return nil, nil - } - - // 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() - }) - if len(defaultClasses) > 1 { - klog.V(4).Infof("%d default VolumeAttributesClass were found, choosing: %s", len(defaultClasses), defaultClasses[0].Name) - } - - return defaultClasses[0], nil -} - -// IsDefaultVolumeAttributesClassAnnotation returns a boolean if the default -// volume attributes class annotation is set -func IsDefaultVolumeAttributesClassAnnotation(obj metav1.ObjectMeta) bool { - return obj.Annotations[AlphaIsDefaultVolumeAttributesClassAnnotation] == "true" -} diff --git a/pkg/volume/util/volumeattributesclass_test.go b/pkg/volume/util/volumeattributesclass_test.go deleted file mode 100644 index 08e1815949c..00000000000 --- a/pkg/volume/util/volumeattributesclass_test.go +++ /dev/null @@ -1,224 +0,0 @@ -/* -Copyright 2023 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package util - -import ( - "testing" - "time" - - storagev1beta1 "k8s.io/api/storage/v1beta1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/client-go/informers" - "k8s.io/kubernetes/pkg/controller" -) - -func TestGetDefaultVolumeAttributesClass(t *testing.T) { - var ( - t1 = time.Now() - t2 = time.Now().Add(1 * time.Hour) - ) - - dirverName1 := "my-driver1" - vac1 := &storagev1beta1.VolumeAttributesClass{ - ObjectMeta: metav1.ObjectMeta{ - Name: "my-vac1", - Annotations: map[string]string{ - "a": "b", - }, - }, - DriverName: dirverName1, - } - vac2 := &storagev1beta1.VolumeAttributesClass{ - ObjectMeta: metav1.ObjectMeta{ - Name: "my-vac2", - Annotations: map[string]string{ - "a": "b", - }, - }, - DriverName: dirverName1, - } - vac3 := &storagev1beta1.VolumeAttributesClass{ - ObjectMeta: metav1.ObjectMeta{ - Name: "my-vac3", - Annotations: map[string]string{ - AlphaIsDefaultVolumeAttributesClassAnnotation: "true", - }, - CreationTimestamp: metav1.Time{Time: t1}, - }, - DriverName: dirverName1, - } - vac4 := &storagev1beta1.VolumeAttributesClass{ - ObjectMeta: metav1.ObjectMeta{ - Name: "my-vac4", - Annotations: map[string]string{ - AlphaIsDefaultVolumeAttributesClassAnnotation: "true", - }, - CreationTimestamp: metav1.Time{Time: t2}, - }, - DriverName: dirverName1, - } - vac5 := &storagev1beta1.VolumeAttributesClass{ - ObjectMeta: metav1.ObjectMeta{ - Name: "my-vac5", - Annotations: map[string]string{ - AlphaIsDefaultVolumeAttributesClassAnnotation: "true", - }, - CreationTimestamp: metav1.Time{Time: t2}, - }, - DriverName: dirverName1, - } - - dirverName2 := "my-driver2" - vac6 := &storagev1beta1.VolumeAttributesClass{ - ObjectMeta: metav1.ObjectMeta{ - Name: "my-vac6", - Annotations: map[string]string{ - "a": "b", - }, - }, - DriverName: dirverName2, - } - vac7 := &storagev1beta1.VolumeAttributesClass{ - ObjectMeta: metav1.ObjectMeta{ - Name: "my-vac7", - Annotations: map[string]string{ - AlphaIsDefaultVolumeAttributesClassAnnotation: "true", - }, - }, - DriverName: dirverName2, - } - - testCases := []struct { - name string - driverName string - classes []*storagev1beta1.VolumeAttributesClass - expect *storagev1beta1.VolumeAttributesClass - }{ - { - name: "no volume attributes class", - driverName: dirverName1, - }, - { - name: "no default volume attributes class", - driverName: dirverName1, - classes: []*storagev1beta1.VolumeAttributesClass{vac1, vac2, vac6}, - expect: nil, - }, - { - name: "no default volume attributes class for the driverName1", - driverName: dirverName1, - classes: []*storagev1beta1.VolumeAttributesClass{vac1, vac2, vac6, vac7}, - expect: nil, - }, - { - name: "one default volume attributes class for the driverName1", - driverName: dirverName1, - classes: []*storagev1beta1.VolumeAttributesClass{vac1, vac2, vac3, vac6, vac7}, - expect: vac3, - }, - { - name: "two default volume attributes class with different creation timestamp for the driverName1", - driverName: dirverName1, - classes: []*storagev1beta1.VolumeAttributesClass{vac3, vac4, vac6, vac7}, - expect: vac4, - }, - { - name: "two default volume attributes class with same creation timestamp for the driverName1", - driverName: dirverName1, - classes: []*storagev1beta1.VolumeAttributesClass{vac4, vac5, vac6, vac7}, - expect: vac4, - }, - } - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - informerFactory := informers.NewSharedInformerFactory(nil, controller.NoResyncPeriodFunc()) - for _, c := range tc.classes { - err := informerFactory.Storage().V1beta1().VolumeAttributesClasses().Informer().GetStore().Add(c) - if err != nil { - t.Errorf("Expected no error, got %v", err) - return - } - } - lister := informerFactory.Storage().V1beta1().VolumeAttributesClasses().Lister() - actual, err := GetDefaultVolumeAttributesClass(lister, tc.driverName) - if err != nil { - t.Errorf("Expected no error, got %v", err) - return - } - if tc.expect != actual { - t.Errorf("Expected %v, got %v", tc.expect, actual) - } - }) - } -} - -func TestIsDefaultVolumeAttributesClassAnnotation(t *testing.T) { - testCases := []struct { - name string - class *storagev1beta1.VolumeAttributesClass - expect bool - }{ - { - name: "no annotation", - class: &storagev1beta1.VolumeAttributesClass{}, - expect: false, - }, - { - name: "annotation is not boolean", - class: &storagev1beta1.VolumeAttributesClass{ - ObjectMeta: metav1.ObjectMeta{ - Annotations: map[string]string{ - AlphaIsDefaultVolumeAttributesClassAnnotation: "not-boolean", - }, - }, - }, - expect: false, - }, - { - name: "annotation is false", - class: &storagev1beta1.VolumeAttributesClass{ - ObjectMeta: metav1.ObjectMeta{ - Annotations: map[string]string{ - AlphaIsDefaultVolumeAttributesClassAnnotation: "false", - }, - }, - }, - expect: false, - }, - { - name: "annotation is true", - class: &storagev1beta1.VolumeAttributesClass{ - ObjectMeta: metav1.ObjectMeta{ - Annotations: map[string]string{ - AlphaIsDefaultVolumeAttributesClassAnnotation: "true", - }, - }, - }, - expect: true, - }, - } - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - actual := IsDefaultVolumeAttributesClassAnnotation(tc.class.ObjectMeta) - if tc.expect != actual { - t.Errorf("Expected %v, got %v", tc.expect, actual) - } - }) - } -}