strategy.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. Copyright 2019 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 csidriver
  14. import (
  15. "context"
  16. "k8s.io/apimachinery/pkg/runtime"
  17. "k8s.io/apimachinery/pkg/util/validation/field"
  18. "k8s.io/apiserver/pkg/storage/names"
  19. utilfeature "k8s.io/apiserver/pkg/util/feature"
  20. "k8s.io/kubernetes/pkg/api/legacyscheme"
  21. "k8s.io/kubernetes/pkg/apis/storage"
  22. "k8s.io/kubernetes/pkg/apis/storage/validation"
  23. "k8s.io/kubernetes/pkg/features"
  24. )
  25. // csiDriverStrategy implements behavior for CSIDriver objects
  26. type csiDriverStrategy struct {
  27. runtime.ObjectTyper
  28. names.NameGenerator
  29. }
  30. // Strategy is the default logic that applies when creating and updating
  31. // CSIDriver objects via the REST API.
  32. var Strategy = csiDriverStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
  33. func (csiDriverStrategy) NamespaceScoped() bool {
  34. return false
  35. }
  36. // PrepareForCreate clears the VolumeLifecycleModes field if the corresponding feature is disabled.
  37. func (csiDriverStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
  38. if !utilfeature.DefaultFeatureGate.Enabled(features.CSIInlineVolume) {
  39. csiDriver := obj.(*storage.CSIDriver)
  40. csiDriver.Spec.VolumeLifecycleModes = nil
  41. }
  42. }
  43. func (csiDriverStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
  44. csiDriver := obj.(*storage.CSIDriver)
  45. errs := validation.ValidateCSIDriver(csiDriver)
  46. errs = append(errs, validation.ValidateCSIDriver(csiDriver)...)
  47. return errs
  48. }
  49. // Canonicalize normalizes the object after validation.
  50. func (csiDriverStrategy) Canonicalize(obj runtime.Object) {
  51. }
  52. func (csiDriverStrategy) AllowCreateOnUpdate() bool {
  53. return false
  54. }
  55. // PrepareForUpdate clears the VolumeLifecycleModes field if the corresponding feature is disabled and
  56. // existing object does not already have that field set. This allows the field to remain when
  57. // downgrading to a version that has the feature disabled.
  58. func (csiDriverStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  59. if old.(*storage.CSIDriver).Spec.VolumeLifecycleModes == nil &&
  60. !utilfeature.DefaultFeatureGate.Enabled(features.CSIInlineVolume) {
  61. newCSIDriver := obj.(*storage.CSIDriver)
  62. newCSIDriver.Spec.VolumeLifecycleModes = nil
  63. }
  64. }
  65. func (csiDriverStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  66. newCSIDriverObj := obj.(*storage.CSIDriver)
  67. oldCSIDriverObj := old.(*storage.CSIDriver)
  68. errorList := validation.ValidateCSIDriver(newCSIDriverObj)
  69. return append(errorList, validation.ValidateCSIDriverUpdate(newCSIDriverObj, oldCSIDriverObj)...)
  70. }
  71. func (csiDriverStrategy) AllowUnconditionalUpdate() bool {
  72. return false
  73. }