k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/scheduler/framework/plugins/noderesources/requested_to_capacity_ratio.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 noderesources 18 19 import ( 20 "math" 21 22 "k8s.io/kubernetes/pkg/scheduler/apis/config" 23 "k8s.io/kubernetes/pkg/scheduler/framework" 24 "k8s.io/kubernetes/pkg/scheduler/framework/plugins/helper" 25 ) 26 27 const maxUtilization = 100 28 29 // buildRequestedToCapacityRatioScorerFunction allows users to apply bin packing 30 // on core resources like CPU, Memory as well as extended resources like accelerators. 31 func buildRequestedToCapacityRatioScorerFunction(scoringFunctionShape helper.FunctionShape, resources []config.ResourceSpec) func([]int64, []int64) int64 { 32 rawScoringFunction := helper.BuildBrokenLinearFunction(scoringFunctionShape) 33 resourceScoringFunction := func(requested, capacity int64) int64 { 34 if capacity == 0 || requested > capacity { 35 return rawScoringFunction(maxUtilization) 36 } 37 38 return rawScoringFunction(requested * maxUtilization / capacity) 39 } 40 return func(requested, allocable []int64) int64 { 41 var nodeScore, weightSum int64 42 for i := range requested { 43 if allocable[i] == 0 { 44 continue 45 } 46 weight := resources[i].Weight 47 resourceScore := resourceScoringFunction(requested[i], allocable[i]) 48 if resourceScore > 0 { 49 nodeScore += resourceScore * weight 50 weightSum += weight 51 } 52 } 53 if weightSum == 0 { 54 return 0 55 } 56 return int64(math.Round(float64(nodeScore) / float64(weightSum))) 57 } 58 } 59 60 func requestedToCapacityRatioScorer(resources []config.ResourceSpec, shape []config.UtilizationShapePoint) func([]int64, []int64) int64 { 61 shapes := make([]helper.FunctionShapePoint, 0, len(shape)) 62 for _, point := range shape { 63 shapes = append(shapes, helper.FunctionShapePoint{ 64 Utilization: int64(point.Utilization), 65 // MaxCustomPriorityScore may diverge from the max score used in the scheduler and defined by MaxNodeScore, 66 // therefore we need to scale the score returned by requested to capacity ratio to the score range 67 // used by the scheduler. 68 Score: int64(point.Score) * (framework.MaxNodeScore / config.MaxCustomPriorityScore), 69 }) 70 } 71 72 return buildRequestedToCapacityRatioScorerFunction(shapes, resources) 73 }