strategy.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. Copyright 2015 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 horizontalpodautoscaler
  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. "k8s.io/kubernetes/pkg/apis/autoscaling"
  21. "k8s.io/kubernetes/pkg/apis/autoscaling/validation"
  22. )
  23. // autoscalerStrategy implements behavior for HorizontalPodAutoscalers
  24. type autoscalerStrategy struct {
  25. runtime.ObjectTyper
  26. names.NameGenerator
  27. }
  28. // Strategy is the default logic that applies when creating and updating HorizontalPodAutoscaler
  29. // objects via the REST API.
  30. var Strategy = autoscalerStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
  31. // NamespaceScoped is true for autoscaler.
  32. func (autoscalerStrategy) NamespaceScoped() bool {
  33. return true
  34. }
  35. // PrepareForCreate clears fields that are not allowed to be set by end users on creation.
  36. func (autoscalerStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
  37. newHPA := obj.(*autoscaling.HorizontalPodAutoscaler)
  38. // create cannot set status
  39. newHPA.Status = autoscaling.HorizontalPodAutoscalerStatus{}
  40. }
  41. // Validate validates a new autoscaler.
  42. func (autoscalerStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
  43. autoscaler := obj.(*autoscaling.HorizontalPodAutoscaler)
  44. return validation.ValidateHorizontalPodAutoscaler(autoscaler)
  45. }
  46. // Canonicalize normalizes the object after validation.
  47. func (autoscalerStrategy) Canonicalize(obj runtime.Object) {
  48. }
  49. // AllowCreateOnUpdate is false for autoscalers.
  50. func (autoscalerStrategy) AllowCreateOnUpdate() bool {
  51. return false
  52. }
  53. // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
  54. func (autoscalerStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  55. newHPA := obj.(*autoscaling.HorizontalPodAutoscaler)
  56. oldHPA := old.(*autoscaling.HorizontalPodAutoscaler)
  57. // Update is not allowed to set status
  58. newHPA.Status = oldHPA.Status
  59. }
  60. // ValidateUpdate is the default update validation for an end user.
  61. func (autoscalerStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  62. return validation.ValidateHorizontalPodAutoscalerUpdate(obj.(*autoscaling.HorizontalPodAutoscaler), old.(*autoscaling.HorizontalPodAutoscaler))
  63. }
  64. func (autoscalerStrategy) AllowUnconditionalUpdate() bool {
  65. return true
  66. }
  67. type autoscalerStatusStrategy struct {
  68. autoscalerStrategy
  69. }
  70. // StatusStrategy is the default logic invoked when updating object status.
  71. var StatusStrategy = autoscalerStatusStrategy{Strategy}
  72. func (autoscalerStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  73. newAutoscaler := obj.(*autoscaling.HorizontalPodAutoscaler)
  74. oldAutoscaler := old.(*autoscaling.HorizontalPodAutoscaler)
  75. // status changes are not allowed to update spec
  76. newAutoscaler.Spec = oldAutoscaler.Spec
  77. }
  78. func (autoscalerStatusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  79. return validation.ValidateHorizontalPodAutoscalerStatusUpdate(obj.(*autoscaling.HorizontalPodAutoscaler), old.(*autoscaling.HorizontalPodAutoscaler))
  80. }