strategy.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 volumeattachment
  14. import (
  15. "context"
  16. storageapiv1beta1 "k8s.io/api/storage/v1beta1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. "k8s.io/apimachinery/pkg/runtime/schema"
  20. "k8s.io/apimachinery/pkg/util/validation/field"
  21. genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
  22. "k8s.io/apiserver/pkg/storage/names"
  23. utilfeature "k8s.io/apiserver/pkg/util/feature"
  24. "k8s.io/kubernetes/pkg/api/legacyscheme"
  25. "k8s.io/kubernetes/pkg/apis/storage"
  26. "k8s.io/kubernetes/pkg/apis/storage/validation"
  27. "k8s.io/kubernetes/pkg/features"
  28. )
  29. // volumeAttachmentStrategy implements behavior for VolumeAttachment objects
  30. type volumeAttachmentStrategy struct {
  31. runtime.ObjectTyper
  32. names.NameGenerator
  33. }
  34. // Strategy is the default logic that applies when creating and updating
  35. // VolumeAttachment objects via the REST API.
  36. var Strategy = volumeAttachmentStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
  37. func (volumeAttachmentStrategy) NamespaceScoped() bool {
  38. return false
  39. }
  40. // ResetBeforeCreate clears the Status field which is not allowed to be set by end users on creation.
  41. func (volumeAttachmentStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
  42. var groupVersion schema.GroupVersion
  43. if requestInfo, found := genericapirequest.RequestInfoFrom(ctx); found {
  44. groupVersion = schema.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion}
  45. }
  46. volumeAttachment := obj.(*storage.VolumeAttachment)
  47. switch groupVersion {
  48. case storageapiv1beta1.SchemeGroupVersion:
  49. // allow modification of status for v1beta1
  50. default:
  51. volumeAttachment.Status = storage.VolumeAttachmentStatus{}
  52. }
  53. if !utilfeature.DefaultFeatureGate.Enabled(features.CSIMigration) {
  54. volumeAttachment.Spec.Source.InlineVolumeSpec = nil
  55. }
  56. }
  57. func (volumeAttachmentStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
  58. volumeAttachment := obj.(*storage.VolumeAttachment)
  59. errs := validation.ValidateVolumeAttachment(volumeAttachment)
  60. var groupVersion schema.GroupVersion
  61. if requestInfo, found := genericapirequest.RequestInfoFrom(ctx); found {
  62. groupVersion = schema.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion}
  63. }
  64. switch groupVersion {
  65. case storageapiv1beta1.SchemeGroupVersion:
  66. // no extra validation
  67. default:
  68. // tighten up validation of newly created v1 attachments
  69. errs = append(errs, validation.ValidateVolumeAttachmentV1(volumeAttachment)...)
  70. }
  71. return errs
  72. }
  73. // Canonicalize normalizes the object after validation.
  74. func (volumeAttachmentStrategy) Canonicalize(obj runtime.Object) {
  75. }
  76. func (volumeAttachmentStrategy) AllowCreateOnUpdate() bool {
  77. return false
  78. }
  79. // PrepareForUpdate sets the Status fields which is not allowed to be set by an end user updating a VolumeAttachment
  80. func (volumeAttachmentStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  81. var groupVersion schema.GroupVersion
  82. if requestInfo, found := genericapirequest.RequestInfoFrom(ctx); found {
  83. groupVersion = schema.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion}
  84. }
  85. newVolumeAttachment := obj.(*storage.VolumeAttachment)
  86. oldVolumeAttachment := old.(*storage.VolumeAttachment)
  87. switch groupVersion {
  88. case storageapiv1beta1.SchemeGroupVersion:
  89. // allow modification of Status via main resource for v1beta1
  90. default:
  91. newVolumeAttachment.Status = oldVolumeAttachment.Status
  92. // No need to increment Generation because we don't allow updates to spec
  93. }
  94. if !utilfeature.DefaultFeatureGate.Enabled(features.CSIMigration) && oldVolumeAttachment.Spec.Source.InlineVolumeSpec == nil {
  95. newVolumeAttachment.Spec.Source.InlineVolumeSpec = nil
  96. }
  97. }
  98. func (volumeAttachmentStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  99. newVolumeAttachmentObj := obj.(*storage.VolumeAttachment)
  100. oldVolumeAttachmentObj := old.(*storage.VolumeAttachment)
  101. errorList := validation.ValidateVolumeAttachment(newVolumeAttachmentObj)
  102. return append(errorList, validation.ValidateVolumeAttachmentUpdate(newVolumeAttachmentObj, oldVolumeAttachmentObj)...)
  103. }
  104. func (volumeAttachmentStrategy) AllowUnconditionalUpdate() bool {
  105. return false
  106. }
  107. // volumeAttachmentStatusStrategy implements behavior for VolumeAttachmentStatus subresource
  108. type volumeAttachmentStatusStrategy struct {
  109. volumeAttachmentStrategy
  110. }
  111. // StatusStrategy is the default logic that applies when creating and updating
  112. // VolumeAttachmentStatus subresource via the REST API.
  113. var StatusStrategy = volumeAttachmentStatusStrategy{Strategy}
  114. // PrepareForUpdate sets the Status fields which is not allowed to be set by an end user updating a VolumeAttachment
  115. func (volumeAttachmentStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  116. newVolumeAttachment := obj.(*storage.VolumeAttachment)
  117. oldVolumeAttachment := old.(*storage.VolumeAttachment)
  118. newVolumeAttachment.Spec = oldVolumeAttachment.Spec
  119. metav1.ResetObjectMetaForStatus(&newVolumeAttachment.ObjectMeta, &oldVolumeAttachment.ObjectMeta)
  120. }