k8s.io/kubernetes@v1.29.3/pkg/kubelet/cm/qos_container_manager_linux_test.go (about) 1 //go:build linux 2 // +build linux 3 4 /* 5 Copyright 2021 The Kubernetes Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 */ 19 20 package cm 21 22 import ( 23 "fmt" 24 "strconv" 25 "testing" 26 27 "github.com/stretchr/testify/assert" 28 v1 "k8s.io/api/core/v1" 29 "k8s.io/apimachinery/pkg/api/resource" 30 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 31 ) 32 33 func activeTestPods() []*v1.Pod { 34 return []*v1.Pod{ 35 { 36 ObjectMeta: metav1.ObjectMeta{ 37 UID: "12345678", 38 Name: "guaranteed-pod", 39 Namespace: "test", 40 }, 41 Spec: v1.PodSpec{ 42 Containers: []v1.Container{ 43 { 44 Name: "foo", 45 Image: "busybox", 46 Resources: v1.ResourceRequirements{ 47 Requests: v1.ResourceList{ 48 v1.ResourceMemory: resource.MustParse("128Mi"), 49 v1.ResourceCPU: resource.MustParse("1"), 50 }, 51 Limits: v1.ResourceList{ 52 v1.ResourceMemory: resource.MustParse("128Mi"), 53 v1.ResourceCPU: resource.MustParse("1"), 54 }, 55 }, 56 }, 57 }, 58 }, 59 }, 60 { 61 ObjectMeta: metav1.ObjectMeta{ 62 UID: "87654321", 63 Name: "burstable-pod-1", 64 Namespace: "test", 65 }, 66 Spec: v1.PodSpec{ 67 Containers: []v1.Container{ 68 { 69 Name: "foo", 70 Image: "busybox", 71 Resources: v1.ResourceRequirements{ 72 Requests: v1.ResourceList{ 73 v1.ResourceMemory: resource.MustParse("128Mi"), 74 v1.ResourceCPU: resource.MustParse("1"), 75 }, 76 Limits: v1.ResourceList{ 77 v1.ResourceMemory: resource.MustParse("256Mi"), 78 v1.ResourceCPU: resource.MustParse("2"), 79 }, 80 }, 81 }, 82 }, 83 }, 84 }, 85 { 86 ObjectMeta: metav1.ObjectMeta{ 87 UID: "01234567", 88 Name: "burstable-pod-2", 89 Namespace: "test", 90 }, 91 Spec: v1.PodSpec{ 92 Containers: []v1.Container{ 93 { 94 Name: "foo", 95 Image: "busybox", 96 Resources: v1.ResourceRequirements{ 97 Requests: v1.ResourceList{ 98 v1.ResourceMemory: resource.MustParse("256Mi"), 99 v1.ResourceCPU: resource.MustParse("2"), 100 }, 101 }, 102 }, 103 }, 104 }, 105 }, 106 } 107 } 108 109 func createTestQOSContainerManager() (*qosContainerManagerImpl, error) { 110 subsystems, err := GetCgroupSubsystems() 111 if err != nil { 112 return nil, fmt.Errorf("failed to get mounted cgroup subsystems: %v", err) 113 } 114 115 cgroupRoot := ParseCgroupfsToCgroupName("/") 116 cgroupRoot = NewCgroupName(cgroupRoot, defaultNodeAllocatableCgroupName) 117 118 qosContainerManager := &qosContainerManagerImpl{ 119 subsystems: subsystems, 120 cgroupManager: NewCgroupManager(subsystems, "cgroupfs"), 121 cgroupRoot: cgroupRoot, 122 qosReserved: nil, 123 } 124 125 qosContainerManager.activePods = activeTestPods 126 127 return qosContainerManager, nil 128 } 129 130 func TestQoSContainerCgroup(t *testing.T) { 131 m, err := createTestQOSContainerManager() 132 assert.Nil(t, err) 133 134 qosConfigs := map[v1.PodQOSClass]*CgroupConfig{ 135 v1.PodQOSGuaranteed: { 136 Name: m.qosContainersInfo.Guaranteed, 137 ResourceParameters: &ResourceConfig{}, 138 }, 139 v1.PodQOSBurstable: { 140 Name: m.qosContainersInfo.Burstable, 141 ResourceParameters: &ResourceConfig{}, 142 }, 143 v1.PodQOSBestEffort: { 144 Name: m.qosContainersInfo.BestEffort, 145 ResourceParameters: &ResourceConfig{}, 146 }, 147 } 148 149 m.setMemoryQoS(qosConfigs) 150 151 burstableMin := resource.MustParse("384Mi") 152 guaranteedMin := resource.MustParse("128Mi") 153 assert.Equal(t, qosConfigs[v1.PodQOSGuaranteed].ResourceParameters.Unified["memory.min"], strconv.FormatInt(burstableMin.Value()+guaranteedMin.Value(), 10)) 154 assert.Equal(t, qosConfigs[v1.PodQOSBurstable].ResourceParameters.Unified["memory.min"], strconv.FormatInt(burstableMin.Value(), 10)) 155 }