horizontal_test.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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 podautoscaler
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "io"
  18. "math"
  19. "sync"
  20. "testing"
  21. "time"
  22. "k8s.io/kubernetes/pkg/api"
  23. "k8s.io/kubernetes/pkg/api/resource"
  24. "k8s.io/kubernetes/pkg/api/unversioned"
  25. "k8s.io/kubernetes/pkg/api/v1"
  26. _ "k8s.io/kubernetes/pkg/apimachinery/registered"
  27. "k8s.io/kubernetes/pkg/apis/autoscaling"
  28. "k8s.io/kubernetes/pkg/apis/extensions"
  29. "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
  30. unversionedcore "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/unversioned"
  31. "k8s.io/kubernetes/pkg/client/record"
  32. "k8s.io/kubernetes/pkg/client/restclient"
  33. "k8s.io/kubernetes/pkg/client/testing/core"
  34. "k8s.io/kubernetes/pkg/controller/podautoscaler/metrics"
  35. "k8s.io/kubernetes/pkg/runtime"
  36. "k8s.io/kubernetes/pkg/watch"
  37. heapster "k8s.io/heapster/metrics/api/v1/types"
  38. metrics_api "k8s.io/heapster/metrics/apis/metrics/v1alpha1"
  39. "github.com/stretchr/testify/assert"
  40. )
  41. func (w fakeResponseWrapper) DoRaw() ([]byte, error) {
  42. return w.raw, nil
  43. }
  44. func (w fakeResponseWrapper) Stream() (io.ReadCloser, error) {
  45. return nil, nil
  46. }
  47. func newFakeResponseWrapper(raw []byte) fakeResponseWrapper {
  48. return fakeResponseWrapper{raw: raw}
  49. }
  50. type fakeResponseWrapper struct {
  51. raw []byte
  52. }
  53. type fakeResource struct {
  54. name string
  55. apiVersion string
  56. kind string
  57. }
  58. type testCase struct {
  59. sync.Mutex
  60. minReplicas int32
  61. maxReplicas int32
  62. initialReplicas int32
  63. desiredReplicas int32
  64. // CPU target utilization as a percentage of the requested resources.
  65. CPUTarget int32
  66. CPUCurrent int32
  67. verifyCPUCurrent bool
  68. reportedLevels []uint64
  69. reportedCPURequests []resource.Quantity
  70. cmTarget *extensions.CustomMetricTargetList
  71. scaleUpdated bool
  72. statusUpdated bool
  73. eventCreated bool
  74. verifyEvents bool
  75. useMetricsApi bool
  76. // Channel with names of HPA objects which we have reconciled.
  77. processed chan string
  78. // Target resource information.
  79. resource *fakeResource
  80. }
  81. // Needs to be called under a lock.
  82. func (tc *testCase) computeCPUCurrent() {
  83. if len(tc.reportedLevels) != len(tc.reportedCPURequests) || len(tc.reportedLevels) == 0 {
  84. return
  85. }
  86. reported := 0
  87. for _, r := range tc.reportedLevels {
  88. reported += int(r)
  89. }
  90. requested := 0
  91. for _, req := range tc.reportedCPURequests {
  92. requested += int(req.MilliValue())
  93. }
  94. tc.CPUCurrent = int32(100 * reported / requested)
  95. }
  96. func (tc *testCase) prepareTestClient(t *testing.T) *fake.Clientset {
  97. namespace := "test-namespace"
  98. hpaName := "test-hpa"
  99. podNamePrefix := "test-pod"
  100. selector := &unversioned.LabelSelector{
  101. MatchLabels: map[string]string{"name": podNamePrefix},
  102. }
  103. tc.Lock()
  104. tc.scaleUpdated = false
  105. tc.statusUpdated = false
  106. tc.eventCreated = false
  107. tc.processed = make(chan string, 100)
  108. tc.computeCPUCurrent()
  109. // TODO(madhusudancs): HPA only supports resources in extensions/v1beta1 right now. Add
  110. // tests for "v1" replicationcontrollers when HPA adds support for cross-group scale.
  111. if tc.resource == nil {
  112. tc.resource = &fakeResource{
  113. name: "test-rc",
  114. apiVersion: "extensions/v1beta1",
  115. kind: "replicationcontrollers",
  116. }
  117. }
  118. tc.Unlock()
  119. fakeClient := &fake.Clientset{}
  120. fakeClient.AddReactor("list", "horizontalpodautoscalers", func(action core.Action) (handled bool, ret runtime.Object, err error) {
  121. tc.Lock()
  122. defer tc.Unlock()
  123. obj := &autoscaling.HorizontalPodAutoscalerList{
  124. Items: []autoscaling.HorizontalPodAutoscaler{
  125. {
  126. ObjectMeta: api.ObjectMeta{
  127. Name: hpaName,
  128. Namespace: namespace,
  129. SelfLink: "experimental/v1/namespaces/" + namespace + "/horizontalpodautoscalers/" + hpaName,
  130. },
  131. Spec: autoscaling.HorizontalPodAutoscalerSpec{
  132. ScaleTargetRef: autoscaling.CrossVersionObjectReference{
  133. Kind: tc.resource.kind,
  134. Name: tc.resource.name,
  135. APIVersion: tc.resource.apiVersion,
  136. },
  137. MinReplicas: &tc.minReplicas,
  138. MaxReplicas: tc.maxReplicas,
  139. },
  140. Status: autoscaling.HorizontalPodAutoscalerStatus{
  141. CurrentReplicas: tc.initialReplicas,
  142. DesiredReplicas: tc.initialReplicas,
  143. },
  144. },
  145. },
  146. }
  147. if tc.CPUTarget > 0.0 {
  148. obj.Items[0].Spec.TargetCPUUtilizationPercentage = &tc.CPUTarget
  149. }
  150. if tc.cmTarget != nil {
  151. b, err := json.Marshal(tc.cmTarget)
  152. if err != nil {
  153. t.Fatalf("Failed to marshal cm: %v", err)
  154. }
  155. obj.Items[0].Annotations = make(map[string]string)
  156. obj.Items[0].Annotations[HpaCustomMetricsTargetAnnotationName] = string(b)
  157. }
  158. return true, obj, nil
  159. })
  160. fakeClient.AddReactor("get", "replicationcontrollers", func(action core.Action) (handled bool, ret runtime.Object, err error) {
  161. tc.Lock()
  162. defer tc.Unlock()
  163. obj := &extensions.Scale{
  164. ObjectMeta: api.ObjectMeta{
  165. Name: tc.resource.name,
  166. Namespace: namespace,
  167. },
  168. Spec: extensions.ScaleSpec{
  169. Replicas: tc.initialReplicas,
  170. },
  171. Status: extensions.ScaleStatus{
  172. Replicas: tc.initialReplicas,
  173. Selector: selector,
  174. },
  175. }
  176. return true, obj, nil
  177. })
  178. fakeClient.AddReactor("get", "deployments", func(action core.Action) (handled bool, ret runtime.Object, err error) {
  179. tc.Lock()
  180. defer tc.Unlock()
  181. obj := &extensions.Scale{
  182. ObjectMeta: api.ObjectMeta{
  183. Name: tc.resource.name,
  184. Namespace: namespace,
  185. },
  186. Spec: extensions.ScaleSpec{
  187. Replicas: tc.initialReplicas,
  188. },
  189. Status: extensions.ScaleStatus{
  190. Replicas: tc.initialReplicas,
  191. Selector: selector,
  192. },
  193. }
  194. return true, obj, nil
  195. })
  196. fakeClient.AddReactor("get", "replicasets", func(action core.Action) (handled bool, ret runtime.Object, err error) {
  197. tc.Lock()
  198. defer tc.Unlock()
  199. obj := &extensions.Scale{
  200. ObjectMeta: api.ObjectMeta{
  201. Name: tc.resource.name,
  202. Namespace: namespace,
  203. },
  204. Spec: extensions.ScaleSpec{
  205. Replicas: tc.initialReplicas,
  206. },
  207. Status: extensions.ScaleStatus{
  208. Replicas: tc.initialReplicas,
  209. Selector: selector,
  210. },
  211. }
  212. return true, obj, nil
  213. })
  214. fakeClient.AddReactor("list", "pods", func(action core.Action) (handled bool, ret runtime.Object, err error) {
  215. tc.Lock()
  216. defer tc.Unlock()
  217. obj := &api.PodList{}
  218. for i := 0; i < len(tc.reportedCPURequests); i++ {
  219. podName := fmt.Sprintf("%s-%d", podNamePrefix, i)
  220. pod := api.Pod{
  221. Status: api.PodStatus{
  222. Phase: api.PodRunning,
  223. },
  224. ObjectMeta: api.ObjectMeta{
  225. Name: podName,
  226. Namespace: namespace,
  227. Labels: map[string]string{
  228. "name": podNamePrefix,
  229. },
  230. },
  231. Spec: api.PodSpec{
  232. Containers: []api.Container{
  233. {
  234. Resources: api.ResourceRequirements{
  235. Requests: api.ResourceList{
  236. api.ResourceCPU: tc.reportedCPURequests[i],
  237. },
  238. },
  239. },
  240. },
  241. },
  242. }
  243. obj.Items = append(obj.Items, pod)
  244. }
  245. return true, obj, nil
  246. })
  247. fakeClient.AddProxyReactor("services", func(action core.Action) (handled bool, ret restclient.ResponseWrapper, err error) {
  248. tc.Lock()
  249. defer tc.Unlock()
  250. var heapsterRawMemResponse []byte
  251. if tc.useMetricsApi {
  252. metrics := metrics_api.PodMetricsList{}
  253. for i, cpu := range tc.reportedLevels {
  254. podMetric := metrics_api.PodMetrics{
  255. ObjectMeta: v1.ObjectMeta{
  256. Name: fmt.Sprintf("%s-%d", podNamePrefix, i),
  257. Namespace: namespace,
  258. },
  259. Timestamp: unversioned.Time{Time: time.Now()},
  260. Containers: []metrics_api.ContainerMetrics{
  261. {
  262. Name: "container",
  263. Usage: v1.ResourceList{
  264. v1.ResourceCPU: *resource.NewMilliQuantity(
  265. int64(cpu),
  266. resource.DecimalSI),
  267. v1.ResourceMemory: *resource.NewQuantity(
  268. int64(1024*1024),
  269. resource.BinarySI),
  270. },
  271. },
  272. },
  273. }
  274. metrics.Items = append(metrics.Items, podMetric)
  275. }
  276. heapsterRawMemResponse, _ = json.Marshal(&metrics)
  277. } else {
  278. timestamp := time.Now()
  279. metrics := heapster.MetricResultList{}
  280. for _, level := range tc.reportedLevels {
  281. metric := heapster.MetricResult{
  282. Metrics: []heapster.MetricPoint{{Timestamp: timestamp, Value: level, FloatValue: nil}},
  283. LatestTimestamp: timestamp,
  284. }
  285. metrics.Items = append(metrics.Items, metric)
  286. }
  287. heapsterRawMemResponse, _ = json.Marshal(&metrics)
  288. }
  289. return true, newFakeResponseWrapper(heapsterRawMemResponse), nil
  290. })
  291. fakeClient.AddReactor("update", "replicationcontrollers", func(action core.Action) (handled bool, ret runtime.Object, err error) {
  292. tc.Lock()
  293. defer tc.Unlock()
  294. obj := action.(core.UpdateAction).GetObject().(*extensions.Scale)
  295. replicas := action.(core.UpdateAction).GetObject().(*extensions.Scale).Spec.Replicas
  296. assert.Equal(t, tc.desiredReplicas, replicas)
  297. tc.scaleUpdated = true
  298. return true, obj, nil
  299. })
  300. fakeClient.AddReactor("update", "deployments", func(action core.Action) (handled bool, ret runtime.Object, err error) {
  301. tc.Lock()
  302. defer tc.Unlock()
  303. obj := action.(core.UpdateAction).GetObject().(*extensions.Scale)
  304. replicas := action.(core.UpdateAction).GetObject().(*extensions.Scale).Spec.Replicas
  305. assert.Equal(t, tc.desiredReplicas, replicas)
  306. tc.scaleUpdated = true
  307. return true, obj, nil
  308. })
  309. fakeClient.AddReactor("update", "replicasets", func(action core.Action) (handled bool, ret runtime.Object, err error) {
  310. tc.Lock()
  311. defer tc.Unlock()
  312. obj := action.(core.UpdateAction).GetObject().(*extensions.Scale)
  313. replicas := action.(core.UpdateAction).GetObject().(*extensions.Scale).Spec.Replicas
  314. assert.Equal(t, tc.desiredReplicas, replicas)
  315. tc.scaleUpdated = true
  316. return true, obj, nil
  317. })
  318. fakeClient.AddReactor("update", "horizontalpodautoscalers", func(action core.Action) (handled bool, ret runtime.Object, err error) {
  319. tc.Lock()
  320. defer tc.Unlock()
  321. obj := action.(core.UpdateAction).GetObject().(*autoscaling.HorizontalPodAutoscaler)
  322. assert.Equal(t, namespace, obj.Namespace)
  323. assert.Equal(t, hpaName, obj.Name)
  324. assert.Equal(t, tc.desiredReplicas, obj.Status.DesiredReplicas)
  325. if tc.verifyCPUCurrent {
  326. assert.NotNil(t, obj.Status.CurrentCPUUtilizationPercentage)
  327. assert.Equal(t, tc.CPUCurrent, *obj.Status.CurrentCPUUtilizationPercentage)
  328. }
  329. tc.statusUpdated = true
  330. // Every time we reconcile HPA object we are updating status.
  331. tc.processed <- obj.Name
  332. return true, obj, nil
  333. })
  334. fakeClient.AddReactor("*", "events", func(action core.Action) (handled bool, ret runtime.Object, err error) {
  335. tc.Lock()
  336. defer tc.Unlock()
  337. obj := action.(core.CreateAction).GetObject().(*api.Event)
  338. if tc.verifyEvents {
  339. assert.Equal(t, "SuccessfulRescale", obj.Reason)
  340. assert.Equal(t, fmt.Sprintf("New size: %d; reason: CPU utilization above target", tc.desiredReplicas), obj.Message)
  341. }
  342. tc.eventCreated = true
  343. return true, obj, nil
  344. })
  345. fakeWatch := watch.NewFake()
  346. fakeClient.AddWatchReactor("*", core.DefaultWatchReactor(fakeWatch, nil))
  347. return fakeClient
  348. }
  349. func (tc *testCase) verifyResults(t *testing.T) {
  350. tc.Lock()
  351. defer tc.Unlock()
  352. assert.Equal(t, tc.initialReplicas != tc.desiredReplicas, tc.scaleUpdated)
  353. assert.True(t, tc.statusUpdated)
  354. if tc.verifyEvents {
  355. assert.Equal(t, tc.initialReplicas != tc.desiredReplicas, tc.eventCreated)
  356. }
  357. }
  358. func (tc *testCase) runTest(t *testing.T) {
  359. testClient := tc.prepareTestClient(t)
  360. metricsClient := metrics.NewHeapsterMetricsClient(testClient, metrics.DefaultHeapsterNamespace, metrics.DefaultHeapsterScheme, metrics.DefaultHeapsterService, metrics.DefaultHeapsterPort)
  361. broadcaster := record.NewBroadcasterForTests(0)
  362. broadcaster.StartRecordingToSink(&unversionedcore.EventSinkImpl{Interface: testClient.Core().Events("")})
  363. recorder := broadcaster.NewRecorder(api.EventSource{Component: "horizontal-pod-autoscaler"})
  364. hpaController := &HorizontalController{
  365. metricsClient: metricsClient,
  366. eventRecorder: recorder,
  367. scaleNamespacer: testClient.Extensions(),
  368. hpaNamespacer: testClient.Autoscaling(),
  369. }
  370. store, frameworkController := newInformer(hpaController, time.Minute)
  371. hpaController.store = store
  372. hpaController.controller = frameworkController
  373. stop := make(chan struct{})
  374. defer close(stop)
  375. go hpaController.Run(stop)
  376. tc.Lock()
  377. if tc.verifyEvents {
  378. tc.Unlock()
  379. // We need to wait for events to be broadcasted (sleep for longer than record.sleepDuration).
  380. time.Sleep(2 * time.Second)
  381. } else {
  382. tc.Unlock()
  383. }
  384. // Wait for HPA to be processed.
  385. <-tc.processed
  386. tc.verifyResults(t)
  387. }
  388. func TestDefaultScaleUpRC(t *testing.T) {
  389. tc := testCase{
  390. minReplicas: 2,
  391. maxReplicas: 6,
  392. initialReplicas: 4,
  393. desiredReplicas: 5,
  394. verifyCPUCurrent: true,
  395. reportedLevels: []uint64{900, 950, 950, 1000},
  396. reportedCPURequests: []resource.Quantity{resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0")},
  397. useMetricsApi: true,
  398. }
  399. tc.runTest(t)
  400. }
  401. func TestDefaultScaleUpDeployment(t *testing.T) {
  402. tc := testCase{
  403. minReplicas: 2,
  404. maxReplicas: 6,
  405. initialReplicas: 4,
  406. desiredReplicas: 5,
  407. verifyCPUCurrent: true,
  408. reportedLevels: []uint64{900, 950, 950, 1000},
  409. reportedCPURequests: []resource.Quantity{resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0")},
  410. useMetricsApi: true,
  411. resource: &fakeResource{
  412. name: "test-dep",
  413. apiVersion: "extensions/v1beta1",
  414. kind: "deployments",
  415. },
  416. }
  417. tc.runTest(t)
  418. }
  419. func TestDefaultScaleUpReplicaSet(t *testing.T) {
  420. tc := testCase{
  421. minReplicas: 2,
  422. maxReplicas: 6,
  423. initialReplicas: 4,
  424. desiredReplicas: 5,
  425. verifyCPUCurrent: true,
  426. reportedLevels: []uint64{900, 950, 950, 1000},
  427. reportedCPURequests: []resource.Quantity{resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0")},
  428. useMetricsApi: true,
  429. resource: &fakeResource{
  430. name: "test-replicaset",
  431. apiVersion: "extensions/v1beta1",
  432. kind: "replicasets",
  433. },
  434. }
  435. tc.runTest(t)
  436. }
  437. func TestScaleUp(t *testing.T) {
  438. tc := testCase{
  439. minReplicas: 2,
  440. maxReplicas: 6,
  441. initialReplicas: 3,
  442. desiredReplicas: 5,
  443. CPUTarget: 30,
  444. verifyCPUCurrent: true,
  445. reportedLevels: []uint64{300, 500, 700},
  446. reportedCPURequests: []resource.Quantity{resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0")},
  447. useMetricsApi: true,
  448. }
  449. tc.runTest(t)
  450. }
  451. func TestScaleUpDeployment(t *testing.T) {
  452. tc := testCase{
  453. minReplicas: 2,
  454. maxReplicas: 6,
  455. initialReplicas: 3,
  456. desiredReplicas: 5,
  457. CPUTarget: 30,
  458. verifyCPUCurrent: true,
  459. reportedLevels: []uint64{300, 500, 700},
  460. reportedCPURequests: []resource.Quantity{resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0")},
  461. useMetricsApi: true,
  462. resource: &fakeResource{
  463. name: "test-dep",
  464. apiVersion: "extensions/v1beta1",
  465. kind: "deployments",
  466. },
  467. }
  468. tc.runTest(t)
  469. }
  470. func TestScaleUpReplicaSet(t *testing.T) {
  471. tc := testCase{
  472. minReplicas: 2,
  473. maxReplicas: 6,
  474. initialReplicas: 3,
  475. desiredReplicas: 5,
  476. CPUTarget: 30,
  477. verifyCPUCurrent: true,
  478. reportedLevels: []uint64{300, 500, 700},
  479. reportedCPURequests: []resource.Quantity{resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0")},
  480. useMetricsApi: true,
  481. resource: &fakeResource{
  482. name: "test-replicaset",
  483. apiVersion: "extensions/v1beta1",
  484. kind: "replicasets",
  485. },
  486. }
  487. tc.runTest(t)
  488. }
  489. func TestScaleUpCM(t *testing.T) {
  490. tc := testCase{
  491. minReplicas: 2,
  492. maxReplicas: 6,
  493. initialReplicas: 3,
  494. desiredReplicas: 4,
  495. CPUTarget: 0,
  496. cmTarget: &extensions.CustomMetricTargetList{
  497. Items: []extensions.CustomMetricTarget{{
  498. Name: "qps",
  499. TargetValue: resource.MustParse("15.0"),
  500. }},
  501. },
  502. reportedLevels: []uint64{20, 10, 30},
  503. reportedCPURequests: []resource.Quantity{resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0")},
  504. }
  505. tc.runTest(t)
  506. }
  507. func TestDefaultScaleDown(t *testing.T) {
  508. tc := testCase{
  509. minReplicas: 2,
  510. maxReplicas: 6,
  511. initialReplicas: 5,
  512. desiredReplicas: 4,
  513. verifyCPUCurrent: true,
  514. reportedLevels: []uint64{400, 500, 600, 700, 800},
  515. reportedCPURequests: []resource.Quantity{resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0")},
  516. useMetricsApi: true,
  517. }
  518. tc.runTest(t)
  519. }
  520. func TestScaleDown(t *testing.T) {
  521. tc := testCase{
  522. minReplicas: 2,
  523. maxReplicas: 6,
  524. initialReplicas: 5,
  525. desiredReplicas: 3,
  526. CPUTarget: 50,
  527. verifyCPUCurrent: true,
  528. reportedLevels: []uint64{100, 300, 500, 250, 250},
  529. reportedCPURequests: []resource.Quantity{resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0")},
  530. useMetricsApi: true,
  531. }
  532. tc.runTest(t)
  533. }
  534. func TestScaleDownCM(t *testing.T) {
  535. tc := testCase{
  536. minReplicas: 2,
  537. maxReplicas: 6,
  538. initialReplicas: 5,
  539. desiredReplicas: 3,
  540. CPUTarget: 0,
  541. cmTarget: &extensions.CustomMetricTargetList{
  542. Items: []extensions.CustomMetricTarget{{
  543. Name: "qps",
  544. TargetValue: resource.MustParse("20"),
  545. }}},
  546. reportedLevels: []uint64{12, 12, 12, 12, 12},
  547. reportedCPURequests: []resource.Quantity{resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0")},
  548. }
  549. tc.runTest(t)
  550. }
  551. func TestTolerance(t *testing.T) {
  552. tc := testCase{
  553. minReplicas: 1,
  554. maxReplicas: 5,
  555. initialReplicas: 3,
  556. desiredReplicas: 3,
  557. CPUTarget: 100,
  558. reportedLevels: []uint64{1010, 1030, 1020},
  559. reportedCPURequests: []resource.Quantity{resource.MustParse("0.9"), resource.MustParse("1.0"), resource.MustParse("1.1")},
  560. useMetricsApi: true,
  561. }
  562. tc.runTest(t)
  563. }
  564. func TestToleranceCM(t *testing.T) {
  565. tc := testCase{
  566. minReplicas: 1,
  567. maxReplicas: 5,
  568. initialReplicas: 3,
  569. desiredReplicas: 3,
  570. cmTarget: &extensions.CustomMetricTargetList{
  571. Items: []extensions.CustomMetricTarget{{
  572. Name: "qps",
  573. TargetValue: resource.MustParse("20"),
  574. }}},
  575. reportedLevels: []uint64{20, 21, 21},
  576. reportedCPURequests: []resource.Quantity{resource.MustParse("0.9"), resource.MustParse("1.0"), resource.MustParse("1.1")},
  577. }
  578. tc.runTest(t)
  579. }
  580. func TestMinReplicas(t *testing.T) {
  581. tc := testCase{
  582. minReplicas: 2,
  583. maxReplicas: 5,
  584. initialReplicas: 3,
  585. desiredReplicas: 2,
  586. CPUTarget: 90,
  587. reportedLevels: []uint64{10, 95, 10},
  588. reportedCPURequests: []resource.Quantity{resource.MustParse("0.9"), resource.MustParse("1.0"), resource.MustParse("1.1")},
  589. useMetricsApi: true,
  590. }
  591. tc.runTest(t)
  592. }
  593. func TestZeroReplicas(t *testing.T) {
  594. tc := testCase{
  595. minReplicas: 3,
  596. maxReplicas: 5,
  597. initialReplicas: 0,
  598. desiredReplicas: 0,
  599. CPUTarget: 90,
  600. reportedLevels: []uint64{},
  601. reportedCPURequests: []resource.Quantity{},
  602. useMetricsApi: true,
  603. }
  604. tc.runTest(t)
  605. }
  606. func TestTooFewReplicas(t *testing.T) {
  607. tc := testCase{
  608. minReplicas: 3,
  609. maxReplicas: 5,
  610. initialReplicas: 2,
  611. desiredReplicas: 3,
  612. CPUTarget: 90,
  613. reportedLevels: []uint64{},
  614. reportedCPURequests: []resource.Quantity{},
  615. useMetricsApi: true,
  616. }
  617. tc.runTest(t)
  618. }
  619. func TestTooManyReplicas(t *testing.T) {
  620. tc := testCase{
  621. minReplicas: 3,
  622. maxReplicas: 5,
  623. initialReplicas: 10,
  624. desiredReplicas: 5,
  625. CPUTarget: 90,
  626. reportedLevels: []uint64{},
  627. reportedCPURequests: []resource.Quantity{},
  628. useMetricsApi: true,
  629. }
  630. tc.runTest(t)
  631. }
  632. func TestMaxReplicas(t *testing.T) {
  633. tc := testCase{
  634. minReplicas: 2,
  635. maxReplicas: 5,
  636. initialReplicas: 3,
  637. desiredReplicas: 5,
  638. CPUTarget: 90,
  639. reportedLevels: []uint64{8000, 9500, 1000},
  640. reportedCPURequests: []resource.Quantity{resource.MustParse("0.9"), resource.MustParse("1.0"), resource.MustParse("1.1")},
  641. useMetricsApi: true,
  642. }
  643. tc.runTest(t)
  644. }
  645. func TestSuperfluousMetrics(t *testing.T) {
  646. tc := testCase{
  647. minReplicas: 2,
  648. maxReplicas: 6,
  649. initialReplicas: 4,
  650. desiredReplicas: 4,
  651. CPUTarget: 100,
  652. reportedLevels: []uint64{4000, 9500, 3000, 7000, 3200, 2000},
  653. reportedCPURequests: []resource.Quantity{resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0")},
  654. useMetricsApi: true,
  655. }
  656. tc.runTest(t)
  657. }
  658. func TestMissingMetrics(t *testing.T) {
  659. tc := testCase{
  660. minReplicas: 2,
  661. maxReplicas: 6,
  662. initialReplicas: 4,
  663. desiredReplicas: 4,
  664. CPUTarget: 100,
  665. reportedLevels: []uint64{400, 95},
  666. reportedCPURequests: []resource.Quantity{resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0")},
  667. useMetricsApi: true,
  668. }
  669. tc.runTest(t)
  670. }
  671. func TestEmptyMetrics(t *testing.T) {
  672. tc := testCase{
  673. minReplicas: 2,
  674. maxReplicas: 6,
  675. initialReplicas: 4,
  676. desiredReplicas: 4,
  677. CPUTarget: 100,
  678. reportedLevels: []uint64{},
  679. reportedCPURequests: []resource.Quantity{resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0")},
  680. useMetricsApi: true,
  681. }
  682. tc.runTest(t)
  683. }
  684. func TestEmptyCPURequest(t *testing.T) {
  685. tc := testCase{
  686. minReplicas: 1,
  687. maxReplicas: 5,
  688. initialReplicas: 1,
  689. desiredReplicas: 1,
  690. CPUTarget: 100,
  691. reportedLevels: []uint64{200},
  692. useMetricsApi: true,
  693. }
  694. tc.runTest(t)
  695. }
  696. func TestEventCreated(t *testing.T) {
  697. tc := testCase{
  698. minReplicas: 1,
  699. maxReplicas: 5,
  700. initialReplicas: 1,
  701. desiredReplicas: 2,
  702. CPUTarget: 50,
  703. reportedLevels: []uint64{200},
  704. reportedCPURequests: []resource.Quantity{resource.MustParse("0.2")},
  705. verifyEvents: true,
  706. useMetricsApi: true,
  707. }
  708. tc.runTest(t)
  709. }
  710. func TestEventNotCreated(t *testing.T) {
  711. tc := testCase{
  712. minReplicas: 1,
  713. maxReplicas: 5,
  714. initialReplicas: 2,
  715. desiredReplicas: 2,
  716. CPUTarget: 50,
  717. reportedLevels: []uint64{200, 200},
  718. reportedCPURequests: []resource.Quantity{resource.MustParse("0.4"), resource.MustParse("0.4")},
  719. verifyEvents: true,
  720. useMetricsApi: true,
  721. }
  722. tc.runTest(t)
  723. }
  724. // TestComputedToleranceAlgImplementation is a regression test which
  725. // back-calculates a minimal percentage for downscaling based on a small percentage
  726. // increase in pod utilization which is calibrated against the tolerance value.
  727. func TestComputedToleranceAlgImplementation(t *testing.T) {
  728. startPods := int32(10)
  729. // 150 mCPU per pod.
  730. totalUsedCPUOfAllPods := uint64(startPods * 150)
  731. // Each pod starts out asking for 2X what is really needed.
  732. // This means we will have a 50% ratio of used/requested
  733. totalRequestedCPUOfAllPods := int32(2 * totalUsedCPUOfAllPods)
  734. requestedToUsed := float64(totalRequestedCPUOfAllPods / int32(totalUsedCPUOfAllPods))
  735. // Spread the amount we ask over 10 pods. We can add some jitter later in reportedLevels.
  736. perPodRequested := totalRequestedCPUOfAllPods / startPods
  737. // Force a minimal scaling event by satisfying (tolerance < 1 - resourcesUsedRatio).
  738. target := math.Abs(1/(requestedToUsed*(1-tolerance))) + .01
  739. finalCpuPercentTarget := int32(target * 100)
  740. resourcesUsedRatio := float64(totalUsedCPUOfAllPods) / float64(float64(totalRequestedCPUOfAllPods)*target)
  741. // i.e. .60 * 20 -> scaled down expectation.
  742. finalPods := int32(math.Ceil(resourcesUsedRatio * float64(startPods)))
  743. // To breach tolerance we will create a utilization ratio difference of tolerance to usageRatioToleranceValue)
  744. tc := testCase{
  745. minReplicas: 0,
  746. maxReplicas: 1000,
  747. initialReplicas: startPods,
  748. desiredReplicas: finalPods,
  749. CPUTarget: finalCpuPercentTarget,
  750. reportedLevels: []uint64{
  751. totalUsedCPUOfAllPods / 10,
  752. totalUsedCPUOfAllPods / 10,
  753. totalUsedCPUOfAllPods / 10,
  754. totalUsedCPUOfAllPods / 10,
  755. totalUsedCPUOfAllPods / 10,
  756. totalUsedCPUOfAllPods / 10,
  757. totalUsedCPUOfAllPods / 10,
  758. totalUsedCPUOfAllPods / 10,
  759. totalUsedCPUOfAllPods / 10,
  760. totalUsedCPUOfAllPods / 10,
  761. },
  762. reportedCPURequests: []resource.Quantity{
  763. resource.MustParse(fmt.Sprint(perPodRequested+100) + "m"),
  764. resource.MustParse(fmt.Sprint(perPodRequested-100) + "m"),
  765. resource.MustParse(fmt.Sprint(perPodRequested+10) + "m"),
  766. resource.MustParse(fmt.Sprint(perPodRequested-10) + "m"),
  767. resource.MustParse(fmt.Sprint(perPodRequested+2) + "m"),
  768. resource.MustParse(fmt.Sprint(perPodRequested-2) + "m"),
  769. resource.MustParse(fmt.Sprint(perPodRequested+1) + "m"),
  770. resource.MustParse(fmt.Sprint(perPodRequested-1) + "m"),
  771. resource.MustParse(fmt.Sprint(perPodRequested) + "m"),
  772. resource.MustParse(fmt.Sprint(perPodRequested) + "m"),
  773. },
  774. useMetricsApi: true,
  775. }
  776. tc.runTest(t)
  777. // Reuse the data structure above, now testing "unscaling".
  778. // Now, we test that no scaling happens if we are in a very close margin to the tolerance
  779. target = math.Abs(1/(requestedToUsed*(1-tolerance))) + .004
  780. finalCpuPercentTarget = int32(target * 100)
  781. tc.CPUTarget = finalCpuPercentTarget
  782. tc.initialReplicas = startPods
  783. tc.desiredReplicas = startPods
  784. tc.runTest(t)
  785. }
  786. // TODO: add more tests