github.com/kubewharf/katalyst-core@v0.5.3/pkg/scheduler/plugins/noderesourcetopology/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 noderesourcetopology
    18  
    19  import (
    20  	v1 "k8s.io/api/core/v1"
    21  	"k8s.io/apimachinery/pkg/api/resource"
    22  	"k8s.io/apimachinery/pkg/util/sets"
    23  	"k8s.io/kubernetes/pkg/scheduler/framework"
    24  )
    25  
    26  func leastAllocatedScoreStrategy(requested, allocatable v1.ResourceList, resourceToWeightMap resourceToWeightMap, alignedResource sets.String) int64 {
    27  	var score int64 = 0
    28  	var weightSum int64 = 0
    29  
    30  	for resourceName := range requested {
    31  		// resources not in alignedResource will not be calculated,
    32  		// these resources may not be allocated to the same numas with alignedResource
    33  		if alignedResource != nil && !alignedResource.Has(resourceName.String()) {
    34  			continue
    35  		}
    36  		resourceScore := leastAllocatedScore(requested[resourceName], allocatable[resourceName])
    37  		weight := resourceToWeightMap.weight(resourceName)
    38  		score += resourceScore * weight
    39  		weightSum += weight
    40  	}
    41  
    42  	return score / weightSum
    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 less allocated resources the node has, the higher the score is.
    49  func leastAllocatedScore(requested, capacity resource.Quantity) int64 {
    50  	if capacity.CmpInt64(0) == 0 {
    51  		return 0
    52  	}
    53  	if requested.Cmp(capacity) > 0 {
    54  		return 0
    55  	}
    56  	numaValue := capacity.Value()
    57  	requestedValue := requested.Value()
    58  	return (numaValue - requestedValue) * framework.MaxNodeScore / capacity.Value()
    59  }