storage.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package storage
  14. import (
  15. "k8s.io/api/core/v1"
  16. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  17. "strings"
  18. )
  19. // TODO(yue9944882): Remove this helper package once it's copied to k/api
  20. // IsDefaultStorageClassAnnotation represents a StorageClass annotation that
  21. // marks a class as the default StorageClass
  22. const IsDefaultStorageClassAnnotation = "storageclass.kubernetes.io/is-default-class"
  23. // BetaIsDefaultStorageClassAnnotation is the beta version of IsDefaultStorageClassAnnotation.
  24. const BetaIsDefaultStorageClassAnnotation = "storageclass.beta.kubernetes.io/is-default-class"
  25. // IsDefaultAnnotationText returns a pretty Yes/No String if
  26. // the annotation is set
  27. func IsDefaultAnnotationText(obj metav1.ObjectMeta) string {
  28. if obj.Annotations[IsDefaultStorageClassAnnotation] == "true" {
  29. return "Yes"
  30. }
  31. if obj.Annotations[BetaIsDefaultStorageClassAnnotation] == "true" {
  32. return "Yes"
  33. }
  34. return "No"
  35. }
  36. // GetAccessModesAsString returns a string representation of an array of access modes.
  37. // modes, when present, are always in the same order: RWO,ROX,RWX.
  38. func GetAccessModesAsString(modes []v1.PersistentVolumeAccessMode) string {
  39. modes = removeDuplicateAccessModes(modes)
  40. modesStr := []string{}
  41. if containsAccessMode(modes, v1.ReadWriteOnce) {
  42. modesStr = append(modesStr, "RWO")
  43. }
  44. if containsAccessMode(modes, v1.ReadOnlyMany) {
  45. modesStr = append(modesStr, "ROX")
  46. }
  47. if containsAccessMode(modes, v1.ReadWriteMany) {
  48. modesStr = append(modesStr, "RWX")
  49. }
  50. return strings.Join(modesStr, ",")
  51. }
  52. // removeDuplicateAccessModes returns an array of access modes without any duplicates
  53. func removeDuplicateAccessModes(modes []v1.PersistentVolumeAccessMode) []v1.PersistentVolumeAccessMode {
  54. accessModes := []v1.PersistentVolumeAccessMode{}
  55. for _, m := range modes {
  56. if !containsAccessMode(accessModes, m) {
  57. accessModes = append(accessModes, m)
  58. }
  59. }
  60. return accessModes
  61. }
  62. func containsAccessMode(modes []v1.PersistentVolumeAccessMode, mode v1.PersistentVolumeAccessMode) bool {
  63. for _, m := range modes {
  64. if m == mode {
  65. return true
  66. }
  67. }
  68. return false
  69. }
  70. // GetPersistentVolumeClass returns StorageClassName.
  71. func GetPersistentVolumeClass(volume *v1.PersistentVolume) string {
  72. // Use beta annotation first
  73. if class, found := volume.Annotations[v1.BetaStorageClassAnnotation]; found {
  74. return class
  75. }
  76. return volume.Spec.StorageClassName
  77. }
  78. // GetPersistentVolumeClaimClass returns StorageClassName. If no storage class was
  79. // requested, it returns "".
  80. func GetPersistentVolumeClaimClass(claim *v1.PersistentVolumeClaim) string {
  81. // Use beta annotation first
  82. if class, found := claim.Annotations[v1.BetaStorageClassAnnotation]; found {
  83. return class
  84. }
  85. if claim.Spec.StorageClassName != nil {
  86. return *claim.Spec.StorageClassName
  87. }
  88. return ""
  89. }