-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathnamespace.py
196 lines (172 loc) · 8.61 KB
/
namespace.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import time, os, argparse, json, sys
from concurrent.futures import ThreadPoolExecutor
start_time = time.time()
from modules.main import ArgParse
from modules import process as k8s
from modules.logging import Logger
from modules.get_pods import K8sPods
from modules.get_svc import K8sService
from modules.get_deploy import K8sDeploy
from modules.get_ds import K8sDaemonSet
from modules.get_sts import K8sStatefulSet
from modules.get_ns import K8sNameSpace
from modules.get_ingress import K8sIngress
from modules.get_jobs import K8sJobs
from modules.get_svc import K8sService
from modules.get_rbac import K8sNameSpaceRole, K8sNameSpaceRoleBinding
class Namespace:
def __init__(self, logger):
self.logger = logger
self.all_ns_list = K8sNameSpace.get_ns(self.logger)
def get_object_data(self, fun, k8s_object, ns, v, l):
k8s_object_list = fun
if len(k8s_object_list.items):
if not 'services' in k8s_object:
k8s.Check.security_context(k8s_object, k8s_object_list, \
['NAMESPACE', 'POD', 'CONTAINER_NAME', 'PRIVILEGED_ESC', \
'PRIVILEGED', 'READ_ONLY_FS', 'RUN_AS_NON_ROOT', \
'RUNA_AS_USER'], v, ns, l, self.logger)
k8s.Check.health_probes(k8s_object, k8s_object_list, \
['NAMESPACE', 'POD', 'CONTAINER_NAME', 'READINESS_PROPBE', \
'LIVENESS_PROBE'], v, ns, l, self.logger)
k8s.Check.resources(k8s_object, k8s_object_list, \
['NAMESPACE', 'POD', 'CONTAINER_NAME', 'LIMITS', 'REQUESTS'], \
v, ns, l, self.logger)
if k8s_object in ['deployments','statefulsets']:
k8s.Check.replica(k8s_object + 'ns', k8s_object_list, \
['NAMESPACE', 'DEPLOYMENT', 'REPLICA_COUNT'], v, ns, l, self.logger)
else:
k8s.Service.get_service(k8s_object, k8s_object_list, \
['NAMESPACE', 'SERVICE', 'SERVICE_TYPE', 'CLUSTER_IP', \
'SELECTOR'], v, ns, l, self.logger)
else:
self.logger.warning ("No {} found!".format(k8s_object))
def get_ns_data(self, v, ns, l):
data, sum_list, empty_ns = [], [], []
if not ns:
ns = 'all'
ns_list = self.all_ns_list
else:
ns_list = ns
# getting objects list in threads
with ThreadPoolExecutor(max_workers=10) as executor:
temp_deploy = executor.submit(K8sDeploy.get_deployments, ns, self.logger)
temp_ds = executor.submit(K8sDaemonSet.get_damemonsets, ns, self.logger)
temp_sts = executor.submit(K8sStatefulSet.get_sts, ns, self.logger)
temp_pods = executor.submit(K8sPods.get_pods, ns, self.logger)
temp_svc = executor.submit(K8sService.get_svc, ns, self.logger)
temp_ingress = executor.submit(K8sIngress.get_ingress, ns, self.logger)
temp_jobs = executor.submit(K8sJobs.get_jobs, ns, self.logger)
temp_role = executor.submit(K8sNameSpaceRole.list_namespaced_role, ns, self.logger)
temp_role_binding = \
executor.submit(K8sNameSpaceRoleBinding.list_namespaced_role_binding, ns, self.logger)
# stroing data from threads ran above
deployments = temp_deploy.result()
ds = temp_ds.result()
sts = temp_sts.result()
pods = temp_pods.result()
svc = temp_svc.result()
ingress = temp_ingress.result()
jobs = temp_jobs.result()
roles = temp_role.result()
role_bindings = temp_role_binding.result()
# getting count of each ns objects and printing in table
print ("\n{} namespace details:".format(ns))
data = k8s.NameSpace.get_ns_details(ns_list, deployments, ds, sts, \
pods, svc, ingress, jobs, roles, role_bindings)
# getting total object-wise count across the cluster
total_ns, total_deploy, total_ds, total_sts, total_pods, total_svc, \
total_ing , total_jobs, total_roles, total_role_bindings \
= 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
for i in data:
total_ns += 1
total_deploy = total_deploy + i[1]
total_ds = total_ds + i[2]
total_sts = total_sts + i[3]
total_pods = total_pods + i[4]
total_svc = total_svc + i[5]
total_ing = total_ing + i[6]
total_jobs = total_jobs + i[7]
total_roles = total_roles + i[8]
total_role_bindings = total_role_bindings + i[9]
if i[1] == 0 and i[2] == 0 and i[3] == 0 and i[4] == 0 and \
not i[0] in ['default', 'kube-node-lease', 'kube-public', 'local']:
empty_ns.append([i[0]])
# calculating cluster-wide count of objects if namespace is no provided
if type(ns_list) != str:
data = k8s.Output.append_hyphen(data, '--------')
data.append(["Total: " + str(total_ns), total_deploy, total_ds,
total_sts, total_pods, total_svc, total_ing, total_jobs, \
total_roles, total_role_bindings ])
headers = ['NAMESPACE', 'DEPLOYMENTS', 'DAEMONSETS', 'STATEFULSETS', \
'PODS', 'SERVICE', 'INGRESS', 'JOBS', 'ROLES', 'ROLE_BINDINGS']
k8s.Output.print_table(data, headers, True, l)
analysis = {"namespace_namespace_property": "namespace_object_count",
"total_namespaces": total_ns,
"total_deployments": total_deploy,
"total_daemonsets": total_ds,
"total_statefulsets": total_sts,
"total_servcies": total_svc,
"total_ingresses": total_ing,
"total_jobs": total_jobs,
"total_roles": total_roles,
"total_rolebindings": total_role_bindings}
json_data_all_ns_detail = k8s.Output.json_out(data[:-2], analysis, headers, 'namespace', 'namespace_details', '')
if l: self.logger.info(json_data_all_ns_detail)
# get namespace wise object details. Will give output in verbose mode
def get_all_object_data(self, ns, v, l):
print (k8s.Output.BOLD + "\nNamespace: " + \
k8s.Output.RESET + "{}".format(ns))
Namespace.get_object_data(self, K8sDeploy.get_deployments(ns, self.logger), \
'deployments', ns, v, l)
Namespace.get_object_data(self, K8sDaemonSet.get_damemonsets(ns, self.logger), \
'damemonsets', ns, v, l)
Namespace.get_object_data(self, K8sStatefulSet.get_sts(ns, self.logger), \
'statefulsets', ns, v, l)
Namespace.get_object_data(self, K8sJobs.get_jobs(ns, self.logger), \
'jobs', ns, v, l)
Namespace.get_object_data(self, K8sService.get_svc(ns, self.logger), \
'services', ns, v, l)
if v:
if type(ns_list) != str:
for item in ns_list.items:
ns = item.metadata.name
k8s.Output.separator(k8s.Output.GREEN, '-', l)
get_all_object_data(self, ns, True, l)
else:
get_all_object_data(self, ns, v, l)
# getting namespaces which are empty
if len(empty_ns) > 0:
k8s.Output.separator(k8s.Output.GREEN, '-', l)
print (k8s.Output.YELLOW + "\n[WARNING] " + k8s.Output.RESET + \
"Below {} namespaces have no workloads running: "\
.format(len(empty_ns)))
k8s.Output.print_table(empty_ns, headers, True, l)
# creating single list of namespace for json parsing
empyt_ns_list = [item for sublist in empty_ns for item in sublist]
analysis = {"namespace_property": "empty_namespace",
"empty_namespace_count": len(empty_ns),
"empty_namespace_list": empyt_ns_list
}
if l: self.logger.info(json.dumps(analysis))
return [ data , pods, svc, deployments, ds, jobs, ingress ]
def call_all(v, ns, l, logger):
call = Namespace(logger)
call.get_ns_data(v, ns, l)
def main():
args = ArgParse.arg_parse()
# args is [u, verbose, ns, l, format, silent]
logger = Logger.get_logger(args.format, args.silent)
if args:
call_all(args.verbose, args.namespace, args.logging, logger)
k8s.Output.time_taken(start_time)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print(k8s.Output.RED + "[ERROR] " \
+ k8s.Output.RESET + 'Interrupted from keyboard!')
try:
sys.exit(0)
except SystemExit:
os._exit(0)