-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanup.py
executable file
·114 lines (86 loc) · 3 KB
/
cleanup.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python3
# Std libs
import argparse
import sys
import kubernetes
import urllib3
# 3rd party deps
from kubernetes import client, config
from kubernetes.client.rest import ApiException
def pod_is_terminated(pod) -> bool:
try:
reason = pod.status.reason
except Exception:
return False
if reason == 'Terminated':
return True
return False
def no_node_for_pod(pod) -> bool:
try:
reason = pod.status.reason
except Exception:
return False
if reason == 'NodeAffinity':
return True
return False
def container_cannot_run(pod) -> bool:
try:
containers = pod.status.container_statuses
except Exception:
return False
if not containers:
return False
terminated_reasons = [
'ContainerCannotRun',
'StartError'
]
for container in containers:
try:
if container.last_state.terminated.reason in terminated_reasons:
return True
except Exception:
continue
return False
def should_pod_be_deleted(pod) -> bool:
return pod_is_terminated(pod) or container_cannot_run(pod) or no_node_for_pod(pod)
def get_namespaces_to_check(api):
for namespace in api.list_namespace().items:
if namespace.metadata.name not in ('kube-system', 'reboot-coordinator'):
yield namespace
def get_pods_to_check(namespace, api, dry_run=False):
for pod in api.list_namespaced_pod(namespace=namespace.metadata.name).items:
if dry_run:
print(f"\tChecking pod: {pod.metadata.name}")
if should_pod_be_deleted(pod):
yield pod
if __name__ == '__main__':
urllib3.disable_warnings()
try:
config.load_incluster_config()
except kubernetes.config.config_exception.ConfigException:
try:
config.load_kube_config()
except kubernetes.config.config_exception.ConfigException as e2:
raise e2
parser = argparse.ArgumentParser(description="Pod Cleaner")
parser.add_argument('--dry-run', action='store_true')
parser.add_argument('--verbose', action='store_true')
parser.add_argument('--debug', action='store_true')
args = parser.parse_args()
print("dry-run" if args.dry_run else 'wet-run')
api = client.CoreV1Api()
for namespace in get_namespaces_to_check(api):
if args.dry_run or args.verbose:
print(f"Checking namespace: {namespace.metadata.name}")
for pod in get_pods_to_check(namespace, api, args.dry_run):
if args.dry_run:
print('dry-run: would have deleted',
pod.metadata.name, 'in', pod.metadata.namespace)
continue
try:
print('deleting', pod.metadata.name,
'in', pod.metadata.namespace)
api.delete_namespaced_pod(
pod.metadata.name, pod.metadata.namespace)
except ApiException as e:
print('exception while deleting: ', e)