util.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 persistentvolumeclaim
  14. import (
  15. utilfeature "k8s.io/apiserver/pkg/util/feature"
  16. "k8s.io/kubernetes/pkg/apis/core"
  17. "k8s.io/kubernetes/pkg/features"
  18. )
  19. const (
  20. pvc string = "PersistentVolumeClaim"
  21. volumeSnapshot string = "VolumeSnapshot"
  22. )
  23. // DropDisabledFields removes disabled fields from the pvc spec.
  24. // This should be called from PrepareForCreate/PrepareForUpdate for all resources containing a pvc spec.
  25. func DropDisabledFields(pvcSpec, oldPVCSpec *core.PersistentVolumeClaimSpec) {
  26. if !utilfeature.DefaultFeatureGate.Enabled(features.BlockVolume) && !volumeModeInUse(oldPVCSpec) {
  27. pvcSpec.VolumeMode = nil
  28. }
  29. if !dataSourceIsEnabled(pvcSpec) && !dataSourceInUse(oldPVCSpec) {
  30. pvcSpec.DataSource = nil
  31. }
  32. }
  33. func volumeModeInUse(oldPVCSpec *core.PersistentVolumeClaimSpec) bool {
  34. if oldPVCSpec == nil {
  35. return false
  36. }
  37. if oldPVCSpec.VolumeMode != nil {
  38. return true
  39. }
  40. return false
  41. }
  42. func dataSourceInUse(oldPVCSpec *core.PersistentVolumeClaimSpec) bool {
  43. if oldPVCSpec == nil {
  44. return false
  45. }
  46. if oldPVCSpec.DataSource != nil {
  47. return true
  48. }
  49. return false
  50. }
  51. func dataSourceIsEnabled(pvcSpec *core.PersistentVolumeClaimSpec) bool {
  52. if pvcSpec.DataSource != nil {
  53. apiGroup := ""
  54. if pvcSpec.DataSource.APIGroup != nil {
  55. apiGroup = *pvcSpec.DataSource.APIGroup
  56. }
  57. if utilfeature.DefaultFeatureGate.Enabled(features.VolumePVCDataSource) &&
  58. pvcSpec.DataSource.Kind == pvc &&
  59. apiGroup == "" {
  60. return true
  61. }
  62. if utilfeature.DefaultFeatureGate.Enabled(features.VolumeSnapshotDataSource) &&
  63. pvcSpec.DataSource.Kind == volumeSnapshot &&
  64. apiGroup == "snapshot.storage.k8s.io" {
  65. return true
  66. }
  67. }
  68. return false
  69. }