github.com/kubewharf/katalyst-core@v0.5.3/pkg/scheduler/plugins/qosawarenoderesources/least_allocated.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 qosawarenoderesources
    18  
    19  import (
    20  	"k8s.io/kubernetes/pkg/scheduler/framework"
    21  )
    22  
    23  // leastResourceScorer favors nodes with fewer requested resources.
    24  // It calculates the percentage of memory, CPU and other resources requested by pods scheduled on the node, and
    25  // prioritizes based on the minimum of the average of the fraction of requested to capacity.
    26  //
    27  // Details:
    28  // (cpu((capacity-requested)*MaxNodeScore*cpuWeight/capacity) + memory((capacity-requested)*MaxNodeScore*memoryWeight/capacity) + ...)/weightSum
    29  func leastResourceScorer(resToWeightMap resourceToWeightMap) func(resourceToValueMap, resourceToValueMap) int64 {
    30  	return func(requested, allocatable resourceToValueMap) int64 {
    31  		var nodeScore, weightSum int64
    32  		for resource := range requested {
    33  			weight := resToWeightMap[resource]
    34  			resourceScore := leastRequestedScore(requested[resource], allocatable[resource])
    35  			nodeScore += resourceScore * weight
    36  			weightSum += weight
    37  		}
    38  		if weightSum == 0 {
    39  			return 0
    40  		}
    41  		return nodeScore / weightSum
    42  	}
    43  }
    44  
    45  // The unused capacity is calculated on a scale of 0-MaxNodeScore
    46  // 0 being the lowest priority and `MaxNodeScore` being the highest.
    47  // The more unused resources the higher the score is.
    48  func leastRequestedScore(requested, capacity int64) int64 {
    49  	if capacity == 0 {
    50  		return 0
    51  	}
    52  	if requested > capacity {
    53  		return 0
    54  	}
    55  
    56  	return ((capacity - requested) * framework.MaxNodeScore) / capacity
    57  }