2015-08-04 00:28:33 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2015-08-04 00:28:33 +00:00
|
|
|
|
|
|
|
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 qos
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
2019-02-05 18:29:37 +00:00
|
|
|
v1 "k8s.io/api/core/v1"
|
2017-06-22 18:24:23 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
2019-02-05 18:29:37 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apis/scheduling"
|
2015-08-04 00:28:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
standardMemoryAmount = 8000000000
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-11-18 20:50:58 +00:00
|
|
|
cpuLimit = v1.Pod{
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
2016-05-04 00:30:09 +00:00
|
|
|
{
|
2016-11-18 20:50:58 +00:00
|
|
|
Resources: v1.ResourceRequirements{
|
|
|
|
Limits: v1.ResourceList{
|
|
|
|
v1.ResourceName(v1.ResourceCPU): resource.MustParse("10"),
|
2016-05-04 00:30:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-08-04 00:28:33 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-11-18 20:50:58 +00:00
|
|
|
memoryLimitCPURequest = v1.Pod{
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
2016-05-04 00:30:09 +00:00
|
|
|
{
|
2016-11-18 20:50:58 +00:00
|
|
|
Resources: v1.ResourceRequirements{
|
|
|
|
Requests: v1.ResourceList{
|
|
|
|
v1.ResourceName(v1.ResourceCPU): resource.MustParse("0"),
|
2016-05-04 00:30:09 +00:00
|
|
|
},
|
2016-11-18 20:50:58 +00:00
|
|
|
Limits: v1.ResourceList{
|
|
|
|
v1.ResourceName(v1.ResourceMemory): resource.MustParse("10G"),
|
2016-05-04 00:30:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-08-04 00:28:33 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-11-18 20:50:58 +00:00
|
|
|
zeroMemoryLimit = v1.Pod{
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
2016-05-04 00:30:09 +00:00
|
|
|
{
|
2016-11-18 20:50:58 +00:00
|
|
|
Resources: v1.ResourceRequirements{
|
|
|
|
Limits: v1.ResourceList{
|
|
|
|
v1.ResourceName(v1.ResourceMemory): resource.MustParse("0"),
|
2016-05-04 00:30:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-08-04 00:28:33 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-11-18 20:50:58 +00:00
|
|
|
noRequestLimit = v1.Pod{
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
2016-05-04 00:30:09 +00:00
|
|
|
{
|
2016-11-18 20:50:58 +00:00
|
|
|
Resources: v1.ResourceRequirements{},
|
2016-05-04 00:30:09 +00:00
|
|
|
},
|
2015-08-04 00:28:33 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-11-18 20:50:58 +00:00
|
|
|
equalRequestLimitCPUMemory = v1.Pod{
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
2016-05-04 00:30:09 +00:00
|
|
|
{
|
2016-11-18 20:50:58 +00:00
|
|
|
Resources: v1.ResourceRequirements{
|
|
|
|
Requests: v1.ResourceList{
|
|
|
|
v1.ResourceName(v1.ResourceMemory): resource.MustParse("10G"),
|
|
|
|
v1.ResourceName(v1.ResourceCPU): resource.MustParse("5m"),
|
2016-05-04 00:30:09 +00:00
|
|
|
},
|
2016-11-18 20:50:58 +00:00
|
|
|
Limits: v1.ResourceList{
|
|
|
|
v1.ResourceName(v1.ResourceCPU): resource.MustParse("5m"),
|
|
|
|
v1.ResourceName(v1.ResourceMemory): resource.MustParse("10G"),
|
2016-05-04 00:30:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-08-04 00:28:33 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-11-18 20:50:58 +00:00
|
|
|
cpuUnlimitedMemoryLimitedWithRequests = v1.Pod{
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
2016-05-04 00:30:09 +00:00
|
|
|
{
|
2016-11-18 20:50:58 +00:00
|
|
|
Resources: v1.ResourceRequirements{
|
|
|
|
Requests: v1.ResourceList{
|
2018-02-02 21:51:59 +00:00
|
|
|
v1.ResourceName(v1.ResourceMemory): resource.MustParse(strconv.FormatInt(standardMemoryAmount/2, 10)),
|
2016-11-18 20:50:58 +00:00
|
|
|
v1.ResourceName(v1.ResourceCPU): resource.MustParse("5m"),
|
2016-05-04 00:30:09 +00:00
|
|
|
},
|
2016-11-18 20:50:58 +00:00
|
|
|
Limits: v1.ResourceList{
|
|
|
|
v1.ResourceName(v1.ResourceMemory): resource.MustParse("10G"),
|
2016-05-04 00:30:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-08-04 00:28:33 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-11-18 20:50:58 +00:00
|
|
|
requestNoLimit = v1.Pod{
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
2016-05-04 00:30:09 +00:00
|
|
|
{
|
2016-11-18 20:50:58 +00:00
|
|
|
Resources: v1.ResourceRequirements{
|
|
|
|
Requests: v1.ResourceList{
|
2018-02-02 21:51:59 +00:00
|
|
|
v1.ResourceName(v1.ResourceMemory): resource.MustParse(strconv.FormatInt(standardMemoryAmount-1, 10)),
|
2016-11-18 20:50:58 +00:00
|
|
|
v1.ResourceName(v1.ResourceCPU): resource.MustParse("5m"),
|
2016-05-04 00:30:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-08-04 00:28:33 +00:00
|
|
|
}
|
2019-02-05 18:29:37 +00:00
|
|
|
|
|
|
|
systemCritical = scheduling.SystemCriticalPriority
|
|
|
|
|
|
|
|
critical = v1.Pod{
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Priority: &systemCritical,
|
|
|
|
Containers: []v1.Container{
|
|
|
|
{
|
|
|
|
Resources: v1.ResourceRequirements{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-05-04 00:30:09 +00:00
|
|
|
)
|
2015-08-04 00:28:33 +00:00
|
|
|
|
|
|
|
type oomTest struct {
|
2016-11-18 20:50:58 +00:00
|
|
|
pod *v1.Pod
|
2015-08-04 00:28:33 +00:00
|
|
|
memoryCapacity int64
|
2015-09-28 08:00:43 +00:00
|
|
|
lowOOMScoreAdj int // The max oom_score_adj score the container should be assigned.
|
|
|
|
highOOMScoreAdj int // The min oom_score_adj score the container should be assigned.
|
2015-08-04 00:28:33 +00:00
|
|
|
}
|
|
|
|
|
2015-09-28 08:00:43 +00:00
|
|
|
func TestGetContainerOOMScoreAdjust(t *testing.T) {
|
2015-08-04 00:28:33 +00:00
|
|
|
oomTests := []oomTest{
|
|
|
|
{
|
2016-05-04 00:30:09 +00:00
|
|
|
pod: &cpuLimit,
|
2015-08-04 00:28:33 +00:00
|
|
|
memoryCapacity: 4000000000,
|
2016-05-04 00:30:09 +00:00
|
|
|
lowOOMScoreAdj: 999,
|
|
|
|
highOOMScoreAdj: 999,
|
2015-08-04 00:28:33 +00:00
|
|
|
},
|
|
|
|
{
|
2016-05-04 00:30:09 +00:00
|
|
|
pod: &memoryLimitCPURequest,
|
2015-08-04 00:28:33 +00:00
|
|
|
memoryCapacity: 8000000000,
|
2016-05-04 00:30:09 +00:00
|
|
|
lowOOMScoreAdj: 999,
|
|
|
|
highOOMScoreAdj: 999,
|
2015-08-04 00:28:33 +00:00
|
|
|
},
|
|
|
|
{
|
2016-05-04 00:30:09 +00:00
|
|
|
pod: &zeroMemoryLimit,
|
2015-08-04 00:28:33 +00:00
|
|
|
memoryCapacity: 7230457451,
|
2015-09-28 08:00:43 +00:00
|
|
|
lowOOMScoreAdj: 1000,
|
|
|
|
highOOMScoreAdj: 1000,
|
2015-08-04 00:28:33 +00:00
|
|
|
},
|
|
|
|
{
|
2016-05-04 00:30:09 +00:00
|
|
|
pod: &noRequestLimit,
|
2015-08-04 00:28:33 +00:00
|
|
|
memoryCapacity: 4000000000,
|
2015-09-28 08:00:43 +00:00
|
|
|
lowOOMScoreAdj: 1000,
|
|
|
|
highOOMScoreAdj: 1000,
|
2015-08-04 00:28:33 +00:00
|
|
|
},
|
|
|
|
{
|
2016-05-04 00:30:09 +00:00
|
|
|
pod: &equalRequestLimitCPUMemory,
|
2015-08-04 00:28:33 +00:00
|
|
|
memoryCapacity: 123456789,
|
2016-05-04 00:30:09 +00:00
|
|
|
lowOOMScoreAdj: -998,
|
|
|
|
highOOMScoreAdj: -998,
|
2015-08-04 00:28:33 +00:00
|
|
|
},
|
|
|
|
{
|
2016-05-04 00:30:09 +00:00
|
|
|
pod: &cpuUnlimitedMemoryLimitedWithRequests,
|
2015-08-04 00:28:33 +00:00
|
|
|
memoryCapacity: standardMemoryAmount,
|
2015-09-28 08:00:43 +00:00
|
|
|
lowOOMScoreAdj: 495,
|
|
|
|
highOOMScoreAdj: 505,
|
2015-08-04 00:28:33 +00:00
|
|
|
},
|
|
|
|
{
|
2016-05-04 00:30:09 +00:00
|
|
|
pod: &requestNoLimit,
|
2015-08-04 00:28:33 +00:00
|
|
|
memoryCapacity: standardMemoryAmount,
|
2015-09-28 08:00:43 +00:00
|
|
|
lowOOMScoreAdj: 2,
|
|
|
|
highOOMScoreAdj: 2,
|
2015-08-04 00:28:33 +00:00
|
|
|
},
|
2019-02-05 18:29:37 +00:00
|
|
|
{
|
|
|
|
pod: &critical,
|
|
|
|
memoryCapacity: 4000000000,
|
|
|
|
lowOOMScoreAdj: -998,
|
|
|
|
highOOMScoreAdj: -998,
|
|
|
|
},
|
2015-08-04 00:28:33 +00:00
|
|
|
}
|
|
|
|
for _, test := range oomTests {
|
2016-05-04 00:30:09 +00:00
|
|
|
oomScoreAdj := GetContainerOOMScoreAdjust(test.pod, &test.pod.Spec.Containers[0], test.memoryCapacity)
|
2015-09-28 08:00:43 +00:00
|
|
|
if oomScoreAdj < test.lowOOMScoreAdj || oomScoreAdj > test.highOOMScoreAdj {
|
|
|
|
t.Errorf("oom_score_adj should be between %d and %d, but was %d", test.lowOOMScoreAdj, test.highOOMScoreAdj, oomScoreAdj)
|
2015-08-04 00:28:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|