...

Source file src/k8s.io/kubectl/pkg/util/storage/storage.go

Documentation: k8s.io/kubectl/pkg/util/storage

     1  /*
     2  Copyright 2018 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 storage
    18  
    19  import (
    20  	"k8s.io/api/core/v1"
    21  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    22  	"strings"
    23  )
    24  
    25  // TODO(yue9944882): Remove this helper package once it's copied to k/api
    26  
    27  // IsDefaultStorageClassAnnotation represents a StorageClass annotation that
    28  // marks a class as the default StorageClass
    29  const IsDefaultStorageClassAnnotation = "storageclass.kubernetes.io/is-default-class"
    30  
    31  // BetaIsDefaultStorageClassAnnotation is the beta version of IsDefaultStorageClassAnnotation.
    32  const BetaIsDefaultStorageClassAnnotation = "storageclass.beta.kubernetes.io/is-default-class"
    33  
    34  // IsDefaultAnnotationText returns a pretty Yes/No String if
    35  // the annotation is set
    36  func IsDefaultAnnotationText(obj metav1.ObjectMeta) string {
    37  	if obj.Annotations[IsDefaultStorageClassAnnotation] == "true" {
    38  		return "Yes"
    39  	}
    40  	if obj.Annotations[BetaIsDefaultStorageClassAnnotation] == "true" {
    41  		return "Yes"
    42  	}
    43  
    44  	return "No"
    45  }
    46  
    47  // GetAccessModesAsString returns a string representation of an array of access modes.
    48  // modes, when present, are always in the same order: RWO,ROX,RWX,RWOP.
    49  func GetAccessModesAsString(modes []v1.PersistentVolumeAccessMode) string {
    50  	modes = removeDuplicateAccessModes(modes)
    51  	modesStr := []string{}
    52  	if ContainsAccessMode(modes, v1.ReadWriteOnce) {
    53  		modesStr = append(modesStr, "RWO")
    54  	}
    55  	if ContainsAccessMode(modes, v1.ReadOnlyMany) {
    56  		modesStr = append(modesStr, "ROX")
    57  	}
    58  	if ContainsAccessMode(modes, v1.ReadWriteMany) {
    59  		modesStr = append(modesStr, "RWX")
    60  	}
    61  	if ContainsAccessMode(modes, v1.ReadWriteOncePod) {
    62  		modesStr = append(modesStr, "RWOP")
    63  	}
    64  	return strings.Join(modesStr, ",")
    65  }
    66  
    67  // removeDuplicateAccessModes returns an array of access modes without any duplicates
    68  func removeDuplicateAccessModes(modes []v1.PersistentVolumeAccessMode) []v1.PersistentVolumeAccessMode {
    69  	accessModes := []v1.PersistentVolumeAccessMode{}
    70  	for _, m := range modes {
    71  		if !ContainsAccessMode(accessModes, m) {
    72  			accessModes = append(accessModes, m)
    73  		}
    74  	}
    75  	return accessModes
    76  }
    77  
    78  func ContainsAccessMode(modes []v1.PersistentVolumeAccessMode, mode v1.PersistentVolumeAccessMode) bool {
    79  	for _, m := range modes {
    80  		if m == mode {
    81  			return true
    82  		}
    83  	}
    84  	return false
    85  }
    86  
    87  // GetPersistentVolumeClass returns StorageClassName.
    88  func GetPersistentVolumeClass(volume *v1.PersistentVolume) string {
    89  	// Use beta annotation first
    90  	if class, found := volume.Annotations[v1.BetaStorageClassAnnotation]; found {
    91  		return class
    92  	}
    93  
    94  	return volume.Spec.StorageClassName
    95  }
    96  
    97  // GetPersistentVolumeClaimClass returns StorageClassName. If no storage class was
    98  // requested, it returns "".
    99  func GetPersistentVolumeClaimClass(claim *v1.PersistentVolumeClaim) string {
   100  	// Use beta annotation first
   101  	if class, found := claim.Annotations[v1.BetaStorageClassAnnotation]; found {
   102  		return class
   103  	}
   104  
   105  	if claim.Spec.StorageClassName != nil {
   106  		return *claim.Spec.StorageClassName
   107  	}
   108  
   109  	return ""
   110  }
   111  

View as plain text