Skip to content

Commit

Permalink
Merge pull request #2 from makocchi-git/ignore_non_running_resource
Browse files Browse the repository at this point in the history
Ignore non running resource
  • Loading branch information
makocchi-git authored Jul 11, 2019
2 parents d050d76 9b326f4 commit 0303ec2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 20,7 @@ node3 Ready 455m 2 22% 52428K 3503149K 1%
And list containers of pod on Kubernetes node(s).

```shell
$ kubectl free --list node1
$ kubectl free --list node1 --namespace kube-system
NODE NAME POD POD IP POD STATUS NAMESPACE CONTAINER CPU/req CPU/lim MEM/req MEM/lim
node1 calico-node-sml6z 10.8.2.87 Running kube-system calico-node 250m - - -
node1 coredns-5695fb77c8-knc6d 10.112.1.3 Running kube-system coredns 100m - 73400K 178257K
Expand Down
7 changes: 5 additions & 2 deletions pkg/cmd/free_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 49,9 @@ var testPods = []v1.Pod{
Name: "pod1",
Namespace: "default",
},
Status: v1.PodStatus{
Phase: v1.PodRunning,
},
Spec: v1.PodSpec{
NodeName: "node1",
Containers: []v1.Container{
Expand Down Expand Up @@ -391,7 394,7 @@ func TestRun(t *testing.T) {
[]string{},
true,
[]string{
"node1 pod1 Unknown default container1 1 2 1K 2K",
"node1 pod1 Running default container1 1 2 1K 2K",
"",
},
nil,
Expand All @@ -410,7 413,7 @@ func TestRun(t *testing.T) {
table: table.NewOutputTable(buffer),
list: test.list,
nodeClient: fakeNodeClient.CoreV1().Nodes(),
podClient: fakePodClient.CoreV1().Pods(""),
podClient: fakePodClient.CoreV1().Pods("default"),
header: "none",
}

Expand Down
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 0303ec2

Please sign in to comment.