util.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. Copyright 2017 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 persistentvolume
  14. import (
  15. corev1 "k8s.io/api/core/v1"
  16. )
  17. func getClaimRefNamespace(pv *corev1.PersistentVolume) string {
  18. if pv.Spec.ClaimRef != nil {
  19. return pv.Spec.ClaimRef.Namespace
  20. }
  21. return ""
  22. }
  23. // Visitor is called with each object's namespace and name, and returns true if visiting should continue
  24. type Visitor func(namespace, name string, kubeletVisible bool) (shouldContinue bool)
  25. // VisitPVSecretNames invokes the visitor function with the name of every secret
  26. // referenced by the PV spec. If visitor returns false, visiting is short-circuited.
  27. // Returns true if visiting completed, false if visiting was short-circuited.
  28. func VisitPVSecretNames(pv *corev1.PersistentVolume, visitor Visitor) bool {
  29. source := &pv.Spec.PersistentVolumeSource
  30. switch {
  31. case source.AzureFile != nil:
  32. if source.AzureFile.SecretNamespace != nil && len(*source.AzureFile.SecretNamespace) > 0 {
  33. if len(source.AzureFile.SecretName) > 0 && !visitor(*source.AzureFile.SecretNamespace, source.AzureFile.SecretName, true /* kubeletVisible */) {
  34. return false
  35. }
  36. } else {
  37. if len(source.AzureFile.SecretName) > 0 && !visitor(getClaimRefNamespace(pv), source.AzureFile.SecretName, true /* kubeletVisible */) {
  38. return false
  39. }
  40. }
  41. return true
  42. case source.CephFS != nil:
  43. if source.CephFS.SecretRef != nil {
  44. // previously persisted PV objects use claimRef namespace
  45. ns := getClaimRefNamespace(pv)
  46. if len(source.CephFS.SecretRef.Namespace) > 0 {
  47. // use the secret namespace if namespace is set
  48. ns = source.CephFS.SecretRef.Namespace
  49. }
  50. if !visitor(ns, source.CephFS.SecretRef.Name, true /* kubeletVisible */) {
  51. return false
  52. }
  53. }
  54. case source.Cinder != nil:
  55. if source.Cinder.SecretRef != nil && !visitor(source.Cinder.SecretRef.Namespace, source.Cinder.SecretRef.Name, true /* kubeletVisible */) {
  56. return false
  57. }
  58. case source.FlexVolume != nil:
  59. if source.FlexVolume.SecretRef != nil {
  60. // previously persisted PV objects use claimRef namespace
  61. ns := getClaimRefNamespace(pv)
  62. if len(source.FlexVolume.SecretRef.Namespace) > 0 {
  63. // use the secret namespace if namespace is set
  64. ns = source.FlexVolume.SecretRef.Namespace
  65. }
  66. if !visitor(ns, source.FlexVolume.SecretRef.Name, true /* kubeletVisible */) {
  67. return false
  68. }
  69. }
  70. case source.RBD != nil:
  71. if source.RBD.SecretRef != nil {
  72. // previously persisted PV objects use claimRef namespace
  73. ns := getClaimRefNamespace(pv)
  74. if len(source.RBD.SecretRef.Namespace) > 0 {
  75. // use the secret namespace if namespace is set
  76. ns = source.RBD.SecretRef.Namespace
  77. }
  78. if !visitor(ns, source.RBD.SecretRef.Name, true /* kubeletVisible */) {
  79. return false
  80. }
  81. }
  82. case source.ScaleIO != nil:
  83. if source.ScaleIO.SecretRef != nil {
  84. ns := getClaimRefNamespace(pv)
  85. if source.ScaleIO.SecretRef != nil && len(source.ScaleIO.SecretRef.Namespace) > 0 {
  86. ns = source.ScaleIO.SecretRef.Namespace
  87. }
  88. if !visitor(ns, source.ScaleIO.SecretRef.Name, true /* kubeletVisible */) {
  89. return false
  90. }
  91. }
  92. case source.ISCSI != nil:
  93. if source.ISCSI.SecretRef != nil {
  94. // previously persisted PV objects use claimRef namespace
  95. ns := getClaimRefNamespace(pv)
  96. if len(source.ISCSI.SecretRef.Namespace) > 0 {
  97. // use the secret namespace if namespace is set
  98. ns = source.ISCSI.SecretRef.Namespace
  99. }
  100. if !visitor(ns, source.ISCSI.SecretRef.Name, true /* kubeletVisible */) {
  101. return false
  102. }
  103. }
  104. case source.StorageOS != nil:
  105. if source.StorageOS.SecretRef != nil && !visitor(source.StorageOS.SecretRef.Namespace, source.StorageOS.SecretRef.Name, true /* kubeletVisible */) {
  106. return false
  107. }
  108. case source.CSI != nil:
  109. if source.CSI.ControllerPublishSecretRef != nil {
  110. if !visitor(source.CSI.ControllerPublishSecretRef.Namespace, source.CSI.ControllerPublishSecretRef.Name, false /* kubeletVisible */) {
  111. return false
  112. }
  113. }
  114. if source.CSI.ControllerExpandSecretRef != nil {
  115. if !visitor(source.CSI.ControllerExpandSecretRef.Namespace, source.CSI.ControllerExpandSecretRef.Name, false /* kubeletVisible */) {
  116. return false
  117. }
  118. }
  119. if source.CSI.NodePublishSecretRef != nil {
  120. if !visitor(source.CSI.NodePublishSecretRef.Namespace, source.CSI.NodePublishSecretRef.Name, true /* kubeletVisible */) {
  121. return false
  122. }
  123. }
  124. if source.CSI.NodeStageSecretRef != nil {
  125. if !visitor(source.CSI.NodeStageSecretRef.Namespace, source.CSI.NodeStageSecretRef.Name, true /* kubeletVisible */) {
  126. return false
  127. }
  128. }
  129. }
  130. return true
  131. }