Skip to content

Commit 4a1f710

Browse files
authored
Merge pull request kubernetes#78627 from tedyu/resource-req
Check the correct value of Quantity for non-v1.ResourceCPU resource in GetResourceRequest
2 parents cf7662d + 2dca643 commit 4a1f710

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

pkg/api/v1/resource/helpers.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,10 @@ func GetResourceRequest(pod *v1.Pod, resource v1.ResourceName) int64 {
8686
// take max_resource(sum_pod, any_init_container)
8787
for _, container := range pod.Spec.InitContainers {
8888
if rQuantity, ok := container.Resources.Requests[resource]; ok {
89-
if resource == v1.ResourceCPU && rQuantity.MilliValue() > totalResources {
90-
totalResources = rQuantity.MilliValue()
89+
if resource == v1.ResourceCPU {
90+
if rQuantity.MilliValue() > totalResources {
91+
totalResources = rQuantity.MilliValue()
92+
}
9193
} else if rQuantity.Value() > totalResources {
9294
totalResources = rQuantity.Value()
9395
}

pkg/api/v1/resource/helpers_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,31 @@ func TestDefaultResourceHelpers(t *testing.T) {
6363
}
6464
}
6565

66+
func TestGetResourceRequest(t *testing.T) {
67+
cases := []struct {
68+
pod *v1.Pod
69+
res v1.ResourceName
70+
expectedValue int64
71+
expectedError error
72+
}{
73+
{
74+
pod: getPod("foo", "9", "", "", ""),
75+
res: v1.ResourceCPU,
76+
expectedValue: 9000,
77+
},
78+
{
79+
pod: getPod("foo", "", "", "90Mi", ""),
80+
res: v1.ResourceMemory,
81+
expectedValue: 94371840,
82+
},
83+
}
84+
as := assert.New(t)
85+
for idx, tc := range cases {
86+
actual := GetResourceRequest(tc.pod, tc.res)
87+
as.Equal(actual, tc.expectedValue, "expected test case [%d] to return %q; got %q instead", idx, tc.expectedValue, actual)
88+
}
89+
}
90+
6691
func TestExtractResourceValue(t *testing.T) {
6792
cases := []struct {
6893
fs *v1.ResourceFieldSelector

0 commit comments

Comments
 (0)