Skip to content

Commit

Permalink
Ignore resource calculating when pod status is not running
Browse files Browse the repository at this point in the history
  • Loading branch information
makocchi-git committed Jul 11, 2019
1 parent d529fea commit 3c2e5d3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
6 changes: 6 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ func GetPodResources(pods v1.PodList) (int64, int64, int64, int64) {
var rc, rm, lc, lm int64

for _, pod := range pods.Items {

// skip if pod status is not running
if pod.Status.Phase != v1.PodRunning {
continue
}

for _, container := range pod.Spec.Containers {
rc += container.Resources.Requests.Cpu().MilliValue()
lc += container.Resources.Limits.Cpu().MilliValue()
Expand Down
41 changes: 38 additions & 3 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ var testPods = []v1.Pod{
},
},
},
Status: v1.PodStatus{
Phase: v1.PodRunning,
},
},
{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -117,6 +120,36 @@ var testPods = []v1.Pod{
},
},
},
Status: v1.PodStatus{
Phase: v1.PodRunning,
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "pod3",
Namespace: "default",
},
Spec: v1.PodSpec{
NodeName: "node3",
Containers: []v1.Container{
{
Name: "container3",
Resources: v1.ResourceRequirements{
Limits: v1.ResourceList{
v1.ResourceCPU: *resource.NewMilliQuantity(300, resource.DecimalSI),
v1.ResourceMemory: *resource.NewQuantity(300, resource.DecimalSI),
},
Requests: v1.ResourceList{
v1.ResourceCPU: *resource.NewMilliQuantity(300, resource.DecimalSI),
v1.ResourceMemory: *resource.NewQuantity(300, resource.DecimalSI),
},
},
},
},
},
Status: v1.PodStatus{
Phase: v1.PodFailed,
},
},
}

Expand Down Expand Up @@ -474,13 +507,15 @@ func TestGetPods(t *testing.T) {
func TestGetPodResources(t *testing.T) {

// cpu/ mem cpu/ mem
// pod1 container1 [requests 1000/1000] [limits 2000/2000]
// pod2 container1 [requests 500/1000] [limits 500/1000]
// pod2 container2 [requests 50/ 100] [limits 50/ 100]
// pod1 container1 : Running [requests 1000/1000] [limits 2000/2000]
// pod2 container2a : Running [requests 500/1000] [limits 500/1000]
// pod2 container2b : Running [requests 50/ 100] [limits 50/ 100]
// pod3 container3 : Failed [requests 300/ 300] [limits 300/ 300]
pods := v1.PodList{
Items: []v1.Pod{
testPods[0],
testPods[1],
testPods[2],
},
}

Expand Down

0 comments on commit 3c2e5d3

Please sign in to comment.