Unverified Commit 5807d651 authored by Volcano Bot's avatar Volcano Bot Committed by GitHub
Browse files

Merge pull request #2433 from kerthcet/automated-cherry-pick-of-#2423-upstream-release-1.4

[cherry-pick]Consider initContaienr GPUs in considering quota
parents 05b31401 e2b1edc8
Showing with 116 additions and 5 deletions
+116 -5
......@@ -55,17 +55,29 @@ func (g *GPUDevice) getUsedGPUMemory() uint {
// GetGPUResourceOfPod returns the GPU resource required by the pod.
func GetGPUResourceOfPod(pod *v1.Pod) uint {
var initMem uint
for _, container := range pod.Spec.InitContainers {
res := getGPUResourceOfContainer(container.Resources)
if initMem < res {
initMem = res
}
}
var mem uint
for _, container := range pod.Spec.Containers {
mem += getGPUResourceOfContainer(&container)
mem += getGPUResourceOfContainer(container.Resources)
}
return mem
if mem > initMem {
return mem
}
return initMem
}
// getGPUResourceOfPod returns the GPU resource required by the container.
func getGPUResourceOfContainer(container *v1.Container) uint {
// getGPUResourceOfContainer returns the GPU resource required by the container.
func getGPUResourceOfContainer(resources v1.ResourceRequirements) uint {
var mem uint
if val, ok := container.Resources.Limits[VolcanoGPUResource]; ok {
if val, ok := resources.Limits[VolcanoGPUResource]; ok {
mem = uint(val.Value())
}
return mem
......
/*
Copyright 2022 The Volcano Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package api
import (
"testing"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)
func TestGetGPUResourceOfPod(t *testing.T) {
testCases := []struct {
name string
pod *v1.Pod
want uint
}{
{
name: "GPUs required only in Containers",
pod: &v1.Pod{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Resources: v1.ResourceRequirements{
Limits: v1.ResourceList{
VolcanoGPUResource: resource.MustParse("1"),
},
},
},
{
Resources: v1.ResourceRequirements{
Limits: v1.ResourceList{
VolcanoGPUResource: resource.MustParse("3"),
},
},
},
},
},
},
want: 4,
},
{
name: "GPUs required both in initContainers and Containers",
pod: &v1.Pod{
Spec: v1.PodSpec{
InitContainers: []v1.Container{
{
Resources: v1.ResourceRequirements{
Limits: v1.ResourceList{
VolcanoGPUResource: resource.MustParse("1"),
},
},
},
{
Resources: v1.ResourceRequirements{
Limits: v1.ResourceList{
VolcanoGPUResource: resource.MustParse("3"),
},
},
},
},
Containers: []v1.Container{
{
Resources: v1.ResourceRequirements{
Limits: v1.ResourceList{
VolcanoGPUResource: resource.MustParse("2"),
},
},
},
},
},
},
want: 3,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := GetGPUResourceOfPod(tc.pod)
if tc.want != got {
t.Errorf("unexpected result, want: %v, got: %v", tc.want, got)
}
})
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment