From c9f5eea1f6146813ee0f3e2ec485d0ad3d329670 Mon Sep 17 00:00:00 2001 From: Marcin Wielgus Date: Wed, 29 Jun 2016 12:19:49 +0200 Subject: [PATCH] E2e test for disabling cluster autoscaler in GKE --- test/e2e/cluster_size_autoscaling.go | 121 ++++++++++++++++++--------- 1 file changed, 83 insertions(+), 38 deletions(-) diff --git a/test/e2e/cluster_size_autoscaling.go b/test/e2e/cluster_size_autoscaling.go index 651a0e14ae3..8872abd9ea7 100644 --- a/test/e2e/cluster_size_autoscaling.go +++ b/test/e2e/cluster_size_autoscaling.go @@ -137,25 +137,8 @@ var _ = framework.KubeDescribe("Cluster size autoscaling [Slow]", func() { By("Creating new node-pool with one n1-standard-4 machine") const extraPoolName = "extra-pool" - output, err := exec.Command("gcloud", "alpha", "container", "node-pools", "create", extraPoolName, "--quiet", - "--machine-type=n1-standard-4", - "--num-nodes=1", - "--project="+framework.TestContext.CloudConfig.ProjectID, - "--zone="+framework.TestContext.CloudConfig.Zone, - "--cluster="+framework.TestContext.CloudConfig.Cluster).CombinedOutput() - defer func() { - glog.Infof("Deleting node pool %s", extraPoolName) - output, err := exec.Command("gcloud", "alpha", "container", "node-pools", "delete", extraPoolName, "--quiet", - "--project="+framework.TestContext.CloudConfig.ProjectID, - "--zone="+framework.TestContext.CloudConfig.Zone, - "--cluster="+framework.TestContext.CloudConfig.Cluster).CombinedOutput() - if err != nil { - glog.Infof("Error: %v", err) - } - glog.Infof("Node-pool deletion output: %s", output) - }() - framework.ExpectNoError(err) - glog.Infof("Creating node-pool: %s", output) + addNodePool(extraPoolName, "n1-standard-4") + defer deleteNodePool(extraPoolName) framework.ExpectNoError(framework.WaitForClusterSize(c, nodeCount+1, resizeTimeout)) glog.Infof("Not enabling cluster autoscaler for the node pool (on purpose).") @@ -168,6 +151,18 @@ var _ = framework.KubeDescribe("Cluster size autoscaling [Slow]", func() { framework.ExpectNoError(waitForAllCaPodsReadyInNamespace(f, c)) }) + It("should disable node pool autoscaling [Feature:ClusterSizeAutoscalingScaleUp]", func() { + framework.SkipUnlessProviderIs("gke") + + By("Creating new node-pool with one n1-standard-4 machine") + const extraPoolName = "extra-pool" + addNodePool(extraPoolName, "n1-standard-4") + defer deleteNodePool(extraPoolName) + framework.ExpectNoError(framework.WaitForClusterSize(c, nodeCount+1, resizeTimeout)) + framework.ExpectNoError(enableAutoscaler(extraPoolName, 1, 2)) + framework.ExpectNoError(disableAutoscaler(extraPoolName, 1, 2)) + }) + It("should increase cluster size if pods are pending due to host port conflict [Feature:ClusterSizeAutoscalingScaleUp]", func() { CreateHostPortPods(f, "host-port", nodeCount+2, false) defer framework.DeleteRC(f.Client, f.Namespace.Name, "host-port") @@ -230,25 +225,8 @@ var _ = framework.KubeDescribe("Cluster size autoscaling [Slow]", func() { By("Creating new node-pool with one n1-standard-4 machine") const extraPoolName = "extra-pool" - output, err := exec.Command("gcloud", "alpha", "container", "node-pools", "create", extraPoolName, "--quiet", - "--machine-type=n1-standard-4", - "--num-nodes=1", - "--project="+framework.TestContext.CloudConfig.ProjectID, - "--zone="+framework.TestContext.CloudConfig.Zone, - "--cluster="+framework.TestContext.CloudConfig.Cluster).CombinedOutput() - defer func() { - glog.Infof("Deleting node pool %s", extraPoolName) - output, err := exec.Command("gcloud", "alpha", "container", "node-pools", "delete", extraPoolName, "--quiet", - "--project="+framework.TestContext.CloudConfig.ProjectID, - "--zone="+framework.TestContext.CloudConfig.Zone, - "--cluster="+framework.TestContext.CloudConfig.Cluster).CombinedOutput() - if err != nil { - glog.Infof("Error: %v", err) - } - glog.Infof("Node-pool deletion output: %s", output) - }() - framework.ExpectNoError(err) - glog.Infof("Creating node-pool: %s", output) + addNodePool(extraPoolName, "n1-standard-4") + defer deleteNodePool(extraPoolName) framework.ExpectNoError(framework.WaitForClusterSize(c, nodeCount+1, resizeTimeout)) framework.ExpectNoError(enableAutoscaler(extraPoolName, 1, 2)) @@ -356,6 +334,73 @@ func enableAutoscaler(nodePool string, minCount, maxCount int) error { return fmt.Errorf("autoscaler not enabled") } +func disableAutoscaler(nodePool string, minCount, maxCount int) error { + + if nodePool == "default-pool" { + glog.Infof("Using gcloud to disable autoscaling for pool %s", nodePool) + + output, err := exec.Command("gcloud", "alpha", "container", "clusters", "update", framework.TestContext.CloudConfig.Cluster, + "--no-enable-autoscaling", + "--node-pool="+nodePool, + "--project="+framework.TestContext.CloudConfig.ProjectID, + "--zone="+framework.TestContext.CloudConfig.Zone).Output() + + if err != nil { + return fmt.Errorf("Failed to enable autoscaling: %v", err) + } + glog.Infof("Config update result: %s", output) + + } else { + glog.Infof("Using direct api access to disable autoscaling for pool %s", nodePool) + updateRequest := "{" + + " \"update\": {" + + " \"desiredNodePoolId\": \"" + nodePool + "\"," + + " \"desiredNodePoolAutoscaling\": {" + + " \"enabled\": \"false\"," + + " }" + + " }" + + "}" + + url := getGKEClusterUrl() + glog.Infof("Using gke api url %s", url) + putResult, err := doPut(url, updateRequest) + if err != nil { + return fmt.Errorf("Failed to put %s: %v", url, err) + } + glog.Infof("Config update result: %s", putResult) + } + + for startTime := time.Now(); startTime.Add(gkeUpdateTimeout).After(time.Now()); time.Sleep(30 * time.Second) { + if val, err := isAutoscalerEnabled(minCount); err == nil && !val { + return nil + } + } + return fmt.Errorf("autoscaler still enabled") +} + +func addNodePool(name string, machineType string) { + output, err := exec.Command("gcloud", "alpha", "container", "node-pools", "create", name, "--quiet", + "--machine-type="+machineType, + "--num-nodes=1", + "--project="+framework.TestContext.CloudConfig.ProjectID, + "--zone="+framework.TestContext.CloudConfig.Zone, + "--cluster="+framework.TestContext.CloudConfig.Cluster).CombinedOutput() + framework.ExpectNoError(err) + glog.Infof("Creating node-pool %s: %s", name, output) +} + +func deleteNodePool(name string) { + glog.Infof("Deleting node pool %s", name) + output, err := exec.Command("gcloud", "alpha", "container", "node-pools", "delete", name, "--quiet", + "--project="+framework.TestContext.CloudConfig.ProjectID, + "--zone="+framework.TestContext.CloudConfig.Zone, + "--cluster="+framework.TestContext.CloudConfig.Cluster).CombinedOutput() + if err != nil { + glog.Infof("Error: %v", err) + } + glog.Infof("Node-pool deletion output: %s", output) +} + func doPut(url, content string) (string, error) { req, err := http.NewRequest("PUT", url, bytes.NewBuffer([]byte(content))) req.Header.Set("Content-Type", "application/json")