...

Source file src/k8s.io/kubernetes/pkg/volume/util/volumeattributesclass.go

Documentation: k8s.io/kubernetes/pkg/volume/util

     1  /*
     2  Copyright 2023 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package util
    18  
    19  import (
    20  	"sort"
    21  
    22  	storagev1alpha1 "k8s.io/api/storage/v1alpha1"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/apimachinery/pkg/labels"
    25  	storagev1alpha1listers "k8s.io/client-go/listers/storage/v1alpha1"
    26  	"k8s.io/klog/v2"
    27  )
    28  
    29  const (
    30  	// AlphaIsDefaultVolumeAttributesClassAnnotation is the alpha version of IsDefaultVolumeAttributesClassAnnotation.
    31  	AlphaIsDefaultVolumeAttributesClassAnnotation = "volumeattributesclass.alpha.kubernetes.io/is-default-class"
    32  )
    33  
    34  // GetDefaultVolumeAttributesClass returns the default VolumeAttributesClass from the store, or nil.
    35  func GetDefaultVolumeAttributesClass(lister storagev1alpha1listers.VolumeAttributesClassLister, driverName string) (*storagev1alpha1.VolumeAttributesClass, error) {
    36  	list, err := lister.List(labels.Everything())
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	defaultClasses := []*storagev1alpha1.VolumeAttributesClass{}
    42  	for _, class := range list {
    43  		if IsDefaultVolumeAttributesClassAnnotation(class.ObjectMeta) && class.DriverName == driverName {
    44  			defaultClasses = append(defaultClasses, class)
    45  			klog.V(4).Infof("GetDefaultVolumeAttributesClass added: %s", class.Name)
    46  		}
    47  	}
    48  
    49  	if len(defaultClasses) == 0 {
    50  		return nil, nil
    51  	}
    52  
    53  	// Primary sort by creation timestamp, newest first
    54  	// Secondary sort by class name, ascending order
    55  	sort.Slice(defaultClasses, func(i, j int) bool {
    56  		if defaultClasses[i].CreationTimestamp.UnixNano() == defaultClasses[j].CreationTimestamp.UnixNano() {
    57  			return defaultClasses[i].Name < defaultClasses[j].Name
    58  		}
    59  		return defaultClasses[i].CreationTimestamp.UnixNano() > defaultClasses[j].CreationTimestamp.UnixNano()
    60  	})
    61  	if len(defaultClasses) > 1 {
    62  		klog.V(4).Infof("%d default VolumeAttributesClass were found, choosing: %s", len(defaultClasses), defaultClasses[0].Name)
    63  	}
    64  
    65  	return defaultClasses[0], nil
    66  }
    67  
    68  // IsDefaultVolumeAttributesClassAnnotation returns a boolean if the default
    69  // volume attributes class annotation is set
    70  func IsDefaultVolumeAttributesClassAnnotation(obj metav1.ObjectMeta) bool {
    71  	return obj.Annotations[AlphaIsDefaultVolumeAttributesClassAnnotation] == "true"
    72  }
    73  

View as plain text