strategy.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Copyright 2019 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 prioritylevelconfiguration
  14. import (
  15. "context"
  16. apiequality "k8s.io/apimachinery/pkg/api/equality"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. "k8s.io/apimachinery/pkg/util/validation/field"
  19. "k8s.io/apiserver/pkg/storage/names"
  20. "k8s.io/kubernetes/pkg/api/legacyscheme"
  21. "k8s.io/kubernetes/pkg/apis/flowcontrol"
  22. "k8s.io/kubernetes/pkg/apis/flowcontrol/validation"
  23. )
  24. // priorityLevelConfigurationStrategy implements verification logic for priority level configurations.
  25. type priorityLevelConfigurationStrategy struct {
  26. runtime.ObjectTyper
  27. names.NameGenerator
  28. }
  29. // Strategy is the default logic that applies when creating and updating priority level configuration objects.
  30. var Strategy = priorityLevelConfigurationStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
  31. // NamespaceScoped returns false because all PriorityClasses are global.
  32. func (priorityLevelConfigurationStrategy) NamespaceScoped() bool {
  33. return false
  34. }
  35. // PrepareForCreate clears the status of a priority-level-configuration before creation.
  36. func (priorityLevelConfigurationStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
  37. pl := obj.(*flowcontrol.PriorityLevelConfiguration)
  38. pl.Status = flowcontrol.PriorityLevelConfigurationStatus{}
  39. pl.Generation = 1
  40. }
  41. // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
  42. func (priorityLevelConfigurationStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  43. newPriorityLevelConfiguration := obj.(*flowcontrol.PriorityLevelConfiguration)
  44. oldPriorityLevelConfiguration := old.(*flowcontrol.PriorityLevelConfiguration)
  45. // Spec updates bump the generation so that we can distinguish between status updates.
  46. if !apiequality.Semantic.DeepEqual(newPriorityLevelConfiguration.Spec, oldPriorityLevelConfiguration.Spec) {
  47. newPriorityLevelConfiguration.Generation = oldPriorityLevelConfiguration.Generation + 1
  48. }
  49. newPriorityLevelConfiguration.Status = oldPriorityLevelConfiguration.Status
  50. }
  51. // Validate validates a new priority-level.
  52. func (priorityLevelConfigurationStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
  53. return validation.ValidatePriorityLevelConfiguration(obj.(*flowcontrol.PriorityLevelConfiguration))
  54. }
  55. // Canonicalize normalizes the object after validation.
  56. func (priorityLevelConfigurationStrategy) Canonicalize(obj runtime.Object) {
  57. }
  58. func (priorityLevelConfigurationStrategy) AllowUnconditionalUpdate() bool {
  59. return true
  60. }
  61. // AllowCreateOnUpdate is false for priority-level-configurations; this means a POST is needed to create one.
  62. func (priorityLevelConfigurationStrategy) AllowCreateOnUpdate() bool {
  63. return false
  64. }
  65. // ValidateUpdate is the default update validation for an end user.
  66. func (priorityLevelConfigurationStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  67. return validation.ValidatePriorityLevelConfiguration(obj.(*flowcontrol.PriorityLevelConfiguration))
  68. }
  69. type priorityLevelConfigurationStatusStrategy struct {
  70. priorityLevelConfigurationStrategy
  71. }
  72. // StatusStrategy is the default logic that applies when updating priority level configuration objects' status.
  73. var StatusStrategy = priorityLevelConfigurationStatusStrategy{Strategy}
  74. func (priorityLevelConfigurationStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  75. newPriorityLevelConfiguration := obj.(*flowcontrol.PriorityLevelConfiguration)
  76. oldPriorityLevelConfiguration := old.(*flowcontrol.PriorityLevelConfiguration)
  77. newPriorityLevelConfiguration.ObjectMeta = oldPriorityLevelConfiguration.ObjectMeta
  78. newPriorityLevelConfiguration.Spec = oldPriorityLevelConfiguration.Spec
  79. }
  80. func (priorityLevelConfigurationStatusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  81. return validation.ValidatePriorityLevelConfigurationStatusUpdate(old.(*flowcontrol.PriorityLevelConfiguration), obj.(*flowcontrol.PriorityLevelConfiguration))
  82. }