strategy.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 resourcequota
  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. "k8s.io/kubernetes/pkg/api/legacyscheme"
  20. resourcequotautil "k8s.io/kubernetes/pkg/api/resourcequota"
  21. api "k8s.io/kubernetes/pkg/apis/core"
  22. "k8s.io/kubernetes/pkg/apis/core/validation"
  23. )
  24. // resourcequotaStrategy implements behavior for ResourceQuota objects
  25. type resourcequotaStrategy struct {
  26. runtime.ObjectTyper
  27. names.NameGenerator
  28. }
  29. // Strategy is the default logic that applies when creating and updating ResourceQuota
  30. // objects via the REST API.
  31. var Strategy = resourcequotaStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
  32. // NamespaceScoped is true for resourcequotas.
  33. func (resourcequotaStrategy) NamespaceScoped() bool {
  34. return true
  35. }
  36. // PrepareForCreate clears fields that are not allowed to be set by end users on creation.
  37. func (resourcequotaStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
  38. resourcequota := obj.(*api.ResourceQuota)
  39. resourcequota.Status = api.ResourceQuotaStatus{}
  40. resourcequotautil.DropDisabledFields(&resourcequota.Spec, nil)
  41. }
  42. // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
  43. func (resourcequotaStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  44. newResourcequota := obj.(*api.ResourceQuota)
  45. oldResourcequota := old.(*api.ResourceQuota)
  46. newResourcequota.Status = oldResourcequota.Status
  47. resourcequotautil.DropDisabledFields(&newResourcequota.Spec, &oldResourcequota.Spec)
  48. }
  49. // Validate validates a new resourcequota.
  50. func (resourcequotaStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
  51. resourcequota := obj.(*api.ResourceQuota)
  52. return validation.ValidateResourceQuota(resourcequota)
  53. }
  54. // Canonicalize normalizes the object after validation.
  55. func (resourcequotaStrategy) Canonicalize(obj runtime.Object) {
  56. }
  57. // AllowCreateOnUpdate is false for resourcequotas.
  58. func (resourcequotaStrategy) AllowCreateOnUpdate() bool {
  59. return false
  60. }
  61. // ValidateUpdate is the default update validation for an end user.
  62. func (resourcequotaStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  63. errorList := validation.ValidateResourceQuota(obj.(*api.ResourceQuota))
  64. return append(errorList, validation.ValidateResourceQuotaUpdate(obj.(*api.ResourceQuota), old.(*api.ResourceQuota))...)
  65. }
  66. func (resourcequotaStrategy) AllowUnconditionalUpdate() bool {
  67. return true
  68. }
  69. type resourcequotaStatusStrategy struct {
  70. resourcequotaStrategy
  71. }
  72. // StatusStrategy is the default logic invoked when updating object status.
  73. var StatusStrategy = resourcequotaStatusStrategy{Strategy}
  74. func (resourcequotaStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  75. newResourcequota := obj.(*api.ResourceQuota)
  76. oldResourcequota := old.(*api.ResourceQuota)
  77. newResourcequota.Spec = oldResourcequota.Spec
  78. }
  79. func (resourcequotaStatusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  80. return validation.ValidateResourceQuotaStatusUpdate(obj.(*api.ResourceQuota), old.(*api.ResourceQuota))
  81. }