|
| 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 | + "time" |
| 22 | + |
| 23 | + "golang.org/x/oauth2/google" |
| 24 | + clientset "k8s.io/client-go/kubernetes" |
| 25 | + |
| 26 | + . "github.com/onsi/ginkgo" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + "k8s.io/kubernetes/test/e2e/framework" |
| 29 | + |
| 30 | + gcm "google.golang.org/api/monitoring/v3" |
| 31 | + as "k8s.io/api/autoscaling/v2beta1" |
| 32 | + "k8s.io/apimachinery/pkg/api/resource" |
| 33 | + "k8s.io/apimachinery/pkg/util/wait" |
| 34 | + "k8s.io/kubernetes/test/e2e/instrumentation/monitoring" |
| 35 | +) |
| 36 | + |
| 37 | +const ( |
| 38 | + stackdriverExporterDeployment = "stackdriver-exporter-deployment" |
| 39 | + dummyDeploymentName = "dummy-deployment" |
| 40 | + stackdriverExporterPod = "stackdriver-exporter-pod" |
| 41 | +) |
| 42 | + |
| 43 | +var _ = SIGDescribe("[HPA] Horizontal pod autoscaling (scale resource: Custom Metrics from Stackdriver)", func() { |
| 44 | + BeforeEach(func() { |
| 45 | + framework.SkipUnlessProviderIs("gce") |
| 46 | + }) |
| 47 | + |
| 48 | + f := framework.NewDefaultFramework("horizontal-pod-autoscaling") |
| 49 | + var kubeClient clientset.Interface |
| 50 | + |
| 51 | + It("should autoscale with Custom Metrics from Stackdriver [Feature:CustomMetricsAutoscaling]", func() { |
| 52 | + kubeClient = f.ClientSet |
| 53 | + testHPA(f, kubeClient) |
| 54 | + }) |
| 55 | +}) |
| 56 | + |
| 57 | +func testHPA(f *framework.Framework, kubeClient clientset.Interface) { |
| 58 | + projectId := framework.TestContext.CloudConfig.ProjectID |
| 59 | + |
| 60 | + ctx := context.Background() |
| 61 | + client, err := google.DefaultClient(ctx, gcm.CloudPlatformScope) |
| 62 | + |
| 63 | + // Hack for running tests locally, needed to authenticate in Stackdriver |
| 64 | + // If this is your use case, create application default credentials: |
| 65 | + // $ gcloud auth application-default login |
| 66 | + // and uncomment following lines: |
| 67 | + /* |
| 68 | + ts, err := google.DefaultTokenSource(oauth2.NoContext) |
| 69 | + framework.Logf("Couldn't get application default credentials, %v", err) |
| 70 | + if err != nil { |
| 71 | + framework.Failf("Error accessing application default credentials, %v", err) |
| 72 | + } |
| 73 | + client := oauth2.NewClient(oauth2.NoContext, ts) |
| 74 | + */ |
| 75 | + |
| 76 | + gcmService, err := gcm.New(client) |
| 77 | + if err != nil { |
| 78 | + framework.Failf("Failed to create gcm service, %v", err) |
| 79 | + } |
| 80 | + |
| 81 | + // Set up a cluster: create a custom metric and set up k8s-sd adapter |
| 82 | + err = monitoring.CreateDescriptors(gcmService, projectId) |
| 83 | + if err != nil { |
| 84 | + framework.Failf("Failed to create metric descriptor: %v", err) |
| 85 | + } |
| 86 | + defer monitoring.CleanupDescriptors(gcmService, projectId) |
| 87 | + |
| 88 | + err = monitoring.CreateAdapter() |
| 89 | + if err != nil { |
| 90 | + framework.Failf("Failed to set up: %v", err) |
| 91 | + } |
| 92 | + defer monitoring.CleanupAdapter() |
| 93 | + |
| 94 | + // Run application that exports the metric |
| 95 | + err = createDeploymentsToScale(f, kubeClient) |
| 96 | + if err != nil { |
| 97 | + framework.Failf("Failed to create stackdriver-exporter pod: %v", err) |
| 98 | + } |
| 99 | + defer cleanupDeploymentsToScale(f, kubeClient) |
| 100 | + |
| 101 | + // Autoscale the deployments |
| 102 | + err = createPodsHPA(f, kubeClient) |
| 103 | + if err != nil { |
| 104 | + framework.Failf("Failed to create 'Pods' HPA: %v", err) |
| 105 | + } |
| 106 | + err = createObjectHPA(f, kubeClient) |
| 107 | + if err != nil { |
| 108 | + framework.Failf("Failed to create 'Objects' HPA: %v", err) |
| 109 | + } |
| 110 | + |
| 111 | + waitForReplicas(stackdriverExporterDeployment, f.Namespace.ObjectMeta.Name, kubeClient, 15*time.Minute, 1) |
| 112 | + waitForReplicas(dummyDeploymentName, f.Namespace.ObjectMeta.Name, kubeClient, 15*time.Minute, 1) |
| 113 | +} |
| 114 | + |
| 115 | +func createDeploymentsToScale(f *framework.Framework, cs clientset.Interface) error { |
| 116 | + _, err := cs.Extensions().Deployments(f.Namespace.ObjectMeta.Name).Create(monitoring.StackdriverExporterDeployment(stackdriverExporterDeployment, f.Namespace.Name, 2, 100)) |
| 117 | + if err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + _, err = cs.Core().Pods(f.Namespace.ObjectMeta.Name).Create(monitoring.StackdriverExporterPod(stackdriverExporterPod, f.Namespace.Name, stackdriverExporterPod, monitoring.CustomMetricName, 100)) |
| 121 | + if err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + _, err = cs.Extensions().Deployments(f.Namespace.ObjectMeta.Name).Create(monitoring.StackdriverExporterDeployment(dummyDeploymentName, f.Namespace.Name, 2, 100)) |
| 125 | + return err |
| 126 | +} |
| 127 | + |
| 128 | +func cleanupDeploymentsToScale(f *framework.Framework, cs clientset.Interface) { |
| 129 | + _ = cs.Extensions().Deployments(f.Namespace.ObjectMeta.Name).Delete(stackdriverExporterDeployment, &metav1.DeleteOptions{}) |
| 130 | + _ = cs.Core().Pods(f.Namespace.ObjectMeta.Name).Delete(stackdriverExporterPod, &metav1.DeleteOptions{}) |
| 131 | + _ = cs.Extensions().Deployments(f.Namespace.ObjectMeta.Name).Delete(dummyDeploymentName, &metav1.DeleteOptions{}) |
| 132 | +} |
| 133 | + |
| 134 | +func createPodsHPA(f *framework.Framework, cs clientset.Interface) error { |
| 135 | + var minReplicas int32 = 1 |
| 136 | + _, err := cs.AutoscalingV2beta1().HorizontalPodAutoscalers(f.Namespace.ObjectMeta.Name).Create(&as.HorizontalPodAutoscaler{ |
| 137 | + ObjectMeta: metav1.ObjectMeta{ |
| 138 | + Name: "custom-metrics-pods-hpa", |
| 139 | + Namespace: f.Namespace.ObjectMeta.Name, |
| 140 | + }, |
| 141 | + Spec: as.HorizontalPodAutoscalerSpec{ |
| 142 | + Metrics: []as.MetricSpec{ |
| 143 | + { |
| 144 | + Type: as.PodsMetricSourceType, |
| 145 | + Pods: &as.PodsMetricSource{ |
| 146 | + MetricName: monitoring.CustomMetricName, |
| 147 | + TargetAverageValue: *resource.NewQuantity(200, resource.DecimalSI), |
| 148 | + }, |
| 149 | + }, |
| 150 | + }, |
| 151 | + MaxReplicas: 3, |
| 152 | + MinReplicas: &minReplicas, |
| 153 | + ScaleTargetRef: as.CrossVersionObjectReference{ |
| 154 | + APIVersion: "extensions/v1beta1", |
| 155 | + Kind: "Deployment", |
| 156 | + Name: stackdriverExporterDeployment, |
| 157 | + }, |
| 158 | + }, |
| 159 | + }) |
| 160 | + return err |
| 161 | +} |
| 162 | + |
| 163 | +func createObjectHPA(f *framework.Framework, cs clientset.Interface) error { |
| 164 | + var minReplicas int32 = 1 |
| 165 | + _, err := cs.AutoscalingV2beta1().HorizontalPodAutoscalers(f.Namespace.ObjectMeta.Name).Create(&as.HorizontalPodAutoscaler{ |
| 166 | + ObjectMeta: metav1.ObjectMeta{ |
| 167 | + Name: "custom-metrics-objects-hpa", |
| 168 | + Namespace: f.Namespace.ObjectMeta.Name, |
| 169 | + }, |
| 170 | + Spec: as.HorizontalPodAutoscalerSpec{ |
| 171 | + Metrics: []as.MetricSpec{ |
| 172 | + { |
| 173 | + Type: as.ObjectMetricSourceType, |
| 174 | + Object: &as.ObjectMetricSource{ |
| 175 | + MetricName: monitoring.CustomMetricName, |
| 176 | + Target: as.CrossVersionObjectReference{ |
| 177 | + Kind: "Pod", |
| 178 | + Name: stackdriverExporterPod, |
| 179 | + }, |
| 180 | + TargetValue: *resource.NewQuantity(200, resource.DecimalSI), |
| 181 | + }, |
| 182 | + }, |
| 183 | + }, |
| 184 | + MaxReplicas: 3, |
| 185 | + MinReplicas: &minReplicas, |
| 186 | + ScaleTargetRef: as.CrossVersionObjectReference{ |
| 187 | + APIVersion: "extensions/v1beta1", |
| 188 | + Kind: "Deployment", |
| 189 | + Name: dummyDeploymentName, |
| 190 | + }, |
| 191 | + }, |
| 192 | + }) |
| 193 | + return err |
| 194 | +} |
| 195 | + |
| 196 | +func waitForReplicas(deploymentName, namespace string, cs clientset.Interface, timeout time.Duration, desiredReplicas int) { |
| 197 | + interval := 20 * time.Second |
| 198 | + err := wait.PollImmediate(interval, timeout, func() (bool, error) { |
| 199 | + deployment, err := cs.Extensions().Deployments(namespace).Get(deploymentName, metav1.GetOptions{}) |
| 200 | + if err != nil { |
| 201 | + framework.Failf("Failed to get replication controller %s: %v", deployment, err) |
| 202 | + } |
| 203 | + replicas := int(deployment.Status.ReadyReplicas) |
| 204 | + framework.Logf("waiting for %d replicas (current: %d)", desiredReplicas, replicas) |
| 205 | + return replicas == desiredReplicas, nil // Expected number of replicas found. Exit. |
| 206 | + }) |
| 207 | + if err != nil { |
| 208 | + framework.Failf("Timeout waiting %v for %v replicas", timeout, desiredReplicas) |
| 209 | + } |
| 210 | +} |
0 commit comments