-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpod_watch.py
80 lines (68 loc) · 2.86 KB
/
pod_watch.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import sys
import time
import subprocess
import argparse
import curses
# Define colors using curses color constants
FG_DEF = curses.COLOR_WHITE
FG_RED = curses.COLOR_RED
FG_GREEN = curses.COLOR_GREEN
FG_YELLOW = curses.COLOR_YELLOW
def get_pods(namespace, kubeconfig):
"""Get the list of pods in the given namespace using kubectl."""
cmd = ["kubectl", "get", "pods"]
if namespace:
cmd.extend(["-n", namespace])
if kubeconfig:
cmd.extend(["--kubeconfig", kubeconfig])
result = subprocess.run(cmd, capture_output=True, text=True)
return result.stdout
def parse_pod_status(line):
"""Parse the pod status from the kubectl get pods output."""
parts = line.split()
if len(parts) >= 4:
name = parts[0]
ready = parts[1]
status = parts[2]
restarts = parts[3]
age = parts[-1]
return name, ready, status, restarts, age
return None, None, None, None, None
def main(stdscr, namespace, kubeconfig, refresh_time):
curses.curs_set(0)
curses.init_pair(1, FG_RED, curses.COLOR_BLACK)
curses.init_pair(2, FG_GREEN, curses.COLOR_BLACK)
curses.init_pair(3, FG_YELLOW, curses.COLOR_BLACK)
while True:
stdscr.clear()
pods_output = get_pods(namespace, kubeconfig)
lines = pods_output.splitlines()
if not lines:
stdscr.addstr(0, 0, "No pods found in the namespace")
else:
# Print headers
headers = lines[0]
stdscr.addstr(0, 0, headers)
for i, line in enumerate(lines[1:], start=1):
name, ready, status, restarts, age = parse_pod_status(line)
if status:
if status in ["Failed", "Terminating", "CrashLoopBackOff", "ErrImagePull"]:
color = curses.color_pair(1)
elif status in ["Pending", "Unknown", "ContainerCreating", "PodInitializing", "Init"]:
color = curses.color_pair(3)
elif status in ["Running", "Succeeded"] and ready.split('/')[0] == ready.split('/')[1]:
color = curses.color_pair(2)
else:
color = curses.color_pair(1)
else:
color = curses.color_pair(1)
stdscr.addstr(i, 0, line, color)
stdscr.refresh()
time.sleep(refresh_time)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Watch Kubernetes pods and highlight not ready ones.")
parser.add_argument('-n', '--namespace', type=str, help='Kubernetes namespace to watch')
parser.add_argument('-k', '--kubeconfig', type=str, help='Path to kubeconfig file')
parser.add_argument('-t', '--time', type=int, default=10, help='Refresh time in seconds')
args = parser.parse_args()
curses.wrapper(main, args.namespace, args.kubeconfig, args.time)