strategy.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 persistentvolume
  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. "k8s.io/kubernetes/pkg/api/legacyscheme"
  25. pvutil "k8s.io/kubernetes/pkg/api/persistentvolume"
  26. api "k8s.io/kubernetes/pkg/apis/core"
  27. "k8s.io/kubernetes/pkg/apis/core/validation"
  28. volumevalidation "k8s.io/kubernetes/pkg/volume/validation"
  29. )
  30. // persistentvolumeStrategy implements behavior for PersistentVolume objects
  31. type persistentvolumeStrategy struct {
  32. runtime.ObjectTyper
  33. names.NameGenerator
  34. }
  35. // Strategy is the default logic that applies when creating and updating PersistentVolume
  36. // objects via the REST API.
  37. var Strategy = persistentvolumeStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
  38. func (persistentvolumeStrategy) NamespaceScoped() bool {
  39. return false
  40. }
  41. // ResetBeforeCreate clears the Status field which is not allowed to be set by end users on creation.
  42. func (persistentvolumeStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
  43. pv := obj.(*api.PersistentVolume)
  44. pv.Status = api.PersistentVolumeStatus{}
  45. pvutil.DropDisabledFields(&pv.Spec, nil)
  46. }
  47. func (persistentvolumeStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
  48. persistentvolume := obj.(*api.PersistentVolume)
  49. errorList := validation.ValidatePersistentVolume(persistentvolume)
  50. return append(errorList, volumevalidation.ValidatePersistentVolume(persistentvolume)...)
  51. }
  52. // Canonicalize normalizes the object after validation.
  53. func (persistentvolumeStrategy) Canonicalize(obj runtime.Object) {
  54. }
  55. func (persistentvolumeStrategy) AllowCreateOnUpdate() bool {
  56. return false
  57. }
  58. // PrepareForUpdate sets the Status fields which is not allowed to be set by an end user updating a PV
  59. func (persistentvolumeStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  60. newPv := obj.(*api.PersistentVolume)
  61. oldPv := old.(*api.PersistentVolume)
  62. newPv.Status = oldPv.Status
  63. pvutil.DropDisabledFields(&newPv.Spec, &oldPv.Spec)
  64. }
  65. func (persistentvolumeStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  66. newPv := obj.(*api.PersistentVolume)
  67. errorList := validation.ValidatePersistentVolume(newPv)
  68. errorList = append(errorList, volumevalidation.ValidatePersistentVolume(newPv)...)
  69. return append(errorList, validation.ValidatePersistentVolumeUpdate(newPv, old.(*api.PersistentVolume))...)
  70. }
  71. func (persistentvolumeStrategy) AllowUnconditionalUpdate() bool {
  72. return true
  73. }
  74. type persistentvolumeStatusStrategy struct {
  75. persistentvolumeStrategy
  76. }
  77. var StatusStrategy = persistentvolumeStatusStrategy{Strategy}
  78. // PrepareForUpdate sets the Spec field which is not allowed to be changed when updating a PV's Status
  79. func (persistentvolumeStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  80. newPv := obj.(*api.PersistentVolume)
  81. oldPv := old.(*api.PersistentVolume)
  82. newPv.Spec = oldPv.Spec
  83. }
  84. func (persistentvolumeStatusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  85. return validation.ValidatePersistentVolumeStatusUpdate(obj.(*api.PersistentVolume), old.(*api.PersistentVolume))
  86. }
  87. // GetAttrs returns labels and fields of a given object for filtering purposes.
  88. func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, error) {
  89. persistentvolumeObj, ok := obj.(*api.PersistentVolume)
  90. if !ok {
  91. return nil, nil, fmt.Errorf("not a persistentvolume")
  92. }
  93. return labels.Set(persistentvolumeObj.Labels), PersistentVolumeToSelectableFields(persistentvolumeObj), nil
  94. }
  95. // MatchPersistentVolume returns a generic matcher for a given label and field selector.
  96. func MatchPersistentVolumes(label labels.Selector, field fields.Selector) storage.SelectionPredicate {
  97. return storage.SelectionPredicate{
  98. Label: label,
  99. Field: field,
  100. GetAttrs: GetAttrs,
  101. }
  102. }
  103. // PersistentVolumeToSelectableFields returns a field set that represents the object
  104. func PersistentVolumeToSelectableFields(persistentvolume *api.PersistentVolume) fields.Set {
  105. objectMetaFieldsSet := generic.ObjectMetaFieldsSet(&persistentvolume.ObjectMeta, false)
  106. specificFieldsSet := fields.Set{
  107. // This is a bug, but we need to support it for backward compatibility.
  108. "name": persistentvolume.Name,
  109. }
  110. return generic.MergeFieldsSets(objectMetaFieldsSet, specificFieldsSet)
  111. }