...

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

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

     1  /*
     2  Copyright 2022 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  	storagev1 "k8s.io/api/storage/v1"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/apimachinery/pkg/labels"
    25  	storagev1listers "k8s.io/client-go/listers/storage/v1"
    26  	"k8s.io/klog/v2"
    27  )
    28  
    29  const (
    30  	// isDefaultStorageClassAnnotation represents a StorageClass annotation that
    31  	// marks a class as the default StorageClass
    32  	IsDefaultStorageClassAnnotation = "storageclass.kubernetes.io/is-default-class"
    33  
    34  	// betaIsDefaultStorageClassAnnotation is the beta version of IsDefaultStorageClassAnnotation.
    35  	// TODO: remove Beta when no longer used
    36  	BetaIsDefaultStorageClassAnnotation = "storageclass.beta.kubernetes.io/is-default-class"
    37  )
    38  
    39  // GetDefaultClass returns the default StorageClass from the store, or nil.
    40  func GetDefaultClass(lister storagev1listers.StorageClassLister) (*storagev1.StorageClass, error) {
    41  	list, err := lister.List(labels.Everything())
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	defaultClasses := []*storagev1.StorageClass{}
    47  	for _, class := range list {
    48  		if IsDefaultAnnotation(class.ObjectMeta) {
    49  			defaultClasses = append(defaultClasses, class)
    50  			klog.V(4).Infof("GetDefaultClass added: %s", class.Name)
    51  		}
    52  	}
    53  
    54  	if len(defaultClasses) == 0 {
    55  		return nil, nil
    56  	}
    57  
    58  	// Primary sort by creation timestamp, newest first
    59  	// Secondary sort by class name, ascending order
    60  	sort.Slice(defaultClasses, func(i, j int) bool {
    61  		if defaultClasses[i].CreationTimestamp.UnixNano() == defaultClasses[j].CreationTimestamp.UnixNano() {
    62  			return defaultClasses[i].Name < defaultClasses[j].Name
    63  		}
    64  		return defaultClasses[i].CreationTimestamp.UnixNano() > defaultClasses[j].CreationTimestamp.UnixNano()
    65  	})
    66  	if len(defaultClasses) > 1 {
    67  		klog.V(4).Infof("%d default StorageClasses were found, choosing: %s", len(defaultClasses), defaultClasses[0].Name)
    68  	}
    69  
    70  	return defaultClasses[0], nil
    71  }
    72  
    73  // IsDefaultAnnotation returns a boolean if the default storage class
    74  // annotation is set
    75  // TODO: remove Beta when no longer needed
    76  func IsDefaultAnnotation(obj metav1.ObjectMeta) bool {
    77  	if obj.Annotations[IsDefaultStorageClassAnnotation] == "true" {
    78  		return true
    79  	}
    80  	if obj.Annotations[BetaIsDefaultStorageClassAnnotation] == "true" {
    81  		return true
    82  	}
    83  
    84  	return false
    85  }
    86  

View as plain text