defaults.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. Copyright 2018 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 v2beta2
  14. import (
  15. autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2"
  16. v1 "k8s.io/api/core/v1"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. "k8s.io/kubernetes/pkg/apis/autoscaling"
  19. )
  20. var (
  21. // These constants repeats previous HPA behavior
  22. scaleUpLimitPercent int32 = 100
  23. scaleUpLimitMinimumPods int32 = 4
  24. scaleUpPeriod int32 = 15
  25. scaleUpStabilizationSeconds int32
  26. maxPolicy = autoscalingv2beta2.MaxPolicySelect
  27. defaultHPAScaleUpRules = autoscalingv2beta2.HPAScalingRules{
  28. StabilizationWindowSeconds: &scaleUpStabilizationSeconds,
  29. SelectPolicy: &maxPolicy,
  30. Policies: []autoscalingv2beta2.HPAScalingPolicy{
  31. {
  32. Type: autoscalingv2beta2.PodsScalingPolicy,
  33. Value: scaleUpLimitMinimumPods,
  34. PeriodSeconds: scaleUpPeriod,
  35. },
  36. {
  37. Type: autoscalingv2beta2.PercentScalingPolicy,
  38. Value: scaleUpLimitPercent,
  39. PeriodSeconds: scaleUpPeriod,
  40. },
  41. },
  42. }
  43. scaleDownPeriod int32 = 15
  44. // Currently we can set the downscaleStabilizationWindow from the command line
  45. // So we can not rewrite the command line option from here
  46. scaleDownStabilizationSeconds *int32 = nil
  47. scaleDownLimitPercent int32 = 100
  48. defaultHPAScaleDownRules = autoscalingv2beta2.HPAScalingRules{
  49. StabilizationWindowSeconds: scaleDownStabilizationSeconds,
  50. SelectPolicy: &maxPolicy,
  51. Policies: []autoscalingv2beta2.HPAScalingPolicy{
  52. {
  53. Type: autoscalingv2beta2.PercentScalingPolicy,
  54. Value: scaleDownLimitPercent,
  55. PeriodSeconds: scaleDownPeriod,
  56. },
  57. },
  58. }
  59. )
  60. func addDefaultingFuncs(scheme *runtime.Scheme) error {
  61. return RegisterDefaults(scheme)
  62. }
  63. func SetDefaults_HorizontalPodAutoscaler(obj *autoscalingv2beta2.HorizontalPodAutoscaler) {
  64. if obj.Spec.MinReplicas == nil {
  65. minReplicas := int32(1)
  66. obj.Spec.MinReplicas = &minReplicas
  67. }
  68. if len(obj.Spec.Metrics) == 0 {
  69. utilizationDefaultVal := int32(autoscaling.DefaultCPUUtilization)
  70. obj.Spec.Metrics = []autoscalingv2beta2.MetricSpec{
  71. {
  72. Type: autoscalingv2beta2.ResourceMetricSourceType,
  73. Resource: &autoscalingv2beta2.ResourceMetricSource{
  74. Name: v1.ResourceCPU,
  75. Target: autoscalingv2beta2.MetricTarget{
  76. Type: autoscalingv2beta2.UtilizationMetricType,
  77. AverageUtilization: &utilizationDefaultVal,
  78. },
  79. },
  80. },
  81. }
  82. }
  83. SetDefaults_HorizontalPodAutoscalerBehavior(obj)
  84. }
  85. // SetDefaults_HorizontalPodAutoscalerBehavior fills the behavior if it is not null
  86. func SetDefaults_HorizontalPodAutoscalerBehavior(obj *autoscalingv2beta2.HorizontalPodAutoscaler) {
  87. // if behavior is specified, we should fill all the 'nil' values with the default ones
  88. if obj.Spec.Behavior != nil {
  89. obj.Spec.Behavior.ScaleUp = GenerateHPAScaleUpRules(obj.Spec.Behavior.ScaleUp)
  90. obj.Spec.Behavior.ScaleDown = GenerateHPAScaleDownRules(obj.Spec.Behavior.ScaleDown)
  91. }
  92. }
  93. // GenerateHPAScaleUpRules returns a fully-initialized HPAScalingRules value
  94. // We guarantee that no pointer in the structure will have the 'nil' value
  95. func GenerateHPAScaleUpRules(scalingRules *autoscalingv2beta2.HPAScalingRules) *autoscalingv2beta2.HPAScalingRules {
  96. defaultScalingRules := defaultHPAScaleUpRules.DeepCopy()
  97. return copyHPAScalingRules(scalingRules, defaultScalingRules)
  98. }
  99. // GenerateHPAScaleDownRules returns a fully-initialized HPAScalingRules value
  100. // We guarantee that no pointer in the structure will have the 'nil' value
  101. // EXCEPT StabilizationWindowSeconds, for reasoning check the comment for defaultHPAScaleDownRules
  102. func GenerateHPAScaleDownRules(scalingRules *autoscalingv2beta2.HPAScalingRules) *autoscalingv2beta2.HPAScalingRules {
  103. defaultScalingRules := defaultHPAScaleDownRules.DeepCopy()
  104. return copyHPAScalingRules(scalingRules, defaultScalingRules)
  105. }
  106. // copyHPAScalingRules copies all non-`nil` fields in HPA constraint structure
  107. func copyHPAScalingRules(from, to *autoscalingv2beta2.HPAScalingRules) *autoscalingv2beta2.HPAScalingRules {
  108. if from == nil {
  109. return to
  110. }
  111. if from.SelectPolicy != nil {
  112. to.SelectPolicy = from.SelectPolicy
  113. }
  114. if from.StabilizationWindowSeconds != nil {
  115. to.StabilizationWindowSeconds = from.StabilizationWindowSeconds
  116. }
  117. if from.Policies != nil {
  118. to.Policies = from.Policies
  119. }
  120. return to
  121. }