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 // leastResourceScorer favors nodes with fewer requested resources. 25 // It calculates the percentage of memory, CPU and other resources requested by pods scheduled on the node, and 26 // prioritizes based on the minimum of the average of the fraction of requested to capacity. 27 // 28 // Details: 29 // (cpu((capacity-requested)*MaxNodeScore*cpuWeight/capacity) + memory((capacity-requested)*MaxNodeScore*memoryWeight/capacity) + ...)/weightSum 30 func leastResourceScorer(resources []config.ResourceSpec) func([]int64, []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 := leastRequestedScore(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 unused capacity is calculated on a scale of 0-MaxNodeScore 50 // 0 being the lowest priority and `MaxNodeScore` being the highest. 51 // The more unused resources the higher the score is. 52 func leastRequestedScore(requested, capacity int64) int64 { 53 if capacity == 0 { 54 return 0 55 } 56 if requested > capacity { 57 return 0 58 } 59 60 return ((capacity - requested) * framework.MaxNodeScore) / capacity 61 } 62