strategy.go 3.5 KB

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