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 "k8s.io/kubernetes/pkg/scheduler/apis/config" 21 "k8s.io/kubernetes/pkg/scheduler/framework" 22 ) 23 24 // mostResourceScorer favors nodes with most requested resources. 25 // It calculates the percentage of memory and CPU requested by pods scheduled on the node, and prioritizes 26 // based on the maximum of the average of the fraction of requested to capacity. 27 // 28 // Details: 29 // (cpu(MaxNodeScore * requested * cpuWeight / capacity) + memory(MaxNodeScore * requested * memoryWeight / capacity) + ...) / weightSum 30 func mostResourceScorer(resources []config.ResourceSpec) func(requested, allocable []int64) int64 { 31 return func(requested, allocable []int64) int64 { 32 var nodeScore, weightSum int64 33 for i := range requested { 34 if allocable[i] == 0 { 35 continue 36 } 37 weight := resources[i].Weight 38 resourceScore := mostRequestedScore(requested[i], allocable[i]) 39 nodeScore += resourceScore * weight 40 weightSum += weight 41 } 42 if weightSum == 0 { 43 return 0 44 } 45 return nodeScore / weightSum 46 } 47 } 48 49 // The used capacity is calculated on a scale of 0-MaxNodeScore (MaxNodeScore is 50 // constant with value set to 100). 51 // 0 being the lowest priority and 100 being the highest. 52 // The more resources are used the higher the score is. This function 53 // is almost a reversed version of noderesources.leastRequestedScore. 54 func mostRequestedScore(requested, capacity int64) int64 { 55 if capacity == 0 { 56 return 0 57 } 58 if requested > capacity { 59 // `requested` might be greater than `capacity` because pods with no 60 // requests get minimum values. 61 requested = capacity 62 } 63 64 return (requested * framework.MaxNodeScore) / capacity 65 } 66