2015-08-27 08:06:36 +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 (
|
2015-09-28 09:56:40 +00:00
|
|
|
"fmt"
|
2015-08-27 08:06:36 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
client "k8s.io/kubernetes/pkg/client/unversioned"
|
|
|
|
"k8s.io/kubernetes/pkg/util"
|
|
|
|
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-09-18 10:25:19 +00:00
|
|
|
dynamicConsumptionTimeInSeconds = 30
|
|
|
|
staticConsumptionTimeInSeconds = 3600
|
|
|
|
dynamicRequestSizeInMillicores = 100
|
|
|
|
dynamicRequestSizeInMegabytes = 100
|
|
|
|
port = 80
|
|
|
|
targetPort = 8080
|
|
|
|
timeoutRC = 120 * time.Second
|
2015-09-23 13:54:42 +00:00
|
|
|
startServiceTimeout = time.Minute
|
|
|
|
startServiceInterval = 5 * time.Second
|
2015-10-06 08:24:35 +00:00
|
|
|
resourceConsumerImage = "gcr.io/google_containers/resource_consumer:beta"
|
2015-09-18 10:25:19 +00:00
|
|
|
rcIsNil = "ERROR: replicationController = nil"
|
2015-08-27 08:06:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
2015-10-20 02:41:58 +00:00
|
|
|
ResourceConsumer is a tool for testing. It helps create specified usage of CPU or memory (Warning: memory not supported)
|
2015-08-27 08:06:36 +00:00
|
|
|
typical use case:
|
|
|
|
rc.ConsumeCPU(600)
|
|
|
|
// ... check your assumption here
|
|
|
|
rc.ConsumeCPU(300)
|
|
|
|
// ... check your assumption here
|
|
|
|
*/
|
2015-09-07 10:26:23 +00:00
|
|
|
type ResourceConsumer struct {
|
2015-09-18 10:25:19 +00:00
|
|
|
name string
|
|
|
|
framework *Framework
|
|
|
|
cpu chan int
|
|
|
|
mem chan int
|
|
|
|
stopCPU chan int
|
|
|
|
stopMem chan int
|
|
|
|
consumptionTimeInSeconds int
|
|
|
|
sleepTime time.Duration
|
|
|
|
requestSizeInMillicores int
|
|
|
|
requestSizeInMegabytes int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDynamicResourceConsumer(name string, replicas, initCPU, initMemory int, cpuLimit, memLimit int64, framework *Framework) *ResourceConsumer {
|
|
|
|
return newResourceConsumer(name, replicas, initCPU, initMemory, dynamicConsumptionTimeInSeconds, dynamicRequestSizeInMillicores, dynamicRequestSizeInMegabytes, cpuLimit, memLimit, framework)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStaticResourceConsumer(name string, replicas, initCPU, initMemory int, cpuLimit, memLimit int64, framework *Framework) *ResourceConsumer {
|
|
|
|
return newResourceConsumer(name, replicas, initCPU, initMemory, staticConsumptionTimeInSeconds, initCPU/replicas, initMemory/replicas, cpuLimit, memLimit, framework)
|
2015-08-27 08:06:36 +00:00
|
|
|
}
|
|
|
|
|
2015-09-16 11:52:26 +00:00
|
|
|
/*
|
|
|
|
NewResourceConsumer creates new ResourceConsumer
|
|
|
|
initCPU argument is in millicores
|
|
|
|
initMemory argument is in megabytes
|
2015-09-17 09:09:17 +00:00
|
|
|
memLimit argument is in megabytes, memLimit is a maximum amount of memory that can be consumed by a single pod
|
|
|
|
cpuLimit argument is in millicores, cpuLimit is a maximum amount of cpu that can be consumed by a single pod
|
2015-09-16 11:52:26 +00:00
|
|
|
*/
|
2015-09-18 10:25:19 +00:00
|
|
|
func newResourceConsumer(name string, replicas, initCPU, initMemory, consumptionTimeInSeconds, requestSizeInMillicores, requestSizeInMegabytes int, cpuLimit, memLimit int64, framework *Framework) *ResourceConsumer {
|
2015-09-17 09:09:17 +00:00
|
|
|
runServiceAndRCForResourceConsumer(framework.Client, framework.Namespace.Name, name, replicas, cpuLimit, memLimit)
|
2015-09-07 10:26:23 +00:00
|
|
|
rc := &ResourceConsumer{
|
2015-09-18 10:25:19 +00:00
|
|
|
name: name,
|
|
|
|
framework: framework,
|
|
|
|
cpu: make(chan int),
|
|
|
|
mem: make(chan int),
|
|
|
|
stopCPU: make(chan int),
|
|
|
|
stopMem: make(chan int),
|
|
|
|
consumptionTimeInSeconds: consumptionTimeInSeconds,
|
|
|
|
sleepTime: time.Duration(consumptionTimeInSeconds) * time.Second,
|
|
|
|
requestSizeInMillicores: requestSizeInMillicores,
|
|
|
|
requestSizeInMegabytes: requestSizeInMegabytes,
|
2015-08-27 08:06:36 +00:00
|
|
|
}
|
|
|
|
go rc.makeConsumeCPURequests()
|
2015-09-16 11:52:26 +00:00
|
|
|
rc.ConsumeCPU(initCPU)
|
|
|
|
go rc.makeConsumeMemRequests()
|
|
|
|
rc.ConsumeMem(initMemory)
|
2015-08-27 08:06:36 +00:00
|
|
|
return rc
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConsumeCPU consumes given number of CPU
|
2015-09-08 18:12:08 +00:00
|
|
|
func (rc *ResourceConsumer) ConsumeCPU(millicores int) {
|
2015-09-28 09:56:40 +00:00
|
|
|
Logf("RC %s: consume %v millicores in total", rc.name, millicores)
|
2015-09-16 11:52:26 +00:00
|
|
|
rc.cpu <- millicores
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConsumeMem consumes given number of Mem
|
|
|
|
func (rc *ResourceConsumer) ConsumeMem(megabytes int) {
|
2015-09-28 09:56:40 +00:00
|
|
|
Logf("RC %s: consume %v MB in total", rc.name, megabytes)
|
2015-09-16 11:52:26 +00:00
|
|
|
rc.mem <- megabytes
|
2015-08-27 08:06:36 +00:00
|
|
|
}
|
|
|
|
|
2015-09-07 10:26:23 +00:00
|
|
|
func (rc *ResourceConsumer) makeConsumeCPURequests() {
|
2015-09-25 12:52:32 +00:00
|
|
|
defer GinkgoRecover()
|
2015-08-27 08:06:36 +00:00
|
|
|
var count int
|
|
|
|
var rest int
|
2015-09-18 10:25:19 +00:00
|
|
|
sleepTime := time.Duration(0)
|
2015-08-27 08:06:36 +00:00
|
|
|
for {
|
|
|
|
select {
|
2015-09-16 11:52:26 +00:00
|
|
|
case millicores := <-rc.cpu:
|
2015-09-28 09:56:40 +00:00
|
|
|
Logf("RC %s: consume %v millicores in total", rc.name, millicores)
|
2015-09-18 10:25:19 +00:00
|
|
|
if rc.requestSizeInMillicores != 0 {
|
|
|
|
count = millicores / rc.requestSizeInMillicores
|
|
|
|
}
|
|
|
|
rest = millicores - count*rc.requestSizeInMillicores
|
2015-08-27 08:06:36 +00:00
|
|
|
case <-time.After(sleepTime):
|
2015-09-28 09:56:40 +00:00
|
|
|
Logf("RC %s: sending %v requests to consume %v millicores each and 1 request to consume %v millicores", rc.name, count, rc.requestSizeInMillicores, rest)
|
2015-08-27 08:06:36 +00:00
|
|
|
if count > 0 {
|
2015-09-18 10:25:19 +00:00
|
|
|
rc.sendConsumeCPURequests(count, rc.requestSizeInMillicores, rc.consumptionTimeInSeconds)
|
2015-09-16 11:52:26 +00:00
|
|
|
}
|
|
|
|
if rest > 0 {
|
2015-09-18 10:25:19 +00:00
|
|
|
go rc.sendOneConsumeCPURequest(rest, rc.consumptionTimeInSeconds)
|
2015-09-16 11:52:26 +00:00
|
|
|
}
|
2015-09-18 10:25:19 +00:00
|
|
|
sleepTime = rc.sleepTime
|
2015-09-16 11:52:26 +00:00
|
|
|
case <-rc.stopCPU:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *ResourceConsumer) makeConsumeMemRequests() {
|
2015-09-25 12:52:32 +00:00
|
|
|
defer GinkgoRecover()
|
2015-09-16 11:52:26 +00:00
|
|
|
var count int
|
|
|
|
var rest int
|
2015-09-18 10:25:19 +00:00
|
|
|
sleepTime := time.Duration(0)
|
2015-09-16 11:52:26 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case megabytes := <-rc.mem:
|
2015-09-28 09:56:40 +00:00
|
|
|
Logf("RC %s: consume %v MB in total", rc.name, megabytes)
|
2015-09-18 10:25:19 +00:00
|
|
|
if rc.requestSizeInMegabytes != 0 {
|
|
|
|
count = megabytes / rc.requestSizeInMegabytes
|
|
|
|
}
|
|
|
|
rest = megabytes - count*rc.requestSizeInMegabytes
|
2015-09-16 11:52:26 +00:00
|
|
|
case <-time.After(sleepTime):
|
2015-09-28 09:56:40 +00:00
|
|
|
Logf("RC %s: sending %v requests to consume %v MB each and 1 request to consume %v MB", rc.name, count, rc.requestSizeInMegabytes, rest)
|
2015-09-16 11:52:26 +00:00
|
|
|
if count > 0 {
|
2015-09-18 10:25:19 +00:00
|
|
|
rc.sendConsumeMemRequests(count, rc.requestSizeInMegabytes, rc.consumptionTimeInSeconds)
|
2015-08-27 08:06:36 +00:00
|
|
|
}
|
|
|
|
if rest > 0 {
|
2015-09-18 10:25:19 +00:00
|
|
|
go rc.sendOneConsumeMemRequest(rest, rc.consumptionTimeInSeconds)
|
2015-08-27 08:06:36 +00:00
|
|
|
}
|
2015-09-18 10:25:19 +00:00
|
|
|
sleepTime = rc.sleepTime
|
2015-09-16 11:52:26 +00:00
|
|
|
case <-rc.stopMem:
|
2015-08-27 08:06:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-16 11:52:26 +00:00
|
|
|
func (rc *ResourceConsumer) sendConsumeCPURequests(requests, millicores, durationSec int) {
|
2015-08-27 08:06:36 +00:00
|
|
|
for i := 0; i < requests; i++ {
|
2015-09-16 11:52:26 +00:00
|
|
|
go rc.sendOneConsumeCPURequest(millicores, durationSec)
|
2015-08-27 08:06:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-16 11:52:26 +00:00
|
|
|
func (rc *ResourceConsumer) sendConsumeMemRequests(requests, megabytes, durationSec int) {
|
|
|
|
for i := 0; i < requests; i++ {
|
|
|
|
go rc.sendOneConsumeMemRequest(megabytes, durationSec)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendOneConsumeCPURequest sends POST request for cpu consumption
|
|
|
|
func (rc *ResourceConsumer) sendOneConsumeCPURequest(millicores int, durationSec int) {
|
|
|
|
defer GinkgoRecover()
|
2015-08-27 08:06:36 +00:00
|
|
|
_, err := rc.framework.Client.Post().
|
|
|
|
Prefix("proxy").
|
|
|
|
Namespace(rc.framework.Namespace.Name).
|
|
|
|
Resource("services").
|
|
|
|
Name(rc.name).
|
|
|
|
Suffix("ConsumeCPU").
|
2015-09-08 18:12:08 +00:00
|
|
|
Param("millicores", strconv.Itoa(millicores)).
|
2015-08-27 08:06:36 +00:00
|
|
|
Param("durationSec", strconv.Itoa(durationSec)).
|
2015-09-25 12:52:32 +00:00
|
|
|
DoRaw()
|
2015-08-27 08:06:36 +00:00
|
|
|
expectNoError(err)
|
|
|
|
}
|
|
|
|
|
2015-09-16 11:52:26 +00:00
|
|
|
// sendOneConsumeMemRequest sends POST request for memory consumption
|
|
|
|
func (rc *ResourceConsumer) sendOneConsumeMemRequest(megabytes int, durationSec int) {
|
|
|
|
defer GinkgoRecover()
|
|
|
|
_, err := rc.framework.Client.Post().
|
|
|
|
Prefix("proxy").
|
|
|
|
Namespace(rc.framework.Namespace.Name).
|
|
|
|
Resource("services").
|
|
|
|
Name(rc.name).
|
|
|
|
Suffix("ConsumeMem").
|
|
|
|
Param("megabytes", strconv.Itoa(megabytes)).
|
|
|
|
Param("durationSec", strconv.Itoa(durationSec)).
|
2015-09-25 12:52:32 +00:00
|
|
|
DoRaw()
|
2015-09-16 11:52:26 +00:00
|
|
|
expectNoError(err)
|
|
|
|
}
|
|
|
|
|
2015-09-07 10:26:23 +00:00
|
|
|
func (rc *ResourceConsumer) GetReplicas() int {
|
|
|
|
replicationController, err := rc.framework.Client.ReplicationControllers(rc.framework.Namespace.Name).Get(rc.name)
|
|
|
|
expectNoError(err)
|
|
|
|
if replicationController == nil {
|
|
|
|
Failf(rcIsNil)
|
|
|
|
}
|
|
|
|
return replicationController.Status.Replicas
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *ResourceConsumer) WaitForReplicas(desiredReplicas int) {
|
|
|
|
timeout := 10 * time.Minute
|
|
|
|
for start := time.Now(); time.Since(start) < timeout; time.Sleep(20 * time.Second) {
|
|
|
|
if desiredReplicas == rc.GetReplicas() {
|
|
|
|
Logf("Replication Controller current replicas number is equal to desired replicas number: %d", desiredReplicas)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
Logf("Replication Controller current replicas number %d waiting to be %d", rc.GetReplicas(), desiredReplicas)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Failf("timeout waiting %v for pods size to be %d", timeout, desiredReplicas)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *ResourceConsumer) CleanUp() {
|
2015-09-28 09:56:40 +00:00
|
|
|
By(fmt.Sprintf("Removing consuming RC %s", rc.name))
|
2015-09-16 11:52:26 +00:00
|
|
|
rc.stopCPU <- 0
|
|
|
|
rc.stopMem <- 0
|
2015-09-24 09:09:40 +00:00
|
|
|
// Wait some time to ensure all child goroutines are finished.
|
|
|
|
time.Sleep(10 * time.Second)
|
2015-08-27 08:06:36 +00:00
|
|
|
expectNoError(DeleteRC(rc.framework.Client, rc.framework.Namespace.Name, rc.name))
|
|
|
|
expectNoError(rc.framework.Client.Services(rc.framework.Namespace.Name).Delete(rc.name))
|
|
|
|
}
|
|
|
|
|
2015-09-17 09:09:17 +00:00
|
|
|
func runServiceAndRCForResourceConsumer(c *client.Client, ns, name string, replicas int, cpuLimitMillis, memLimitMb int64) {
|
2015-09-28 09:56:40 +00:00
|
|
|
By(fmt.Sprintf("Running consuming RC %s with %v replicas", name, replicas))
|
2015-09-07 10:26:23 +00:00
|
|
|
_, err := c.Services(ns).Create(&api.Service{
|
2015-08-27 08:06:36 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: name,
|
|
|
|
},
|
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Ports: []api.ServicePort{{
|
|
|
|
Port: port,
|
|
|
|
TargetPort: util.NewIntOrStringFromInt(targetPort),
|
|
|
|
}},
|
|
|
|
|
|
|
|
Selector: map[string]string{
|
|
|
|
"name": name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2015-09-07 10:26:23 +00:00
|
|
|
expectNoError(err)
|
2015-08-27 08:06:36 +00:00
|
|
|
config := RCConfig{
|
2015-09-29 13:52:30 +00:00
|
|
|
Client: c,
|
2015-10-06 08:24:35 +00:00
|
|
|
Image: resourceConsumerImage,
|
2015-09-29 13:52:30 +00:00
|
|
|
Name: name,
|
|
|
|
Namespace: ns,
|
|
|
|
Timeout: timeoutRC,
|
|
|
|
Replicas: replicas,
|
|
|
|
CpuRequest: cpuLimitMillis,
|
|
|
|
CpuLimit: cpuLimitMillis,
|
|
|
|
MemRequest: memLimitMb * 1024 * 1024, // MemLimit is in bytes
|
|
|
|
MemLimit: memLimitMb * 1024 * 1024,
|
2015-08-27 08:06:36 +00:00
|
|
|
}
|
|
|
|
expectNoError(RunRC(config))
|
2015-09-25 08:16:03 +00:00
|
|
|
// Make sure endpoints are propagated.
|
|
|
|
// TODO(piosz): replace sleep with endpoints watch.
|
|
|
|
time.Sleep(10 * time.Second)
|
2015-08-27 08:06:36 +00:00
|
|
|
}
|