k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/test/e2e/upgrades/autoscaling/horizontal_pod_autoscalers.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package autoscaling 18 19 import ( 20 "context" 21 "fmt" 22 "time" 23 24 autoscalingv2 "k8s.io/api/autoscaling/v2" 25 "k8s.io/kubernetes/test/e2e/framework" 26 e2eautoscaling "k8s.io/kubernetes/test/e2e/framework/autoscaling" 27 "k8s.io/kubernetes/test/e2e/upgrades" 28 29 "github.com/onsi/ginkgo/v2" 30 ) 31 32 // HPAUpgradeTest tests that HPA rescales target resource correctly before and after a cluster upgrade. 33 type HPAUpgradeTest struct { 34 rc *e2eautoscaling.ResourceConsumer 35 hpa *autoscalingv2.HorizontalPodAutoscaler 36 } 37 38 // Name returns the tracking name of the test. 39 func (HPAUpgradeTest) Name() string { return "hpa-upgrade" } 40 41 // Setup creates a resource consumer and an HPA object that autoscales the consumer. 42 func (t *HPAUpgradeTest) Setup(ctx context.Context, f *framework.Framework) { 43 t.rc = e2eautoscaling.NewDynamicResourceConsumer(ctx, 44 "res-cons-upgrade", 45 f.Namespace.Name, 46 e2eautoscaling.KindRC, 47 1, /* replicas */ 48 250, /* initCPUTotal */ 49 0, 50 0, 51 500, /* cpuLimit */ 52 200, /* memLimit */ 53 f.ClientSet, 54 f.ScalesGetter, 55 e2eautoscaling.Disable, 56 e2eautoscaling.Idle) 57 t.hpa = e2eautoscaling.CreateCPUResourceHorizontalPodAutoscaler(ctx, 58 t.rc, 59 20, /* targetCPUUtilizationPercent */ 60 1, /* minPods */ 61 5) /* maxPods */ 62 63 t.rc.Pause() 64 t.test(ctx) 65 } 66 67 // Test waits for upgrade to complete and verifies if HPA works correctly. 68 func (t *HPAUpgradeTest) Test(ctx context.Context, f *framework.Framework, done <-chan struct{}, upgrade upgrades.UpgradeType) { 69 // Block until upgrade is done 70 ginkgo.By(fmt.Sprintf("Waiting for upgrade to finish before checking HPA")) 71 <-done 72 t.test(ctx) 73 } 74 75 // Teardown cleans up any remaining resources. 76 func (t *HPAUpgradeTest) Teardown(ctx context.Context, f *framework.Framework) { 77 // rely on the namespace deletion to clean up everything 78 e2eautoscaling.DeleteHorizontalPodAutoscaler(ctx, t.rc, t.hpa.Name) 79 t.rc.CleanUp(ctx) 80 } 81 82 func (t *HPAUpgradeTest) test(ctx context.Context) { 83 const timeToWait = 15 * time.Minute 84 t.rc.Resume(ctx) 85 86 ginkgo.By(fmt.Sprintf("HPA scales to 1 replica: consume 10 millicores, target per pod 100 millicores, min pods 1.")) 87 t.rc.ConsumeCPU(10) /* millicores */ 88 ginkgo.By(fmt.Sprintf("HPA waits for 1 replica")) 89 t.rc.WaitForReplicas(ctx, 1, timeToWait) 90 91 ginkgo.By(fmt.Sprintf("HPA scales to 3 replicas: consume 250 millicores, target per pod 100 millicores.")) 92 t.rc.ConsumeCPU(250) /* millicores */ 93 ginkgo.By(fmt.Sprintf("HPA waits for 3 replicas")) 94 t.rc.WaitForReplicas(ctx, 3, timeToWait) 95 96 ginkgo.By(fmt.Sprintf("HPA scales to 5 replicas: consume 700 millicores, target per pod 100 millicores, max pods 5.")) 97 t.rc.ConsumeCPU(700) /* millicores */ 98 ginkgo.By(fmt.Sprintf("HPA waits for 5 replicas")) 99 t.rc.WaitForReplicas(ctx, 5, timeToWait) 100 101 // We need to pause background goroutines as during upgrade master is unavailable and requests issued by them fail. 102 t.rc.Pause() 103 }