k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/scheduler/framework/plugins/helper/normalize_score.go (about)

     1  /*
     2  Copyright 2019 The Kubernetes 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 helper
    18  
    19  import (
    20  	"k8s.io/kubernetes/pkg/scheduler/framework"
    21  )
    22  
    23  // DefaultNormalizeScore generates a Normalize Score function that can normalize the
    24  // scores from [0, max(scores)] to [0, maxPriority]. If reverse is set to true, it
    25  // reverses the scores by subtracting it from maxPriority.
    26  // Note: The input scores are always assumed to be non-negative integers.
    27  func DefaultNormalizeScore(maxPriority int64, reverse bool, scores framework.NodeScoreList) *framework.Status {
    28  	var maxCount int64
    29  	for i := range scores {
    30  		if scores[i].Score > maxCount {
    31  			maxCount = scores[i].Score
    32  		}
    33  	}
    34  
    35  	if maxCount == 0 {
    36  		if reverse {
    37  			for i := range scores {
    38  				scores[i].Score = maxPriority
    39  			}
    40  		}
    41  		return nil
    42  	}
    43  
    44  	for i := range scores {
    45  		score := scores[i].Score
    46  
    47  		score = maxPriority * score / maxCount
    48  		if reverse {
    49  			score = maxPriority - score
    50  		}
    51  
    52  		scores[i].Score = score
    53  	}
    54  	return nil
    55  }