util.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. utilfeature "k8s.io/apiserver/pkg/util/feature"
  16. api "k8s.io/kubernetes/pkg/apis/core"
  17. "k8s.io/kubernetes/pkg/features"
  18. )
  19. // DropDisabledFields removes disabled fields from the pv spec.
  20. // This should be called from PrepareForCreate/PrepareForUpdate for all resources containing a pv spec.
  21. func DropDisabledFields(pvSpec *api.PersistentVolumeSpec, oldPVSpec *api.PersistentVolumeSpec) {
  22. if !utilfeature.DefaultFeatureGate.Enabled(features.BlockVolume) && !volumeModeInUse(oldPVSpec) {
  23. pvSpec.VolumeMode = nil
  24. }
  25. if !utilfeature.DefaultFeatureGate.Enabled(features.ExpandCSIVolumes) && !hasExpansionSecrets(oldPVSpec) {
  26. if pvSpec.CSI != nil {
  27. pvSpec.CSI.ControllerExpandSecretRef = nil
  28. }
  29. }
  30. }
  31. func hasExpansionSecrets(oldPVSpec *api.PersistentVolumeSpec) bool {
  32. if oldPVSpec == nil || oldPVSpec.CSI == nil {
  33. return false
  34. }
  35. if oldPVSpec.CSI.ControllerExpandSecretRef != nil {
  36. return true
  37. }
  38. return false
  39. }
  40. func volumeModeInUse(oldPVSpec *api.PersistentVolumeSpec) bool {
  41. if oldPVSpec == nil {
  42. return false
  43. }
  44. if oldPVSpec.VolumeMode != nil {
  45. return true
  46. }
  47. return false
  48. }