mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-07 11:13:48 +00:00
Setup for cluster autoscaling tests in GKE
This commit is contained in:
parent
1ad3e11a59
commit
ab56306bec
@ -17,7 +17,11 @@ limitations under the License.
|
|||||||
package e2e
|
package e2e
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -36,6 +40,10 @@ const (
|
|||||||
resizeTimeout = 5 * time.Minute
|
resizeTimeout = 5 * time.Minute
|
||||||
scaleUpTimeout = 5 * time.Minute
|
scaleUpTimeout = 5 * time.Minute
|
||||||
scaleDownTimeout = 15 * time.Minute
|
scaleDownTimeout = 15 * time.Minute
|
||||||
|
|
||||||
|
gkeEndpoint = "https://test-container.sandbox.googleapis.com"
|
||||||
|
zone = "us-central1-b"
|
||||||
|
gkeUpdateTimeout = 10 * time.Minute
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ = framework.KubeDescribe("Cluster size autoscaling [Slow]", func() {
|
var _ = framework.KubeDescribe("Cluster size autoscaling [Slow]", func() {
|
||||||
@ -46,7 +54,15 @@ var _ = framework.KubeDescribe("Cluster size autoscaling [Slow]", func() {
|
|||||||
var originalSizes map[string]int
|
var originalSizes map[string]int
|
||||||
|
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
framework.SkipUnlessProviderIs("gce")
|
framework.SkipUnlessProviderIs("gce", "gke")
|
||||||
|
if framework.ProviderIs("gke") {
|
||||||
|
val, err := isAutoscalerEnabled()
|
||||||
|
framework.ExpectNoError(err)
|
||||||
|
if !val {
|
||||||
|
err = enableAutoscaler()
|
||||||
|
framework.ExpectNoError(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
nodes := framework.GetReadySchedulableNodesOrDie(f.Client)
|
nodes := framework.GetReadySchedulableNodesOrDie(f.Client)
|
||||||
nodeCount = len(nodes.Items)
|
nodeCount = len(nodes.Items)
|
||||||
@ -114,6 +130,83 @@ var _ = framework.KubeDescribe("Cluster size autoscaling [Slow]", func() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
func getGKEClusterUrl() string {
|
||||||
|
out, err := exec.Command("gcloud", "auth", "print-access-token").Output()
|
||||||
|
framework.ExpectNoError(err)
|
||||||
|
token := strings.Replace(string(out), "\n", "", -1)
|
||||||
|
|
||||||
|
return fmt.Sprintf("%s/v1/projects/%s/zones/%s/clusters/%s?access_token=%s",
|
||||||
|
gkeEndpoint,
|
||||||
|
framework.TestContext.CloudConfig.ProjectID,
|
||||||
|
framework.TestContext.CloudConfig.Zone,
|
||||||
|
framework.TestContext.CloudConfig.Cluster,
|
||||||
|
token)
|
||||||
|
}
|
||||||
|
|
||||||
|
func isAutoscalerEnabled() (bool, error) {
|
||||||
|
resp, err := http.Get(getGKEClusterUrl())
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
strBody := string(body)
|
||||||
|
glog.Infof("Cluster config %s", strBody)
|
||||||
|
|
||||||
|
if strings.Contains(strBody, "minNodeCount") {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func enableAutoscaler() error {
|
||||||
|
updateRequest := "{" +
|
||||||
|
" \"update\": {" +
|
||||||
|
" \"desiredNodePoolId\": \"default-pool\"," +
|
||||||
|
" \"desiredNodePoolAutoscaling\": {" +
|
||||||
|
" \"enabled\": \"true\"," +
|
||||||
|
" \"minNodeCount\": \"3\"," +
|
||||||
|
" \"maxNodeCount\": \"5\"" +
|
||||||
|
" }" +
|
||||||
|
" }" +
|
||||||
|
"}"
|
||||||
|
|
||||||
|
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(); err == nil && val {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fmt.Errorf("autoscaler not enabled")
|
||||||
|
}
|
||||||
|
|
||||||
|
func doPut(url, content string) (string, error) {
|
||||||
|
req, err := http.NewRequest("PUT", url, bytes.NewBuffer([]byte(content)))
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
strBody := string(body)
|
||||||
|
return strBody, nil
|
||||||
|
}
|
||||||
|
|
||||||
func CreateHostPortPods(f *framework.Framework, id string, replicas int, expectRunning bool) {
|
func CreateHostPortPods(f *framework.Framework, id string, replicas int, expectRunning bool) {
|
||||||
By(fmt.Sprintf("Running RC which reserves host port"))
|
By(fmt.Sprintf("Running RC which reserves host port"))
|
||||||
config := &framework.RCConfig{
|
config := &framework.RCConfig{
|
||||||
|
Loading…
Reference in New Issue
Block a user