-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcustom-pod-list
executable file
·57 lines (49 loc) · 2.2 KB
/
custom-pod-list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python3
import os
from kubernetes import client, config
from kubernetes.client.rest import ApiException
from prettytable import PrettyTable
kubeconfig = os.getenv("K8S_AUTH_KUBECONFIG") or os.getenv("KUBECONFIG")
config.load_kube_config(kubeconfig)
def get_pods():
t = PrettyTable(['Name', 'Node', 'Status'])
t.align['Name'] = 'l'
t.align['Status'] = 'l'
t.align['Node'] = 'l'
t.border = False
t.right_padding_width = 1
t.left_padding_width = 0
v1 = client.CoreV1Api()
label_selector = [] #["example-cnf-type=cnf-app"]
pod_list = v1.list_namespaced_pod("example-cnf", label_selector=",".join(label_selector))
for pod in pod_list.items:
reason = pod.status.phase
if pod.status.reason:
reason = pod.status.reason
has_running = False
restarts = 0
if pod.status.container_statuses:
for item in pod.status.container_statuses:
restarts += item.restart_count
if item.state.waiting and item.state.waiting.reason:
reason = item.state.waiting.reason
elif item.state.terminated and item.state.terminated.reason:
reason = item.state.terminated.reason
elif item.state.terminated:
if item.state.terminated.signal:
resaon = ("Signal:%s" % item.state.terminated.signal)
else:
reason = ("ExitCode:%s" % item.state.terminated.exit_code)
elif item.ready and item.state.running:
has_running = True
if reason == "Completed" and has_running:
pass
if pod.metadata.deletion_timestamp and pod.status.reason == "NodeLost":
reason = "Unknown"
elif pod.metadata.deletion_timestamp:
reason = "Terminating"
name = pod.metadata.name[:40]
t.add_row([name, pod.spec.node_name, reason])
print(t)
if __name__ == "__main__":
get_pods()