sigs.k8s.io/kueue@v0.6.2/pkg/util/resource/resource.go (about) 1 /* 2 Copyright 2023 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 resource 18 19 import ( 20 corev1 "k8s.io/api/core/v1" 21 "k8s.io/apimachinery/pkg/api/resource" 22 ) 23 24 type resolveConflict func(a, b resource.Quantity) resource.Quantity 25 26 func mergeResourceList(a, b corev1.ResourceList, f resolveConflict) corev1.ResourceList { 27 if a == nil { 28 return b.DeepCopy() 29 } 30 ret := a.DeepCopy() 31 32 for k, vb := range b { 33 if va, exists := ret[k]; !exists { 34 ret[k] = vb.DeepCopy() 35 } else { 36 if f != nil { 37 ret[k] = f(va, vb) 38 } 39 } 40 } 41 return ret 42 } 43 44 // MergeResourceListKeepFirst creates a new ResourceList holding all resource values from dst 45 // and any new values from src 46 func MergeResourceListKeepFirst(dst, src corev1.ResourceList) corev1.ResourceList { 47 return mergeResourceList(dst, src, nil) 48 } 49 50 // MergeResourceListKeepMax creates a new ResourceList holding all the values from a and b 51 // and resolve potential conflicts by keeping the highest value. 52 func MergeResourceListKeepMax(a, b corev1.ResourceList) corev1.ResourceList { 53 return mergeResourceList(a, b, func(a, b resource.Quantity) resource.Quantity { 54 if a.Cmp(b) < 0 { 55 return b 56 } 57 return a 58 }) 59 } 60 61 // MergeResourceListKeepMin creates a new ResourceList holding all the values from a and b 62 // and resolve potential conflicts by keeping the lowest value. 63 func MergeResourceListKeepMin(a, b corev1.ResourceList) corev1.ResourceList { 64 return mergeResourceList(a, b, func(a, b resource.Quantity) resource.Quantity { 65 if a.Cmp(b) > 0 { 66 return b 67 } 68 return a 69 }) 70 } 71 72 // MergeResourceListKeepSum creates a new ResourceList holding all the values from a and b 73 // and resolve potential conflicts by adding up the two values. 74 func MergeResourceListKeepSum(a, b corev1.ResourceList) corev1.ResourceList { 75 return mergeResourceList(a, b, func(a, b resource.Quantity) resource.Quantity { 76 a.Add(b) 77 return a 78 }) 79 } 80 81 // GetGreaterKeys returns the list of ResourceNames for which the value in a are greater than the value in b. 82 func GetGreaterKeys(a, b corev1.ResourceList) []string { 83 if len(a) == 0 || len(b) == 0 { 84 return nil 85 } 86 87 ret := make([]string, 0, len(a)) 88 for k, va := range a { 89 if vb, found := b[k]; found && va.Cmp(vb) > 0 { 90 ret = append(ret, k.String()) 91 } 92 } 93 if len(ret) == 0 { 94 return nil 95 } 96 return ret 97 } 98 99 func QuantityToFloat(q *resource.Quantity) float64 { 100 if q == nil || q.IsZero() { 101 return 0 102 } 103 if i64, isInt := q.AsInt64(); isInt { 104 return float64(i64) 105 } 106 return float64(q.MilliValue()) / 1000 107 }