github.com/kubewharf/katalyst-core@v0.5.3/pkg/scheduler/plugins/qosawarenoderesources/most_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  // mostResourceScorer favors nodes with most requested resources.
    24  // It calculates the percentage of memory and CPU requested by pods scheduled on the node, and prioritizes
    25  // based on the maximum of the average of the fraction of requested to capacity.
    26  //
    27  // Details:
    28  // (cpu(MaxNodeScore * sum(requested) / capacity) + memory(MaxNodeScore * sum(requested) / capacity)) / weightSum
    29  func mostResourceScorer(resToWeightMap resourceToWeightMap) func(requested, allocable 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 := mostRequestedScore(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 used capacity is calculated on a scale of 0-MaxNodeScore (MaxNodeScore is
    46  // constant with value set to 100).
    47  // 0 being the lowest priority and 100 being the highest.
    48  // The more resources are used the higher the score is. This function
    49  // is almost a reversed version of noderesources.leastRequestedScore.
    50  func mostRequestedScore(requested, capacity int64) int64 {
    51  	if capacity == 0 {
    52  		return 0
    53  	}
    54  	if requested > capacity {
    55  		// `requested` might be greater than `capacity` because pods with no
    56  		// requests get minimum values.
    57  		requested = capacity
    58  	}
    59  
    60  	return (requested * framework.MaxNodeScore) / capacity
    61  }