123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625 |
- // +build linux
- /*
- Copyright 2015 The Kubernetes Authors.
- 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 cm
- import (
- "reflect"
- "strconv"
- "testing"
- "time"
- "k8s.io/api/core/v1"
- "k8s.io/apimachinery/pkg/api/resource"
- utilfeature "k8s.io/apiserver/pkg/util/feature"
- featuregatetesting "k8s.io/component-base/featuregate/testing"
- pkgfeatures "k8s.io/kubernetes/pkg/features"
- )
- // getResourceList returns a ResourceList with the
- // specified cpu and memory resource values
- func getResourceList(cpu, memory string) v1.ResourceList {
- res := v1.ResourceList{}
- if cpu != "" {
- res[v1.ResourceCPU] = resource.MustParse(cpu)
- }
- if memory != "" {
- res[v1.ResourceMemory] = resource.MustParse(memory)
- }
- return res
- }
- // getResourceRequirements returns a ResourceRequirements object
- func getResourceRequirements(requests, limits v1.ResourceList) v1.ResourceRequirements {
- res := v1.ResourceRequirements{}
- res.Requests = requests
- res.Limits = limits
- return res
- }
- func TestResourceConfigForPod(t *testing.T) {
- defaultQuotaPeriod := uint64(100 * time.Millisecond / time.Microsecond)
- tunedQuotaPeriod := uint64(5 * time.Millisecond / time.Microsecond)
- minShares := uint64(MinShares)
- burstableShares := MilliCPUToShares(100)
- memoryQuantity := resource.MustParse("200Mi")
- burstableMemory := memoryQuantity.Value()
- burstablePartialShares := MilliCPUToShares(200)
- burstableQuota := MilliCPUToQuota(200, int64(defaultQuotaPeriod))
- guaranteedShares := MilliCPUToShares(100)
- guaranteedQuota := MilliCPUToQuota(100, int64(defaultQuotaPeriod))
- guaranteedTunedQuota := MilliCPUToQuota(100, int64(tunedQuotaPeriod))
- memoryQuantity = resource.MustParse("100Mi")
- cpuNoLimit := int64(-1)
- guaranteedMemory := memoryQuantity.Value()
- testCases := map[string]struct {
- pod *v1.Pod
- expected *ResourceConfig
- enforceCPULimits bool
- quotaPeriod uint64
- }{
- "besteffort": {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Resources: getResourceRequirements(getResourceList("", ""), getResourceList("", "")),
- },
- },
- },
- },
- enforceCPULimits: true,
- quotaPeriod: defaultQuotaPeriod,
- expected: &ResourceConfig{CpuShares: &minShares},
- },
- "burstable-no-limits": {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Resources: getResourceRequirements(getResourceList("100m", "100Mi"), getResourceList("", "")),
- },
- },
- },
- },
- enforceCPULimits: true,
- quotaPeriod: defaultQuotaPeriod,
- expected: &ResourceConfig{CpuShares: &burstableShares},
- },
- "burstable-with-limits": {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Resources: getResourceRequirements(getResourceList("100m", "100Mi"), getResourceList("200m", "200Mi")),
- },
- },
- },
- },
- enforceCPULimits: true,
- quotaPeriod: defaultQuotaPeriod,
- expected: &ResourceConfig{CpuShares: &burstableShares, CpuQuota: &burstableQuota, CpuPeriod: &defaultQuotaPeriod, Memory: &burstableMemory},
- },
- "burstable-with-limits-no-cpu-enforcement": {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Resources: getResourceRequirements(getResourceList("100m", "100Mi"), getResourceList("200m", "200Mi")),
- },
- },
- },
- },
- enforceCPULimits: false,
- quotaPeriod: defaultQuotaPeriod,
- expected: &ResourceConfig{CpuShares: &burstableShares, CpuQuota: &cpuNoLimit, CpuPeriod: &defaultQuotaPeriod, Memory: &burstableMemory},
- },
- "burstable-partial-limits": {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Resources: getResourceRequirements(getResourceList("100m", "100Mi"), getResourceList("200m", "200Mi")),
- },
- {
- Resources: getResourceRequirements(getResourceList("100m", "100Mi"), getResourceList("", "")),
- },
- },
- },
- },
- enforceCPULimits: true,
- quotaPeriod: defaultQuotaPeriod,
- expected: &ResourceConfig{CpuShares: &burstablePartialShares},
- },
- "burstable-with-limits-with-tuned-quota": {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Resources: getResourceRequirements(getResourceList("100m", "100Mi"), getResourceList("200m", "200Mi")),
- },
- },
- },
- },
- enforceCPULimits: true,
- quotaPeriod: tunedQuotaPeriod,
- expected: &ResourceConfig{CpuShares: &burstableShares, CpuQuota: &burstableQuota, CpuPeriod: &tunedQuotaPeriod, Memory: &burstableMemory},
- },
- "burstable-with-limits-no-cpu-enforcement-with-tuned-quota": {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Resources: getResourceRequirements(getResourceList("100m", "100Mi"), getResourceList("200m", "200Mi")),
- },
- },
- },
- },
- enforceCPULimits: false,
- quotaPeriod: tunedQuotaPeriod,
- expected: &ResourceConfig{CpuShares: &burstableShares, CpuQuota: &cpuNoLimit, CpuPeriod: &tunedQuotaPeriod, Memory: &burstableMemory},
- },
- "burstable-partial-limits-with-tuned-quota": {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Resources: getResourceRequirements(getResourceList("100m", "100Mi"), getResourceList("200m", "200Mi")),
- },
- {
- Resources: getResourceRequirements(getResourceList("100m", "100Mi"), getResourceList("", "")),
- },
- },
- },
- },
- enforceCPULimits: true,
- quotaPeriod: tunedQuotaPeriod,
- expected: &ResourceConfig{CpuShares: &burstablePartialShares},
- },
- "guaranteed": {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Resources: getResourceRequirements(getResourceList("100m", "100Mi"), getResourceList("100m", "100Mi")),
- },
- },
- },
- },
- enforceCPULimits: true,
- quotaPeriod: defaultQuotaPeriod,
- expected: &ResourceConfig{CpuShares: &guaranteedShares, CpuQuota: &guaranteedQuota, CpuPeriod: &defaultQuotaPeriod, Memory: &guaranteedMemory},
- },
- "guaranteed-no-cpu-enforcement": {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Resources: getResourceRequirements(getResourceList("100m", "100Mi"), getResourceList("100m", "100Mi")),
- },
- },
- },
- },
- enforceCPULimits: false,
- quotaPeriod: defaultQuotaPeriod,
- expected: &ResourceConfig{CpuShares: &guaranteedShares, CpuQuota: &cpuNoLimit, CpuPeriod: &defaultQuotaPeriod, Memory: &guaranteedMemory},
- },
- "guaranteed-with-tuned-quota": {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Resources: getResourceRequirements(getResourceList("100m", "100Mi"), getResourceList("100m", "100Mi")),
- },
- },
- },
- },
- enforceCPULimits: true,
- quotaPeriod: tunedQuotaPeriod,
- expected: &ResourceConfig{CpuShares: &guaranteedShares, CpuQuota: &guaranteedTunedQuota, CpuPeriod: &tunedQuotaPeriod, Memory: &guaranteedMemory},
- },
- "guaranteed-no-cpu-enforcement-with-tuned-quota": {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Resources: getResourceRequirements(getResourceList("100m", "100Mi"), getResourceList("100m", "100Mi")),
- },
- },
- },
- },
- enforceCPULimits: false,
- quotaPeriod: tunedQuotaPeriod,
- expected: &ResourceConfig{CpuShares: &guaranteedShares, CpuQuota: &cpuNoLimit, CpuPeriod: &tunedQuotaPeriod, Memory: &guaranteedMemory},
- },
- }
- for testName, testCase := range testCases {
- actual := ResourceConfigForPod(testCase.pod, testCase.enforceCPULimits, testCase.quotaPeriod)
- if !reflect.DeepEqual(actual.CpuPeriod, testCase.expected.CpuPeriod) {
- t.Errorf("unexpected result, test: %v, cpu period not as expected", testName)
- }
- if !reflect.DeepEqual(actual.CpuQuota, testCase.expected.CpuQuota) {
- t.Errorf("unexpected result, test: %v, cpu quota not as expected", testName)
- }
- if !reflect.DeepEqual(actual.CpuShares, testCase.expected.CpuShares) {
- t.Errorf("unexpected result, test: %v, cpu shares not as expected", testName)
- }
- if !reflect.DeepEqual(actual.Memory, testCase.expected.Memory) {
- t.Errorf("unexpected result, test: %v, memory not as expected", testName)
- }
- }
- }
- func TestResourceConfigForPodWithCustomCPUCFSQuotaPeriod(t *testing.T) {
- defaultQuotaPeriod := uint64(100 * time.Millisecond / time.Microsecond)
- tunedQuotaPeriod := uint64(5 * time.Millisecond / time.Microsecond)
- tunedQuota := int64(1 * time.Millisecond / time.Microsecond)
- defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, pkgfeatures.CPUCFSQuotaPeriod, true)()
- minShares := uint64(MinShares)
- burstableShares := MilliCPUToShares(100)
- memoryQuantity := resource.MustParse("200Mi")
- burstableMemory := memoryQuantity.Value()
- burstablePartialShares := MilliCPUToShares(200)
- burstableQuota := MilliCPUToQuota(200, int64(defaultQuotaPeriod))
- guaranteedShares := MilliCPUToShares(100)
- guaranteedQuota := MilliCPUToQuota(100, int64(defaultQuotaPeriod))
- guaranteedTunedQuota := MilliCPUToQuota(100, int64(tunedQuotaPeriod))
- memoryQuantity = resource.MustParse("100Mi")
- cpuNoLimit := int64(-1)
- guaranteedMemory := memoryQuantity.Value()
- testCases := map[string]struct {
- pod *v1.Pod
- expected *ResourceConfig
- enforceCPULimits bool
- quotaPeriod uint64
- }{
- "besteffort": {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Resources: getResourceRequirements(getResourceList("", ""), getResourceList("", "")),
- },
- },
- },
- },
- enforceCPULimits: true,
- quotaPeriod: defaultQuotaPeriod,
- expected: &ResourceConfig{CpuShares: &minShares},
- },
- "burstable-no-limits": {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Resources: getResourceRequirements(getResourceList("100m", "100Mi"), getResourceList("", "")),
- },
- },
- },
- },
- enforceCPULimits: true,
- quotaPeriod: defaultQuotaPeriod,
- expected: &ResourceConfig{CpuShares: &burstableShares},
- },
- "burstable-with-limits": {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Resources: getResourceRequirements(getResourceList("100m", "100Mi"), getResourceList("200m", "200Mi")),
- },
- },
- },
- },
- enforceCPULimits: true,
- quotaPeriod: defaultQuotaPeriod,
- expected: &ResourceConfig{CpuShares: &burstableShares, CpuQuota: &burstableQuota, CpuPeriod: &defaultQuotaPeriod, Memory: &burstableMemory},
- },
- "burstable-with-limits-no-cpu-enforcement": {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Resources: getResourceRequirements(getResourceList("100m", "100Mi"), getResourceList("200m", "200Mi")),
- },
- },
- },
- },
- enforceCPULimits: false,
- quotaPeriod: defaultQuotaPeriod,
- expected: &ResourceConfig{CpuShares: &burstableShares, CpuQuota: &cpuNoLimit, CpuPeriod: &defaultQuotaPeriod, Memory: &burstableMemory},
- },
- "burstable-partial-limits": {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Resources: getResourceRequirements(getResourceList("100m", "100Mi"), getResourceList("200m", "200Mi")),
- },
- {
- Resources: getResourceRequirements(getResourceList("100m", "100Mi"), getResourceList("", "")),
- },
- },
- },
- },
- enforceCPULimits: true,
- quotaPeriod: defaultQuotaPeriod,
- expected: &ResourceConfig{CpuShares: &burstablePartialShares},
- },
- "burstable-with-limits-with-tuned-quota": {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Resources: getResourceRequirements(getResourceList("100m", "100Mi"), getResourceList("200m", "200Mi")),
- },
- },
- },
- },
- enforceCPULimits: true,
- quotaPeriod: tunedQuotaPeriod,
- expected: &ResourceConfig{CpuShares: &burstableShares, CpuQuota: &tunedQuota, CpuPeriod: &tunedQuotaPeriod, Memory: &burstableMemory},
- },
- "burstable-with-limits-no-cpu-enforcement-with-tuned-quota": {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Resources: getResourceRequirements(getResourceList("100m", "100Mi"), getResourceList("200m", "200Mi")),
- },
- },
- },
- },
- enforceCPULimits: false,
- quotaPeriod: tunedQuotaPeriod,
- expected: &ResourceConfig{CpuShares: &burstableShares, CpuQuota: &cpuNoLimit, CpuPeriod: &tunedQuotaPeriod, Memory: &burstableMemory},
- },
- "burstable-partial-limits-with-tuned-quota": {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Resources: getResourceRequirements(getResourceList("100m", "100Mi"), getResourceList("200m", "200Mi")),
- },
- {
- Resources: getResourceRequirements(getResourceList("100m", "100Mi"), getResourceList("", "")),
- },
- },
- },
- },
- enforceCPULimits: true,
- quotaPeriod: tunedQuotaPeriod,
- expected: &ResourceConfig{CpuShares: &burstablePartialShares},
- },
- "guaranteed": {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Resources: getResourceRequirements(getResourceList("100m", "100Mi"), getResourceList("100m", "100Mi")),
- },
- },
- },
- },
- enforceCPULimits: true,
- quotaPeriod: defaultQuotaPeriod,
- expected: &ResourceConfig{CpuShares: &guaranteedShares, CpuQuota: &guaranteedQuota, CpuPeriod: &defaultQuotaPeriod, Memory: &guaranteedMemory},
- },
- "guaranteed-no-cpu-enforcement": {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Resources: getResourceRequirements(getResourceList("100m", "100Mi"), getResourceList("100m", "100Mi")),
- },
- },
- },
- },
- enforceCPULimits: false,
- quotaPeriod: defaultQuotaPeriod,
- expected: &ResourceConfig{CpuShares: &guaranteedShares, CpuQuota: &cpuNoLimit, CpuPeriod: &defaultQuotaPeriod, Memory: &guaranteedMemory},
- },
- "guaranteed-with-tuned-quota": {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Resources: getResourceRequirements(getResourceList("100m", "100Mi"), getResourceList("100m", "100Mi")),
- },
- },
- },
- },
- enforceCPULimits: true,
- quotaPeriod: tunedQuotaPeriod,
- expected: &ResourceConfig{CpuShares: &guaranteedShares, CpuQuota: &guaranteedTunedQuota, CpuPeriod: &tunedQuotaPeriod, Memory: &guaranteedMemory},
- },
- "guaranteed-no-cpu-enforcement-with-tuned-quota": {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Resources: getResourceRequirements(getResourceList("100m", "100Mi"), getResourceList("100m", "100Mi")),
- },
- },
- },
- },
- enforceCPULimits: false,
- quotaPeriod: tunedQuotaPeriod,
- expected: &ResourceConfig{CpuShares: &guaranteedShares, CpuQuota: &cpuNoLimit, CpuPeriod: &tunedQuotaPeriod, Memory: &guaranteedMemory},
- },
- }
- for testName, testCase := range testCases {
- actual := ResourceConfigForPod(testCase.pod, testCase.enforceCPULimits, testCase.quotaPeriod)
- if !reflect.DeepEqual(actual.CpuPeriod, testCase.expected.CpuPeriod) {
- t.Errorf("unexpected result, test: %v, cpu period not as expected", testName)
- }
- if !reflect.DeepEqual(actual.CpuQuota, testCase.expected.CpuQuota) {
- t.Errorf("unexpected result, test: %v, cpu quota not as expected", testName)
- }
- if !reflect.DeepEqual(actual.CpuShares, testCase.expected.CpuShares) {
- t.Errorf("unexpected result, test: %v, cpu shares not as expected", testName)
- }
- if !reflect.DeepEqual(actual.Memory, testCase.expected.Memory) {
- t.Errorf("unexpected result, test: %v, memory not as expected", testName)
- }
- }
- }
- func TestMilliCPUToQuota(t *testing.T) {
- testCases := []struct {
- input int64
- quota int64
- period uint64
- }{
- {
- input: int64(0),
- quota: int64(0),
- period: uint64(0),
- },
- {
- input: int64(5),
- quota: int64(1000),
- period: uint64(100000),
- },
- {
- input: int64(9),
- quota: int64(1000),
- period: uint64(100000),
- },
- {
- input: int64(10),
- quota: int64(1000),
- period: uint64(100000),
- },
- {
- input: int64(200),
- quota: int64(20000),
- period: uint64(100000),
- },
- {
- input: int64(500),
- quota: int64(50000),
- period: uint64(100000),
- },
- {
- input: int64(1000),
- quota: int64(100000),
- period: uint64(100000),
- },
- {
- input: int64(1500),
- quota: int64(150000),
- period: uint64(100000),
- },
- }
- for _, testCase := range testCases {
- quota := MilliCPUToQuota(testCase.input, int64(testCase.period))
- if quota != testCase.quota {
- t.Errorf("Input %v and %v, expected quota %v, but got quota %v", testCase.input, testCase.period, testCase.quota, quota)
- }
- }
- }
- func TestHugePageLimits(t *testing.T) {
- Mi := int64(1024 * 1024)
- type inputStruct struct {
- key string
- input string
- }
- testCases := []struct {
- name string
- inputs []inputStruct
- expected map[int64]int64
- }{
- {
- name: "no valid hugepages",
- inputs: []inputStruct{
- {
- key: "2Mi",
- input: "128",
- },
- },
- expected: map[int64]int64{},
- },
- {
- name: "2Mi only",
- inputs: []inputStruct{
- {
- key: v1.ResourceHugePagesPrefix + "2Mi",
- input: "128",
- },
- },
- expected: map[int64]int64{2 * Mi: 128},
- },
- {
- name: "2Mi and 4Mi",
- inputs: []inputStruct{
- {
- key: v1.ResourceHugePagesPrefix + "2Mi",
- input: "128",
- },
- {
- key: v1.ResourceHugePagesPrefix + strconv.FormatInt(2*Mi, 10),
- input: "256",
- },
- {
- key: v1.ResourceHugePagesPrefix + "4Mi",
- input: "512",
- },
- {
- key: "4Mi",
- input: "1024",
- },
- },
- expected: map[int64]int64{2 * Mi: 384, 4 * Mi: 512},
- },
- }
- for _, testcase := range testCases {
- t.Run(testcase.name, func(t *testing.T) {
- resourceList := v1.ResourceList{}
- for _, input := range testcase.inputs {
- value, err := resource.ParseQuantity(input.input)
- if err != nil {
- t.Fatalf("error in parsing hugepages, value: %s", input.input)
- } else {
- resourceList[v1.ResourceName(input.key)] = value
- }
- }
- resultValue := HugePageLimits(resourceList)
- if !reflect.DeepEqual(testcase.expected, resultValue) {
- t.Errorf("unexpected result, expected: %v, actual: %v", testcase.expected, resultValue)
- }
- })
- }
- }
|