2015-07-21 14:15:55 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 The Kubernetes Authors All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package e2e
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
"time"
|
|
|
|
|
2015-08-14 09:50:19 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
|
2015-07-21 14:15:55 +00:00
|
|
|
. "github.com/onsi/ginkgo"
|
2015-08-14 09:50:19 +00:00
|
|
|
. "github.com/onsi/gomega"
|
2015-07-21 14:15:55 +00:00
|
|
|
)
|
|
|
|
|
2015-10-07 09:15:58 +00:00
|
|
|
const (
|
|
|
|
scaleUpTimeout = 20 * time.Minute
|
|
|
|
scaleDownTimeout = 30 * time.Minute
|
|
|
|
)
|
|
|
|
|
2016-01-28 16:56:27 +00:00
|
|
|
// [Feature:ClusterSizeAutoscaling]: Cluster size autoscaling is experimental
|
|
|
|
// and require Google Cloud Monitoring to be enabled, so these tests are not
|
|
|
|
// run by default.
|
|
|
|
//
|
|
|
|
// These tests take ~20 minutes to run each.
|
|
|
|
var _ = Describe("Cluster size autoscaling [Feature:ClusterSizeAutoscaling] [Slow]", func() {
|
2016-02-24 15:24:36 +00:00
|
|
|
f := NewDefaultFramework("autoscaling")
|
2015-08-14 09:50:19 +00:00
|
|
|
var nodeCount int
|
|
|
|
var coresPerNode int
|
2015-08-19 13:05:13 +00:00
|
|
|
var memCapacityMb int
|
2015-07-21 14:15:55 +00:00
|
|
|
|
|
|
|
BeforeEach(func() {
|
2015-08-14 09:50:19 +00:00
|
|
|
SkipUnlessProviderIs("gce")
|
|
|
|
|
2015-12-10 14:35:58 +00:00
|
|
|
nodes := ListSchedulableNodesOrDie(f.Client)
|
2015-08-14 09:50:19 +00:00
|
|
|
nodeCount = len(nodes.Items)
|
|
|
|
Expect(nodeCount).NotTo(BeZero())
|
2015-08-19 13:05:13 +00:00
|
|
|
cpu := nodes.Items[0].Status.Capacity[api.ResourceCPU]
|
|
|
|
mem := nodes.Items[0].Status.Capacity[api.ResourceMemory]
|
|
|
|
coresPerNode = int((&cpu).MilliValue() / 1000)
|
|
|
|
memCapacityMb = int((&mem).Value() / 1024 / 1024)
|
2015-07-21 14:15:55 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
AfterEach(func() {
|
|
|
|
cleanUpAutoscaler()
|
|
|
|
})
|
|
|
|
|
2015-11-12 22:30:06 +00:00
|
|
|
It("Should scale cluster size based on cpu utilization", func() {
|
2015-09-29 13:52:30 +00:00
|
|
|
setUpAutoscaler("cpu/node_utilization", 0.4, nodeCount, nodeCount+1)
|
2015-07-21 14:15:55 +00:00
|
|
|
|
2015-09-29 13:52:30 +00:00
|
|
|
// Consume 50% CPU
|
2015-10-14 18:45:32 +00:00
|
|
|
rcs := createConsumingRCs(f, "cpu-utilization", nodeCount*coresPerNode, 500, 0)
|
|
|
|
err := waitForClusterSize(f.Client, nodeCount+1, scaleUpTimeout)
|
|
|
|
for _, rc := range rcs {
|
|
|
|
rc.CleanUp()
|
|
|
|
}
|
|
|
|
expectNoError(err)
|
2015-07-21 14:15:55 +00:00
|
|
|
|
2015-10-07 09:15:58 +00:00
|
|
|
expectNoError(waitForClusterSize(f.Client, nodeCount, scaleDownTimeout))
|
2015-07-21 14:15:55 +00:00
|
|
|
})
|
|
|
|
|
2015-11-12 22:30:06 +00:00
|
|
|
It("Should scale cluster size based on cpu reservation", func() {
|
2015-09-25 09:41:26 +00:00
|
|
|
setUpAutoscaler("cpu/node_reservation", 0.5, nodeCount, nodeCount+1)
|
2015-07-21 14:15:55 +00:00
|
|
|
|
2015-09-25 09:41:26 +00:00
|
|
|
ReserveCpu(f, "cpu-reservation", 600*nodeCount*coresPerNode)
|
2015-10-07 09:15:58 +00:00
|
|
|
expectNoError(waitForClusterSize(f.Client, nodeCount+1, scaleUpTimeout))
|
2015-07-21 14:15:55 +00:00
|
|
|
|
2015-09-25 09:41:26 +00:00
|
|
|
expectNoError(DeleteRC(f.Client, f.Namespace.Name, "cpu-reservation"))
|
2015-10-07 09:15:58 +00:00
|
|
|
expectNoError(waitForClusterSize(f.Client, nodeCount, scaleDownTimeout))
|
2015-07-21 14:15:55 +00:00
|
|
|
})
|
|
|
|
|
2015-11-12 22:30:06 +00:00
|
|
|
It("Should scale cluster size based on memory utilization", func() {
|
2015-10-05 19:03:17 +00:00
|
|
|
setUpAutoscaler("memory/node_utilization", 0.6, nodeCount, nodeCount+1)
|
2015-07-21 14:15:55 +00:00
|
|
|
|
2015-09-21 14:41:07 +00:00
|
|
|
// Consume 60% of total memory capacity
|
|
|
|
megabytesPerReplica := int(memCapacityMb * 6 / 10 / coresPerNode)
|
2015-10-14 18:45:32 +00:00
|
|
|
rcs := createConsumingRCs(f, "mem-utilization", nodeCount*coresPerNode, 0, megabytesPerReplica)
|
|
|
|
err := waitForClusterSize(f.Client, nodeCount+1, scaleUpTimeout)
|
|
|
|
for _, rc := range rcs {
|
|
|
|
rc.CleanUp()
|
|
|
|
}
|
|
|
|
expectNoError(err)
|
2015-07-21 14:15:55 +00:00
|
|
|
|
2015-10-07 09:15:58 +00:00
|
|
|
expectNoError(waitForClusterSize(f.Client, nodeCount, scaleDownTimeout))
|
2015-07-21 14:15:55 +00:00
|
|
|
})
|
|
|
|
|
2015-11-12 22:30:06 +00:00
|
|
|
It("Should scale cluster size based on memory reservation", func() {
|
2015-09-25 09:41:26 +00:00
|
|
|
setUpAutoscaler("memory/node_reservation", 0.5, nodeCount, nodeCount+1)
|
2015-07-21 14:15:55 +00:00
|
|
|
|
2015-09-25 09:41:26 +00:00
|
|
|
ReserveMemory(f, "memory-reservation", nodeCount*memCapacityMb*6/10)
|
2015-10-07 09:15:58 +00:00
|
|
|
expectNoError(waitForClusterSize(f.Client, nodeCount+1, scaleUpTimeout))
|
2015-07-21 14:15:55 +00:00
|
|
|
|
2015-09-25 09:41:26 +00:00
|
|
|
expectNoError(DeleteRC(f.Client, f.Namespace.Name, "memory-reservation"))
|
2015-10-07 09:15:58 +00:00
|
|
|
expectNoError(waitForClusterSize(f.Client, nodeCount, scaleDownTimeout))
|
2015-07-21 14:15:55 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-08-14 09:50:19 +00:00
|
|
|
func setUpAutoscaler(metric string, target float64, min, max int) {
|
2015-07-21 14:15:55 +00:00
|
|
|
// TODO integrate with kube-up.sh script once it will support autoscaler setup.
|
|
|
|
By("Setting up autoscaler to scale based on " + metric)
|
2015-08-17 19:09:08 +00:00
|
|
|
out, err := exec.Command("gcloud", "compute", "instance-groups", "managed", "set-autoscaling",
|
|
|
|
testContext.CloudConfig.NodeInstanceGroup,
|
2015-07-21 14:15:55 +00:00
|
|
|
"--project="+testContext.CloudConfig.ProjectID,
|
2015-08-17 19:09:08 +00:00
|
|
|
"--zone="+testContext.CloudConfig.Zone,
|
|
|
|
"--custom-metric-utilization=metric=custom.cloudmonitoring.googleapis.com/kubernetes.io/"+metric+fmt.Sprintf(",utilization-target=%v", target)+",utilization-target-type=GAUGE",
|
2015-07-21 14:15:55 +00:00
|
|
|
fmt.Sprintf("--min-num-replicas=%v", min),
|
|
|
|
fmt.Sprintf("--max-num-replicas=%v", max),
|
|
|
|
).CombinedOutput()
|
2015-08-17 14:12:32 +00:00
|
|
|
expectNoError(err, "Output: "+string(out))
|
2015-07-21 14:15:55 +00:00
|
|
|
}
|
|
|
|
|
2015-10-14 18:45:32 +00:00
|
|
|
func createConsumingRCs(f *Framework, name string, count, cpuPerReplica, memPerReplica int) []*ResourceConsumer {
|
|
|
|
var res []*ResourceConsumer
|
|
|
|
for i := 1; i <= count; i++ {
|
|
|
|
name := fmt.Sprintf("%s-%d", name, i)
|
2016-03-04 14:50:55 +00:00
|
|
|
res = append(res, NewStaticResourceConsumer(name, 1, cpuPerReplica, memPerReplica, 0, int64(cpuPerReplica), int64(memPerReplica+100), f))
|
2015-10-14 18:45:32 +00:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2015-07-21 14:15:55 +00:00
|
|
|
func cleanUpAutoscaler() {
|
|
|
|
By("Removing autoscaler")
|
2015-08-17 19:09:08 +00:00
|
|
|
out, err := exec.Command("gcloud", "compute", "instance-groups", "managed", "stop-autoscaling",
|
|
|
|
testContext.CloudConfig.NodeInstanceGroup,
|
|
|
|
"--project="+testContext.CloudConfig.ProjectID,
|
|
|
|
"--zone="+testContext.CloudConfig.Zone,
|
|
|
|
).CombinedOutput()
|
2015-08-17 14:12:32 +00:00
|
|
|
expectNoError(err, "Output: "+string(out))
|
2015-07-21 14:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ReserveCpu(f *Framework, id string, millicores int) {
|
|
|
|
By(fmt.Sprintf("Running RC which reserves %v millicores", millicores))
|
|
|
|
config := &RCConfig{
|
2015-09-25 09:41:26 +00:00
|
|
|
Client: f.Client,
|
|
|
|
Name: id,
|
|
|
|
Namespace: f.Namespace.Name,
|
|
|
|
Timeout: 10 * time.Minute,
|
2015-11-04 23:52:49 +00:00
|
|
|
Image: "gcr.io/google_containers/pause:2.0",
|
2015-09-25 09:41:26 +00:00
|
|
|
Replicas: millicores / 100,
|
|
|
|
CpuRequest: 100,
|
2015-07-21 14:15:55 +00:00
|
|
|
}
|
|
|
|
expectNoError(RunRC(*config))
|
|
|
|
}
|
|
|
|
|
2015-09-25 09:41:26 +00:00
|
|
|
func ReserveMemory(f *Framework, id string, megabytes int) {
|
|
|
|
By(fmt.Sprintf("Running RC which reserves %v MB of memory", megabytes))
|
2015-07-21 14:15:55 +00:00
|
|
|
config := &RCConfig{
|
2015-09-25 09:41:26 +00:00
|
|
|
Client: f.Client,
|
|
|
|
Name: id,
|
|
|
|
Namespace: f.Namespace.Name,
|
|
|
|
Timeout: 10 * time.Minute,
|
2015-11-04 23:52:49 +00:00
|
|
|
Image: "gcr.io/google_containers/pause:2.0",
|
2015-09-25 09:41:26 +00:00
|
|
|
Replicas: megabytes / 500,
|
|
|
|
MemRequest: 500 * 1024 * 1024,
|
2015-07-21 14:15:55 +00:00
|
|
|
}
|
|
|
|
expectNoError(RunRC(*config))
|
|
|
|
}
|