github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/evictionmanager/plugin/memory/helper_test.go (about) 1 /* 2 Copyright 2022 The Katalyst Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package memory 18 19 import ( 20 "testing" 21 "time" 22 23 "github.com/stretchr/testify/assert" 24 v1 "k8s.io/api/core/v1" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 27 apiconsts "github.com/kubewharf/katalyst-api/pkg/consts" 28 "github.com/kubewharf/katalyst-core/pkg/consts" 29 "github.com/kubewharf/katalyst-core/pkg/metaserver" 30 "github.com/kubewharf/katalyst-core/pkg/metaserver/agent/metric" 31 "github.com/kubewharf/katalyst-core/pkg/metrics" 32 "github.com/kubewharf/katalyst-core/pkg/util/general" 33 "github.com/kubewharf/katalyst-core/pkg/util/machine" 34 utilmetric "github.com/kubewharf/katalyst-core/pkg/util/metric" 35 "github.com/kubewharf/katalyst-core/pkg/util/native" 36 ) 37 38 func makeHelper(metaServer *metaserver.MetaServer) (*EvictionHelper, error) { 39 conf := makeConf() 40 41 cpuTopology, err := machine.GenerateDummyCPUTopology(16, 1, 2) 42 if err != nil { 43 return nil, err 44 } 45 46 metaServer.KatalystMachineInfo = &machine.KatalystMachineInfo{ 47 CPUTopology: cpuTopology, 48 } 49 metaServer.MetricsFetcher = metric.NewFakeMetricsFetcher(metrics.DummyMetrics{}) 50 51 return NewEvictionHelper(&metrics.DummyMetrics{}, metaServer, conf), nil 52 } 53 54 func TestEvictionHelper_getEvictionCmpFuncs(t *testing.T) { 55 t.Parallel() 56 57 metaServer := makeMetaServer() 58 helper, err := makeHelper(metaServer) 59 assert.NoError(t, err) 60 assert.NotNil(t, helper) 61 62 conf := makeConf() 63 64 fakeMetricsFetcher := metaServer.MetricsFetcher.(*metric.FakeMetricsFetcher) 65 assert.NotNil(t, fakeMetricsFetcher) 66 67 pods := []*v1.Pod{ 68 { 69 ObjectMeta: metav1.ObjectMeta{ 70 UID: "000001", 71 Name: "pod-1", 72 Annotations: map[string]string{ 73 apiconsts.PodAnnotationQoSLevelKey: apiconsts.PodAnnotationQoSLevelReclaimedCores, 74 }, 75 }, 76 Spec: v1.PodSpec{ 77 Priority: &lowPriority, 78 Containers: []v1.Container{ 79 { 80 Name: "c", 81 }, 82 }, 83 }, 84 }, 85 { 86 ObjectMeta: metav1.ObjectMeta{ 87 UID: "000002", 88 Name: "pod-2", 89 Annotations: map[string]string{ 90 apiconsts.PodAnnotationQoSLevelKey: apiconsts.PodAnnotationQoSLevelReclaimedCores, 91 }, 92 }, 93 Spec: v1.PodSpec{ 94 Priority: &lowPriority, 95 Containers: []v1.Container{ 96 { 97 Name: "c", 98 }, 99 }, 100 }, 101 }, 102 { 103 ObjectMeta: metav1.ObjectMeta{ 104 UID: "000003", 105 Name: "pod-3", 106 Annotations: map[string]string{ 107 apiconsts.PodAnnotationQoSLevelKey: apiconsts.PodAnnotationQoSLevelReclaimedCores, 108 }, 109 }, 110 Spec: v1.PodSpec{ 111 Priority: &highPriority, 112 Containers: []v1.Container{ 113 { 114 Name: "c", 115 }, 116 }, 117 }, 118 }, 119 { 120 ObjectMeta: metav1.ObjectMeta{ 121 UID: "000004", 122 Name: "pod-4", 123 Annotations: map[string]string{ 124 apiconsts.PodAnnotationQoSLevelKey: apiconsts.PodAnnotationQoSLevelReclaimedCores, 125 }, 126 }, 127 Spec: v1.PodSpec{ 128 Priority: &highPriority, 129 Containers: []v1.Container{ 130 { 131 Name: "c", 132 }, 133 }, 134 }, 135 }, 136 { 137 ObjectMeta: metav1.ObjectMeta{ 138 UID: "000005", 139 Name: "pod-5", 140 Annotations: map[string]string{ 141 apiconsts.PodAnnotationQoSLevelKey: apiconsts.PodAnnotationQoSLevelSharedCores, 142 }, 143 }, 144 Spec: v1.PodSpec{ 145 Priority: &lowPriority, 146 Containers: []v1.Container{ 147 { 148 Name: "c", 149 }, 150 }, 151 }, 152 }, 153 { 154 ObjectMeta: metav1.ObjectMeta{ 155 UID: "000006", 156 Name: "pod-6", 157 Annotations: map[string]string{ 158 apiconsts.PodAnnotationQoSLevelKey: apiconsts.PodAnnotationQoSLevelSharedCores, 159 }, 160 }, 161 Spec: v1.PodSpec{ 162 Priority: &lowPriority, 163 Containers: []v1.Container{ 164 { 165 Name: "c", 166 }, 167 }, 168 }, 169 }, 170 { 171 ObjectMeta: metav1.ObjectMeta{ 172 UID: "000007", 173 Name: "pod-7", 174 Annotations: map[string]string{ 175 apiconsts.PodAnnotationQoSLevelKey: apiconsts.PodAnnotationQoSLevelSharedCores, 176 }, 177 }, 178 Spec: v1.PodSpec{ 179 Priority: &highPriority, 180 Containers: []v1.Container{ 181 { 182 Name: "c", 183 }, 184 }, 185 }, 186 }, 187 { 188 ObjectMeta: metav1.ObjectMeta{ 189 UID: "000008", 190 Name: "pod-8", 191 Annotations: map[string]string{ 192 apiconsts.PodAnnotationQoSLevelKey: apiconsts.PodAnnotationQoSLevelSharedCores, 193 }, 194 }, 195 Spec: v1.PodSpec{ 196 Priority: &highPriority, 197 Containers: []v1.Container{ 198 { 199 Name: "c", 200 }, 201 }, 202 }, 203 }, 204 } 205 206 podUsageSystem := []float64{ 207 5 * 1024 * 1024 * 1024, 208 10 * 1024 * 1024 * 1024, 209 20 * 1024 * 1024 * 1024, 210 30 * 1024 * 1024 * 1024, 211 40 * 1024 * 1024 * 1024, 212 50 * 1024 * 1024 * 1024, 213 60 * 1024 * 1024 * 1024, 214 70 * 1024 * 1024 * 1024, 215 80 * 1024 * 1024 * 1024, 216 } 217 218 now := time.Now() 219 for i, pod := range pods { 220 fakeMetricsFetcher.SetContainerMetric(string(pod.UID), pod.Spec.Containers[0].Name, consts.MetricMemUsageContainer, utilmetric.MetricData{Value: podUsageSystem[i], Time: &now}) 221 } 222 general.NewMultiSorter(helper.getEvictionCmpFuncs(conf.GetDynamicConfiguration().SystemEvictionRankingMetrics, 223 nonExistNumaID)...).Sort(native.NewPodSourceImpList(pods)) 224 225 wantPodNameList := []string{ 226 "pod-2", 227 "pod-1", 228 "pod-4", 229 "pod-3", 230 "pod-6", 231 "pod-5", 232 "pod-8", 233 "pod-7", 234 } 235 for i := range pods { 236 assert.Equal(t, wantPodNameList[i], pods[i].Name) 237 } 238 }