From 2dca643ca186e9fbd3ef290295be068299041bf9 Mon Sep 17 00:00:00 2001 From: Ted Yu Date: Mon, 1 Jul 2019 06:45:26 +0800 Subject: [PATCH] Check the correct value of Quantity in GetResourceRequest --- pkg/api/v1/resource/helpers.go | 6 ++++-- pkg/api/v1/resource/helpers_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/pkg/api/v1/resource/helpers.go b/pkg/api/v1/resource/helpers.go index e42a1b343bf..a3cce12f8d4 100644 --- a/pkg/api/v1/resource/helpers.go +++ b/pkg/api/v1/resource/helpers.go @@ -86,8 +86,10 @@ func GetResourceRequest(pod *v1.Pod, resource v1.ResourceName) int64 { // take max_resource(sum_pod, any_init_container) for _, container := range pod.Spec.InitContainers { if rQuantity, ok := container.Resources.Requests[resource]; ok { - if resource == v1.ResourceCPU && rQuantity.MilliValue() > totalResources { - totalResources = rQuantity.MilliValue() + if resource == v1.ResourceCPU { + if rQuantity.MilliValue() > totalResources { + totalResources = rQuantity.MilliValue() + } } else if rQuantity.Value() > totalResources { totalResources = rQuantity.Value() } diff --git a/pkg/api/v1/resource/helpers_test.go b/pkg/api/v1/resource/helpers_test.go index d5462a415dc..30697045bfb 100644 --- a/pkg/api/v1/resource/helpers_test.go +++ b/pkg/api/v1/resource/helpers_test.go @@ -63,6 +63,31 @@ func TestDefaultResourceHelpers(t *testing.T) { } } +func TestGetResourceRequest(t *testing.T) { + cases := []struct { + pod *v1.Pod + res v1.ResourceName + expectedValue int64 + expectedError error + }{ + { + pod: getPod("foo", "9", "", "", ""), + res: v1.ResourceCPU, + expectedValue: 9000, + }, + { + pod: getPod("foo", "", "", "90Mi", ""), + res: v1.ResourceMemory, + expectedValue: 94371840, + }, + } + as := assert.New(t) + for idx, tc := range cases { + actual := GetResourceRequest(tc.pod, tc.res) + as.Equal(actual, tc.expectedValue, "expected test case [%d] to return %q; got %q instead", idx, tc.expectedValue, actual) + } +} + func TestExtractResourceValue(t *testing.T) { cases := []struct { fs *v1.ResourceFieldSelector