github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/evictionmanager/rule/rule_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 rule
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  	kubelettypes "k8s.io/kubernetes/pkg/kubelet/types"
    24  
    25  	apiconsts "github.com/kubewharf/katalyst-api/pkg/consts"
    26  	"github.com/kubewharf/katalyst-core/cmd/katalyst-agent/app/options"
    27  )
    28  
    29  func makeRuledEvictPodWithAnnotation(name, scope string, annotations map[string]string) *RuledEvictPod {
    30  	ep := makeRuledEvictPod(name, scope)
    31  	ep.Pod.Annotations = annotations
    32  	return ep
    33  }
    34  
    35  func makeRuledEvictPodForSort(name, scope string, annotations map[string]string, priority int32) *RuledEvictPod {
    36  	ep := makeRuledEvictPodWithAnnotation(name, scope, annotations)
    37  	ep.Pod.Spec.Priority = &priority
    38  	return ep
    39  }
    40  
    41  func TestEvictionStrategyImp(t *testing.T) {
    42  	t.Parallel()
    43  
    44  	testConf, _ := options.NewOptions().Config()
    45  	s := NewEvictionStrategyImpl(testConf)
    46  
    47  	for _, tc := range []struct {
    48  		comment  string
    49  		ep       *RuledEvictPod
    50  		expected bool
    51  	}{
    52  		{
    53  			comment: "system qos should be skipped",
    54  			ep: makeRuledEvictPodWithAnnotation("system-p", "", map[string]string{
    55  				apiconsts.PodAnnotationQoSLevelKey: apiconsts.PodAnnotationQoSLevelSystemCores,
    56  			}),
    57  			expected: false,
    58  		},
    59  		{
    60  			comment: "critical pod should be skipped",
    61  			ep: makeRuledEvictPodWithAnnotation("system-p", "", map[string]string{
    62  				kubelettypes.ConfigSourceAnnotationKey: "test",
    63  			}),
    64  			expected: false,
    65  		},
    66  		{
    67  			comment:  "normal pod should  not be skipped",
    68  			ep:       makeRuledEvictPodWithAnnotation("system-p", "", map[string]string{}),
    69  			expected: true,
    70  		},
    71  	} {
    72  		assert.Equal(t, tc.expected, s.CandidateValidate(tc.ep))
    73  	}
    74  
    75  	rpList := RuledEvictPodList{
    76  		makeRuledEvictPodForSort("p-shared-priority-60-emp", "emp", map[string]string{
    77  			apiconsts.PodAnnotationQoSLevelKey: apiconsts.PodAnnotationQoSLevelSharedCores,
    78  		}, 60),
    79  		makeRuledEvictPodForSort("p-dedicated-priority-60-memory", EvictionScopeMemory, map[string]string{
    80  			apiconsts.PodAnnotationQoSLevelKey: apiconsts.PodAnnotationQoSLevelDedicatedCores,
    81  		}, 60),
    82  		makeRuledEvictPodForSort("p-shared-priority-80-force", EvictionScopeForce, map[string]string{
    83  			apiconsts.PodAnnotationQoSLevelKey: apiconsts.PodAnnotationQoSLevelSharedCores,
    84  		}, 80),
    85  		makeRuledEvictPodForSort("p-dedicated-priority-60-emp", "emp", map[string]string{
    86  			apiconsts.PodAnnotationQoSLevelKey: apiconsts.PodAnnotationQoSLevelDedicatedCores,
    87  		}, 60),
    88  		makeRuledEvictPodForSort("p-reclaimed-priority-100-force", EvictionScopeForce, map[string]string{
    89  			apiconsts.PodAnnotationQoSLevelKey: apiconsts.PodAnnotationQoSLevelReclaimedCores,
    90  		}, 100),
    91  		makeRuledEvictPodForSort("p-reclaimed-priority-20-force", EvictionScopeForce, map[string]string{
    92  			apiconsts.PodAnnotationQoSLevelKey: apiconsts.PodAnnotationQoSLevelReclaimedCores,
    93  		}, 20),
    94  		makeRuledEvictPodForSort("p-shared-priority-70-memory", EvictionScopeMemory, map[string]string{
    95  			apiconsts.PodAnnotationQoSLevelKey: apiconsts.PodAnnotationQoSLevelSharedCores,
    96  		}, 70),
    97  		makeRuledEvictPodForSort("p-shared-priority-100-force", EvictionScopeForce, map[string]string{
    98  			apiconsts.PodAnnotationQoSLevelKey: apiconsts.PodAnnotationQoSLevelSharedCores,
    99  		}, 100),
   100  		makeRuledEvictPodForSort("p-dedicated-priority-80-memory", EvictionScopeMemory, map[string]string{
   101  			apiconsts.PodAnnotationQoSLevelKey: apiconsts.PodAnnotationQoSLevelDedicatedCores,
   102  		}, 80),
   103  		makeRuledEvictPodForSort("p-shared-priority-60-cpu", EvictionScopeCPU, map[string]string{
   104  			apiconsts.PodAnnotationQoSLevelKey: apiconsts.PodAnnotationQoSLevelSharedCores,
   105  		}, 60),
   106  		makeRuledEvictPodForSort("p-reclaimed-priority-100-emp", "emp", map[string]string{
   107  			apiconsts.PodAnnotationQoSLevelKey: apiconsts.PodAnnotationQoSLevelReclaimedCores,
   108  		}, 100),
   109  		makeRuledEvictPodForSort("p-shared-priority-80-cpu", EvictionScopeCPU, map[string]string{
   110  			apiconsts.PodAnnotationQoSLevelKey: apiconsts.PodAnnotationQoSLevelSharedCores,
   111  		}, 80),
   112  	}
   113  	s.CandidateSort(rpList)
   114  
   115  	assert.Equal(t, []string{
   116  		"p-shared-priority-100-force",
   117  		"p-shared-priority-80-force",
   118  		"p-dedicated-priority-80-memory",
   119  		"p-shared-priority-80-cpu",
   120  		"p-shared-priority-70-memory",
   121  		"p-dedicated-priority-60-memory",
   122  		"p-shared-priority-60-cpu",
   123  		"p-shared-priority-60-emp",
   124  		"p-dedicated-priority-60-emp",
   125  		"p-reclaimed-priority-100-force",
   126  		"p-reclaimed-priority-100-emp",
   127  		"p-reclaimed-priority-20-force",
   128  	}, rpList.getPodNames())
   129  }