@@ -11,24 +11,10 @@ import (
1111 v1 "k8s.io/api/core/v1"
1212 "k8s.io/apimachinery/pkg/api/resource"
1313 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14+ resourcehelper "k8s.io/component-helpers/resource"
1415 "k8s.io/klog/v2"
1516)
1617
17- // GetResourceRequest finds and returns the request value for a specific resource.
18- func GetResourceRequest (pod * v1.Pod , resource v1.ResourceName ) int64 {
19- if resource == v1 .ResourcePods {
20- return 1
21- }
22-
23- requestQuantity := GetResourceRequestQuantity (pod , resource )
24-
25- if resource == v1 .ResourceCPU {
26- return requestQuantity .MilliValue ()
27- }
28-
29- return requestQuantity .Value ()
30- }
31-
3218// GetResourceRequestQuantity finds and returns the request quantity for a specific resource.
3319func GetResourceRequestQuantity (pod * v1.Pod , resourceName v1.ResourceName ) resource.Quantity {
3420 requestQuantity := resource.Quantity {}
@@ -42,26 +28,8 @@ func GetResourceRequestQuantity(pod *v1.Pod, resourceName v1.ResourceName) resou
4228 requestQuantity = resource.Quantity {Format : resource .DecimalSI }
4329 }
4430
45- for _ , container := range pod .Spec .Containers {
46- if rQuantity , ok := container .Resources .Requests [resourceName ]; ok {
47- requestQuantity .Add (rQuantity )
48- }
49- }
50-
51- for _ , container := range pod .Spec .InitContainers {
52- if rQuantity , ok := container .Resources .Requests [resourceName ]; ok {
53- if requestQuantity .Cmp (rQuantity ) < 0 {
54- requestQuantity = rQuantity .DeepCopy ()
55- }
56- }
57- }
58-
59- // We assume pod overhead feature gate is enabled.
60- // We can't import the scheduler settings so we will inherit the default.
61- if pod .Spec .Overhead != nil {
62- if podOverhead , ok := pod .Spec .Overhead [resourceName ]; ok && ! requestQuantity .IsZero () {
63- requestQuantity .Add (podOverhead )
64- }
31+ if rQuantity , ok := resourcehelper .PodRequests (pod , resourcehelper.PodResourcesOptions {})[resourceName ]; ok {
32+ requestQuantity .Add (rQuantity )
6533 }
6634
6735 return requestQuantity
@@ -171,59 +139,9 @@ func GetPodSource(pod *v1.Pod) (string, error) {
171139// containers of the pod. If PodOverhead feature is enabled, pod overhead is added to the
172140// total container resource requests and to the total container limits which have a
173141// non-zero quantity.
174- func PodRequestsAndLimits (pod * v1.Pod ) (reqs , limits v1.ResourceList ) {
175- reqs , limits = v1.ResourceList {}, v1.ResourceList {}
176- for _ , container := range pod .Spec .Containers {
177- addResourceList (reqs , container .Resources .Requests )
178- addResourceList (limits , container .Resources .Limits )
179- }
180- // init containers define the minimum of any resource
181- for _ , container := range pod .Spec .InitContainers {
182- maxResourceList (reqs , container .Resources .Requests )
183- maxResourceList (limits , container .Resources .Limits )
184- }
185-
186- // We assume pod overhead feature gate is enabled.
187- // We can't import the scheduler settings so we will inherit the default.
188- if pod .Spec .Overhead != nil {
189- addResourceList (reqs , pod .Spec .Overhead )
190-
191- for name , quantity := range pod .Spec .Overhead {
192- if value , ok := limits [name ]; ok && ! value .IsZero () {
193- value .Add (quantity )
194- limits [name ] = value
195- }
196- }
197- }
198-
199- return
200- }
201-
202- // addResourceList adds the resources in newList to list
203- func addResourceList (list , newList v1.ResourceList ) {
204- for name , quantity := range newList {
205- if value , ok := list [name ]; ! ok {
206- list [name ] = quantity .DeepCopy ()
207- } else {
208- value .Add (quantity )
209- list [name ] = value
210- }
211- }
212- }
213-
214- // maxResourceList sets list to the greater of list/newList for every resource
215- // either list
216- func maxResourceList (list , new v1.ResourceList ) {
217- for name , quantity := range new {
218- if value , ok := list [name ]; ! ok {
219- list [name ] = quantity .DeepCopy ()
220- continue
221- } else {
222- if quantity .Cmp (value ) > 0 {
223- list [name ] = quantity .DeepCopy ()
224- }
225- }
226- }
142+ func PodRequestsAndLimits (pod * v1.Pod ) (v1.ResourceList , v1.ResourceList ) {
143+ opts := resourcehelper.PodResourcesOptions {}
144+ return resourcehelper .PodRequests (pod , opts ), resourcehelper .PodLimits (pod , opts )
227145}
228146
229147// PodToleratesTaints returns true if a pod tolerates one node's taints
0 commit comments