volcano.sh/volcano@v1.9.0/pkg/scheduler/api/helpers/helpers.go (about) 1 /* 2 Copyright 2018 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 helpers 18 19 import ( 20 "math" 21 22 v1 "k8s.io/api/core/v1" 23 24 "volcano.sh/volcano/pkg/scheduler/api" 25 ) 26 27 // Min is used to find the min of two resource types 28 func Min(l, r *api.Resource) *api.Resource { 29 res := &api.Resource{} 30 31 res.MilliCPU = math.Min(l.MilliCPU, r.MilliCPU) 32 res.Memory = math.Min(l.Memory, r.Memory) 33 34 if l.ScalarResources == nil || r.ScalarResources == nil { 35 return res 36 } 37 38 res.ScalarResources = map[v1.ResourceName]float64{} 39 for lName, lQuant := range l.ScalarResources { 40 res.ScalarResources[lName] = math.Min(lQuant, r.ScalarResources[lName]) 41 } 42 43 return res 44 } 45 46 // Max returns the resource object with larger value in each dimension. 47 func Max(l, r *api.Resource) *api.Resource { 48 res := &api.Resource{} 49 50 res.MilliCPU = math.Max(l.MilliCPU, r.MilliCPU) 51 res.Memory = math.Max(l.Memory, r.Memory) 52 53 if l.ScalarResources == nil && r.ScalarResources == nil { 54 return res 55 } 56 res.ScalarResources = map[v1.ResourceName]float64{} 57 if l.ScalarResources != nil { 58 for lName, lQuant := range l.ScalarResources { 59 if lQuant > 0 { 60 res.ScalarResources[lName] = lQuant 61 } 62 } 63 } 64 if r.ScalarResources != nil { 65 for rName, rQuant := range r.ScalarResources { 66 if rQuant > 0 { 67 maxQuant := math.Max(rQuant, res.ScalarResources[rName]) 68 res.ScalarResources[rName] = maxQuant 69 } 70 } 71 } 72 return res 73 } 74 75 // Share is used to determine the share 76 func Share(l, r float64) float64 { 77 var share float64 78 if r == 0 { 79 if l == 0 { 80 share = 0 81 } else { 82 share = 1 83 } 84 } else { 85 share = l / r 86 } 87 88 return share 89 }