github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/native/pod_sorter.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 native
    18  
    19  import (
    20  	v1 "k8s.io/api/core/v1"
    21  
    22  	corev1helpers "k8s.io/component-helpers/scheduling/corev1"
    23  
    24  	"github.com/kubewharf/katalyst-core/pkg/util/general"
    25  )
    26  
    27  type PodSourceList struct {
    28  	pods []*v1.Pod
    29  }
    30  
    31  var _ general.SourceList = &PodSourceList{}
    32  
    33  func NewPodSourceImpList(pods []*v1.Pod) general.SourceList {
    34  	return &PodSourceList{
    35  		pods: pods,
    36  	}
    37  }
    38  
    39  func (pl *PodSourceList) Len() int {
    40  	return len(pl.pods)
    41  }
    42  
    43  func (pl *PodSourceList) GetSource(index int) interface{} {
    44  	return pl.pods[index]
    45  }
    46  
    47  func (pl *PodSourceList) SetSource(index int, p interface{}) {
    48  	pl.pods[index] = p.(*v1.Pod)
    49  }
    50  
    51  // PodPriorityCmpFunc sorts priority of pods with greater comparison
    52  func PodPriorityCmpFunc(i1, i2 interface{}) int {
    53  	priority1 := corev1helpers.PodPriority(i1.(*v1.Pod))
    54  	priority2 := corev1helpers.PodPriority(i2.(*v1.Pod))
    55  
    56  	return general.CmpInt32(priority1, priority2)
    57  }
    58  
    59  // PodCPURequestCmpFunc sorts cpu request of pods with less comparison
    60  func PodCPURequestCmpFunc(i1, i2 interface{}) int {
    61  	p1Request := SumUpPodRequestResources(i1.(*v1.Pod))
    62  	p2Request := SumUpPodRequestResources(i2.(*v1.Pod))
    63  
    64  	p1CPUQuantity := CPUQuantityGetter()(p1Request)
    65  	p2CPUQuantity := CPUQuantityGetter()(p2Request)
    66  
    67  	return p1CPUQuantity.Cmp(p2CPUQuantity)
    68  }
    69  
    70  // PodUniqKeyCmpFunc sorts uniq key of pod with greater comparison
    71  func PodUniqKeyCmpFunc(i1, i2 interface{}) int {
    72  	p1UniqKey := GenerateUniqObjectNameKey(i1.(*v1.Pod))
    73  	p2UniqKey := GenerateUniqObjectNameKey(i2.(*v1.Pod))
    74  
    75  	return general.CmpString(p1UniqKey, p2UniqKey)
    76  }
    77  
    78  var (
    79  	_ general.CmpFunc = PodPriorityCmpFunc
    80  	_ general.CmpFunc = PodCPURequestCmpFunc
    81  	_ general.CmpFunc = PodUniqKeyCmpFunc
    82  )