horizontal_pod_autoscaling.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 autoscaling
  14. import (
  15. "time"
  16. "k8s.io/apimachinery/pkg/runtime/schema"
  17. "k8s.io/kubernetes/test/e2e/common"
  18. "k8s.io/kubernetes/test/e2e/framework"
  19. "github.com/onsi/ginkgo"
  20. )
  21. // These tests don't seem to be running properly in parallel: issue: #20338.
  22. //
  23. var _ = SIGDescribe("[HPA] Horizontal pod autoscaling (scale resource: CPU)", func() {
  24. var rc *common.ResourceConsumer
  25. f := framework.NewDefaultFramework("horizontal-pod-autoscaling")
  26. titleUp := "Should scale from 1 pod to 3 pods and from 3 to 5"
  27. titleDown := "Should scale from 5 pods to 3 pods and from 3 to 1"
  28. SIGDescribe("[Serial] [Slow] Deployment", func() {
  29. // CPU tests via deployments
  30. ginkgo.It(titleUp, func() {
  31. scaleUp("test-deployment", common.KindDeployment, false, rc, f)
  32. })
  33. ginkgo.It(titleDown, func() {
  34. scaleDown("test-deployment", common.KindDeployment, false, rc, f)
  35. })
  36. })
  37. SIGDescribe("[Serial] [Slow] ReplicaSet", func() {
  38. // CPU tests via ReplicaSets
  39. ginkgo.It(titleUp, func() {
  40. scaleUp("rs", common.KindReplicaSet, false, rc, f)
  41. })
  42. ginkgo.It(titleDown, func() {
  43. scaleDown("rs", common.KindReplicaSet, false, rc, f)
  44. })
  45. })
  46. // These tests take ~20 minutes each.
  47. SIGDescribe("[Serial] [Slow] ReplicationController", func() {
  48. // CPU tests via replication controllers
  49. ginkgo.It(titleUp+" and verify decision stability", func() {
  50. scaleUp("rc", common.KindRC, true, rc, f)
  51. })
  52. ginkgo.It(titleDown+" and verify decision stability", func() {
  53. scaleDown("rc", common.KindRC, true, rc, f)
  54. })
  55. })
  56. SIGDescribe("ReplicationController light", func() {
  57. ginkgo.It("Should scale from 1 pod to 2 pods", func() {
  58. scaleTest := &HPAScaleTest{
  59. initPods: 1,
  60. totalInitialCPUUsage: 150,
  61. perPodCPURequest: 200,
  62. targetCPUUtilizationPercent: 50,
  63. minPods: 1,
  64. maxPods: 2,
  65. firstScale: 2,
  66. }
  67. scaleTest.run("rc-light", common.KindRC, rc, f)
  68. })
  69. ginkgo.It("Should scale from 2 pods to 1 pod", func() {
  70. scaleTest := &HPAScaleTest{
  71. initPods: 2,
  72. totalInitialCPUUsage: 50,
  73. perPodCPURequest: 200,
  74. targetCPUUtilizationPercent: 50,
  75. minPods: 1,
  76. maxPods: 2,
  77. firstScale: 1,
  78. }
  79. scaleTest.run("rc-light", common.KindRC, rc, f)
  80. })
  81. })
  82. })
  83. // HPAScaleTest struct is used by the scale(...) function.
  84. type HPAScaleTest struct {
  85. initPods int
  86. totalInitialCPUUsage int
  87. perPodCPURequest int64
  88. targetCPUUtilizationPercent int32
  89. minPods int32
  90. maxPods int32
  91. firstScale int
  92. firstScaleStasis time.Duration
  93. cpuBurst int
  94. secondScale int32
  95. secondScaleStasis time.Duration
  96. }
  97. // run is a method which runs an HPA lifecycle, from a starting state, to an expected
  98. // The initial state is defined by the initPods parameter.
  99. // The first state change is due to the CPU being consumed initially, which HPA responds to by changing pod counts.
  100. // The second state change (optional) is due to the CPU burst parameter, which HPA again responds to.
  101. // TODO The use of 3 states is arbitrary, we could eventually make this test handle "n" states once this test stabilizes.
  102. func (scaleTest *HPAScaleTest) run(name string, kind schema.GroupVersionKind, rc *common.ResourceConsumer, f *framework.Framework) {
  103. const timeToWait = 15 * time.Minute
  104. rc = common.NewDynamicResourceConsumer(name, f.Namespace.Name, kind, scaleTest.initPods, scaleTest.totalInitialCPUUsage, 0, 0, scaleTest.perPodCPURequest, 200, f.ClientSet, f.ScalesGetter)
  105. defer rc.CleanUp()
  106. hpa := common.CreateCPUHorizontalPodAutoscaler(rc, scaleTest.targetCPUUtilizationPercent, scaleTest.minPods, scaleTest.maxPods)
  107. defer common.DeleteHorizontalPodAutoscaler(rc, hpa.Name)
  108. rc.WaitForReplicas(scaleTest.firstScale, timeToWait)
  109. if scaleTest.firstScaleStasis > 0 {
  110. rc.EnsureDesiredReplicasInRange(scaleTest.firstScale, scaleTest.firstScale+1, scaleTest.firstScaleStasis, hpa.Name)
  111. }
  112. if scaleTest.cpuBurst > 0 && scaleTest.secondScale > 0 {
  113. rc.ConsumeCPU(scaleTest.cpuBurst)
  114. rc.WaitForReplicas(int(scaleTest.secondScale), timeToWait)
  115. }
  116. }
  117. func scaleUp(name string, kind schema.GroupVersionKind, checkStability bool, rc *common.ResourceConsumer, f *framework.Framework) {
  118. stasis := 0 * time.Minute
  119. if checkStability {
  120. stasis = 10 * time.Minute
  121. }
  122. scaleTest := &HPAScaleTest{
  123. initPods: 1,
  124. totalInitialCPUUsage: 250,
  125. perPodCPURequest: 500,
  126. targetCPUUtilizationPercent: 20,
  127. minPods: 1,
  128. maxPods: 5,
  129. firstScale: 3,
  130. firstScaleStasis: stasis,
  131. cpuBurst: 700,
  132. secondScale: 5,
  133. }
  134. scaleTest.run(name, kind, rc, f)
  135. }
  136. func scaleDown(name string, kind schema.GroupVersionKind, checkStability bool, rc *common.ResourceConsumer, f *framework.Framework) {
  137. stasis := 0 * time.Minute
  138. if checkStability {
  139. stasis = 10 * time.Minute
  140. }
  141. scaleTest := &HPAScaleTest{
  142. initPods: 5,
  143. totalInitialCPUUsage: 325,
  144. perPodCPURequest: 500,
  145. targetCPUUtilizationPercent: 30,
  146. minPods: 1,
  147. maxPods: 5,
  148. firstScale: 3,
  149. firstScaleStasis: stasis,
  150. cpuBurst: 10,
  151. secondScale: 1,
  152. }
  153. scaleTest.run(name, kind, rc, f)
  154. }