strategy.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 poddisruptionbudget
  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/policy"
  22. "k8s.io/kubernetes/pkg/apis/policy/validation"
  23. )
  24. // podDisruptionBudgetStrategy implements verification logic for PodDisruptionBudgets.
  25. type podDisruptionBudgetStrategy struct {
  26. runtime.ObjectTyper
  27. names.NameGenerator
  28. }
  29. // Strategy is the default logic that applies when creating and updating PodDisruptionBudget objects.
  30. var Strategy = podDisruptionBudgetStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
  31. // NamespaceScoped returns true because all PodDisruptionBudget' need to be within a namespace.
  32. func (podDisruptionBudgetStrategy) NamespaceScoped() bool {
  33. return true
  34. }
  35. // PrepareForCreate clears the status of an PodDisruptionBudget before creation.
  36. func (podDisruptionBudgetStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
  37. podDisruptionBudget := obj.(*policy.PodDisruptionBudget)
  38. // create cannot set status
  39. podDisruptionBudget.Status = policy.PodDisruptionBudgetStatus{}
  40. podDisruptionBudget.Generation = 1
  41. }
  42. // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
  43. func (podDisruptionBudgetStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  44. newPodDisruptionBudget := obj.(*policy.PodDisruptionBudget)
  45. oldPodDisruptionBudget := old.(*policy.PodDisruptionBudget)
  46. // Update is not allowed to set status
  47. newPodDisruptionBudget.Status = oldPodDisruptionBudget.Status
  48. // Any changes to the spec increment the generation number, any changes to the
  49. // status should reflect the generation number of the corresponding object.
  50. // See metav1.ObjectMeta description for more information on Generation.
  51. if !apiequality.Semantic.DeepEqual(oldPodDisruptionBudget.Spec, newPodDisruptionBudget.Spec) {
  52. newPodDisruptionBudget.Generation = oldPodDisruptionBudget.Generation + 1
  53. }
  54. }
  55. // Validate validates a new PodDisruptionBudget.
  56. func (podDisruptionBudgetStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
  57. podDisruptionBudget := obj.(*policy.PodDisruptionBudget)
  58. return validation.ValidatePodDisruptionBudget(podDisruptionBudget)
  59. }
  60. // Canonicalize normalizes the object after validation.
  61. func (podDisruptionBudgetStrategy) Canonicalize(obj runtime.Object) {
  62. }
  63. // AllowCreateOnUpdate is true for PodDisruptionBudget; this means you may create one with a PUT request.
  64. func (podDisruptionBudgetStrategy) AllowCreateOnUpdate() bool {
  65. return false
  66. }
  67. // ValidateUpdate is the default update validation for an end user.
  68. func (podDisruptionBudgetStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  69. return validation.ValidatePodDisruptionBudget(obj.(*policy.PodDisruptionBudget))
  70. }
  71. // AllowUnconditionalUpdate is the default update policy for PodDisruptionBudget objects. Status update should
  72. // only be allowed if version match.
  73. func (podDisruptionBudgetStrategy) AllowUnconditionalUpdate() bool {
  74. return false
  75. }
  76. type podDisruptionBudgetStatusStrategy struct {
  77. podDisruptionBudgetStrategy
  78. }
  79. // StatusStrategy is the default logic invoked when updating object status.
  80. var StatusStrategy = podDisruptionBudgetStatusStrategy{Strategy}
  81. // PrepareForUpdate clears fields that are not allowed to be set by end users on update of status
  82. func (podDisruptionBudgetStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  83. newPodDisruptionBudget := obj.(*policy.PodDisruptionBudget)
  84. oldPodDisruptionBudget := old.(*policy.PodDisruptionBudget)
  85. // status changes are not allowed to update spec
  86. newPodDisruptionBudget.Spec = oldPodDisruptionBudget.Spec
  87. }
  88. // ValidateUpdate is the default update validation for an end user updating status
  89. func (podDisruptionBudgetStatusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  90. // TODO: Validate status updates.
  91. return field.ErrorList{}
  92. // return validation.ValidatePodDisruptionBudgetStatusUpdate(obj.(*policy.PodDisruptionBudget), old.(*policy.PodDisruptionBudget))
  93. }