Skip to content

Commit

Permalink
Fix error
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuqi-lucas committed Dec 15, 2023
1 parent a118ba6 commit 5027eb8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
20 changes: 15 additions & 5 deletions pkg/common/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ func GetPodResource(pod *v1.Pod) (resource *si.Resource) {
Resources: map[string]*si.Quantity{"pods": {Value: 1}},
}

// A QosBestEffort pod does not request any resources, just a single pod
if qos.GetPodQOS(pod) == v1.PodQOSBestEffort {
return podResource
}

for _, c := range pod.Spec.Containers {
resourceList := c.Resources.Requests
containerResource := getResource(resourceList)
Expand All @@ -74,6 +69,21 @@ func GetPodResource(pod *v1.Pod) (resource *si.Resource) {
checkInitContainerRequest(pod, podResource)
}

// iterate the pod resources when resource is zero, remove it from the pod resource
for k, v := range podResource.Resources {
if v.Value == 0 {
delete(podResource.Resources, k)
}
}

// A QosBestEffort pod does not request any cpu/mem resources, just a single pod
// But with other resources requested, it is not the best effort pod
if qos.GetPodQOS(pod) == v1.PodQOSBestEffort && (len(podResource.Resources) == 1) {
return &si.Resource{
Resources: map[string]*si.Quantity{"pods": {Value: 1}},
}
}

// K8s pod EnableOverHead from:
// alpha: v1.16
// beta: v1.18
Expand Down
38 changes: 38 additions & 0 deletions pkg/common/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,44 @@ func TestBestEffortPod(t *testing.T) {
assert.Equal(t, res.Resources["pods"].GetValue(), int64(1))
}

func TestGPUOnlyResources(t *testing.T) {
containers := make([]v1.Container, 0)

// container 01
c1Resources := make(map[v1.ResourceName]resource.Quantity)
c1Resources[v1.ResourceName("nvidia.com/gpu")] = resource.MustParse("1")
containers = append(containers, v1.Container{
Name: "container-01",
Resources: v1.ResourceRequirements{
Requests: c1Resources,
},
})

pod := &v1.Pod{
TypeMeta: apis.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: apis.ObjectMeta{
Name: "pod-resource-test-00001",
UID: "UID-00001",
},
Spec: v1.PodSpec{
Containers: containers,
},
}

res := GetPodResource(pod)
assert.Equal(t, len(res.Resources), 2)
assert.Equal(t, res.Resources["pods"].GetValue(), int64(1))
assert.Equal(t, res.Resources["nvidia.com/gpu"].GetValue(), int64(1))

c1Resources[v1.ResourceName("nvidia.com/gpu")] = resource.MustParse("0")
res = GetPodResource(pod)
assert.Equal(t, len(res.Resources), 1)
assert.Equal(t, res.Resources["pods"].GetValue(), int64(1))
}

func TestNodeResource(t *testing.T) {
nodeCapacity := make(map[v1.ResourceName]resource.Quantity)
nodeCapacity[v1.ResourceCPU] = resource.MustParse("14500m")
Expand Down

0 comments on commit 5027eb8

Please sign in to comment.