strategy.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. Copyright 2014 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. "context"
  16. "fmt"
  17. "k8s.io/apimachinery/pkg/fields"
  18. "k8s.io/apimachinery/pkg/labels"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. "k8s.io/apimachinery/pkg/util/validation/field"
  21. "k8s.io/apiserver/pkg/registry/generic"
  22. "k8s.io/apiserver/pkg/storage"
  23. "k8s.io/apiserver/pkg/storage/names"
  24. utilfeature "k8s.io/apiserver/pkg/util/feature"
  25. "k8s.io/kubernetes/pkg/api/legacyscheme"
  26. pvcutil "k8s.io/kubernetes/pkg/api/persistentvolumeclaim"
  27. api "k8s.io/kubernetes/pkg/apis/core"
  28. "k8s.io/kubernetes/pkg/apis/core/validation"
  29. "k8s.io/kubernetes/pkg/features"
  30. )
  31. // persistentvolumeclaimStrategy implements behavior for PersistentVolumeClaim objects
  32. type persistentvolumeclaimStrategy struct {
  33. runtime.ObjectTyper
  34. names.NameGenerator
  35. }
  36. // Strategy is the default logic that applies when creating and updating PersistentVolumeClaim
  37. // objects via the REST API.
  38. var Strategy = persistentvolumeclaimStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
  39. func (persistentvolumeclaimStrategy) NamespaceScoped() bool {
  40. return true
  41. }
  42. // PrepareForCreate clears the Status field which is not allowed to be set by end users on creation.
  43. func (persistentvolumeclaimStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
  44. pvc := obj.(*api.PersistentVolumeClaim)
  45. pvc.Status = api.PersistentVolumeClaimStatus{}
  46. pvcutil.DropDisabledFields(&pvc.Spec, nil)
  47. }
  48. func (persistentvolumeclaimStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
  49. pvc := obj.(*api.PersistentVolumeClaim)
  50. return validation.ValidatePersistentVolumeClaim(pvc)
  51. }
  52. // Canonicalize normalizes the object after validation.
  53. func (persistentvolumeclaimStrategy) Canonicalize(obj runtime.Object) {
  54. }
  55. func (persistentvolumeclaimStrategy) AllowCreateOnUpdate() bool {
  56. return false
  57. }
  58. // PrepareForUpdate sets the Status field which is not allowed to be set by end users on update
  59. func (persistentvolumeclaimStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  60. newPvc := obj.(*api.PersistentVolumeClaim)
  61. oldPvc := old.(*api.PersistentVolumeClaim)
  62. newPvc.Status = oldPvc.Status
  63. pvcutil.DropDisabledFields(&newPvc.Spec, &oldPvc.Spec)
  64. }
  65. func (persistentvolumeclaimStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  66. errorList := validation.ValidatePersistentVolumeClaim(obj.(*api.PersistentVolumeClaim))
  67. return append(errorList, validation.ValidatePersistentVolumeClaimUpdate(obj.(*api.PersistentVolumeClaim), old.(*api.PersistentVolumeClaim))...)
  68. }
  69. func (persistentvolumeclaimStrategy) AllowUnconditionalUpdate() bool {
  70. return true
  71. }
  72. type persistentvolumeclaimStatusStrategy struct {
  73. persistentvolumeclaimStrategy
  74. }
  75. var StatusStrategy = persistentvolumeclaimStatusStrategy{Strategy}
  76. // PrepareForUpdate sets the Spec field which is not allowed to be changed when updating a PV's Status
  77. func (persistentvolumeclaimStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  78. newPv := obj.(*api.PersistentVolumeClaim)
  79. oldPv := old.(*api.PersistentVolumeClaim)
  80. newPv.Spec = oldPv.Spec
  81. if !utilfeature.DefaultFeatureGate.Enabled(features.ExpandPersistentVolumes) && oldPv.Status.Conditions == nil {
  82. newPv.Status.Conditions = nil
  83. }
  84. }
  85. func (persistentvolumeclaimStatusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  86. return validation.ValidatePersistentVolumeClaimStatusUpdate(obj.(*api.PersistentVolumeClaim), old.(*api.PersistentVolumeClaim))
  87. }
  88. // GetAttrs returns labels and fields of a given object for filtering purposes.
  89. func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, error) {
  90. persistentvolumeclaimObj, ok := obj.(*api.PersistentVolumeClaim)
  91. if !ok {
  92. return nil, nil, fmt.Errorf("not a persistentvolumeclaim")
  93. }
  94. return labels.Set(persistentvolumeclaimObj.Labels), PersistentVolumeClaimToSelectableFields(persistentvolumeclaimObj), nil
  95. }
  96. // MatchPersistentVolumeClaim returns a generic matcher for a given label and field selector.
  97. func MatchPersistentVolumeClaim(label labels.Selector, field fields.Selector) storage.SelectionPredicate {
  98. return storage.SelectionPredicate{
  99. Label: label,
  100. Field: field,
  101. GetAttrs: GetAttrs,
  102. }
  103. }
  104. // PersistentVolumeClaimToSelectableFields returns a field set that represents the object
  105. func PersistentVolumeClaimToSelectableFields(persistentvolumeclaim *api.PersistentVolumeClaim) fields.Set {
  106. objectMetaFieldsSet := generic.ObjectMetaFieldsSet(&persistentvolumeclaim.ObjectMeta, true)
  107. specificFieldsSet := fields.Set{
  108. // This is a bug, but we need to support it for backward compatibility.
  109. "name": persistentvolumeclaim.Name,
  110. }
  111. return generic.MergeFieldsSets(objectMetaFieldsSet, specificFieldsSet)
  112. }