-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
335 lines (294 loc) · 10.2 KB
/
app.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# This file is part of k8s-status
#
# Copyright (c) 2019 Bryan Davis and contributors
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
"""Web UI for exploring a Toolfroge Kubernetes cluster."""
import collections
import datetime
import logging
import os
import flask
import natsort
import yaml
import k8s.client
app = flask.Flask(__name__)
# Load configuration from YAML file(s).
# See default_config.yaml for more information
__dir__ = os.path.dirname(__file__)
app.config.update(
yaml.safe_load(open(os.path.join(__dir__, "default_config.yaml")))
)
try:
app.config.update(
yaml.safe_load(open(os.path.join(__dir__, "config.yaml")))
)
except IOError:
# It is ok if there is no local config file
pass
logging.getLogger().addHandler(flask.logging.default_handler)
@app.route("/")
def home():
"""Show basic cluster info."""
ctx = {}
try:
cached = "purge" not in flask.request.args
ctx.update(
{
"version": k8s.client.get_version(),
"pods": k8s.client.get_pods_by_namespace(cached=cached),
"metrics": k8s.client.get_summary_metrics(cached=cached),
"namespaces": k8s.client.get_active_namespaces(cached=cached)[
"namespaces"
],
}
)
except Exception:
app.logger.exception("Error collecting statistics")
return flask.render_template("home.html", **ctx)
@app.route("/robots.txt")
def robots_txt():
"""Return a deny-all robots policy."""
return flask.Response(
"User-Agent: *\nDisallow: /\n", mimetype="text/plain"
)
@app.route("/healthz")
def healthz():
"""Respond to health check requests."""
return "ok"
@app.route("/nodes/")
def nodes():
"""List nodes."""
ctx = {}
try:
cached = "purge" not in flask.request.args
pods = collections.defaultdict(int)
for pod in k8s.client.get_all_pods(cached=cached)["items"]:
if pod.status.phase == "Succeeded":
continue
pods[pod.spec.node_name] += 1
ctx.update(
{
"nodes": natsort.natsorted(
k8s.client.get_nodes(cached=cached)["items"],
key=lambda node: node.metadata.name,
),
"metrics": {
m["metadata"]["name"]: m
for m in k8s.client.get_nodes_metrics(cached=cached)[
"items"
]
},
"pods": pods,
}
)
except Exception:
app.logger.exception("Error collecting nodes")
return flask.render_template("nodes.html", **ctx)
@app.route("/nodes/<name>/")
def node(name):
"""Describe a node."""
ctx = {}
try:
cached = "purge" not in flask.request.args
ctx.update(
{
"node": k8s.client.get_node(name, cached=cached)["node"],
"metrics": k8s.client.get_node_metrics(name, cached=cached)[
"metrics"
],
"pods": [
pod
for pod in k8s.client.get_all_pods(cached=cached)["items"]
if pod.spec.node_name == name
],
}
)
except Exception:
app.logger.exception("Error collecting node")
return flask.render_template("node.html", **ctx)
@app.route("/namespaces/")
def namespaces():
"""List namespaces."""
ctx = {}
try:
cached = "purge" not in flask.request.args
ctx.update(
{
"namespaces": k8s.client.get_namespaces(cached=cached),
"active": k8s.client.get_active_namespaces(cached=cached)[
"namespaces"
],
}
)
except Exception:
app.logger.exception("Error collecting namespaces")
return flask.render_template("namespaces.html", **ctx)
@app.route("/namespaces/<namespace>/")
def namespace(namespace):
"""Get details for a given namespace."""
ctx = {
"namespace": namespace,
}
try:
cached = "purge" not in flask.request.args
ctx.update(
{
"pods": k8s.client.get_pods(namespace, cached=cached),
"services": k8s.client.get_services(namespace, cached=cached),
"ingresses": k8s.client.get_ingresses(
namespace, cached=cached
),
"daemonsets": k8s.client.get_daemonsets(
namespace, cached=cached
),
"deployments": k8s.client.get_deployments(
namespace, cached=cached
),
"replicasets": k8s.client.get_replicasets(
namespace, cached=cached
),
"statefulsets": k8s.client.get_statefulsets(
namespace, cached=cached
),
"cronjobs": k8s.client.get_cronjobs(namespace, cached=cached),
"jobs": k8s.client.get_jobs(namespace, cached=cached),
"quota": k8s.client.get_quota(namespace, cached=cached),
}
)
except Exception:
app.logger.exception("Error collecting namespace %s", namespace)
return flask.render_template("namespace.html", **ctx)
@app.route("/namespaces/<namespace>/pods/<pod>/")
def pod(namespace, pod):
"""Get details for a given pod."""
ctx = {
"namespace": namespace,
}
try:
cached = "purge" not in flask.request.args
ctx.update(
{"pod": k8s.client.get_pod(namespace, pod, cached=cached)["pod"]}
)
except Exception:
app.logger.exception("Error collecting namespace %s", namespace)
if "pod" in ctx and ctx["pod"]:
return flask.render_template("pod.html", **ctx)
else:
flask.flash("Pod {} not found.".format(pod), "danger")
return flask.redirect(flask.url_for("namespace", namespace=namespace))
@app.route("/namespaces/<namespace>/ingresses/<name>/")
def ingress(namespace, name):
"""Get details for a given ingress."""
ctx = {
"namespace": namespace,
}
try:
cached = "purge" not in flask.request.args
ctx.update(
{
"ingress": k8s.client.get_ingress(
namespace, name, cached=cached
)["ingress"],
}
)
except Exception:
app.logger.exception("Error collecting namespace %s", namespace)
if "ingress" in ctx and ctx["ingress"]:
return flask.render_template("ingress.html", **ctx)
else:
flask.flash("Ingress {} not found.".format(name), "danger")
return flask.redirect(flask.url_for("namespace", namespace=namespace))
@app.route("/images/")
def images():
"""List all images in use on the cluster."""
ctx = {}
try:
cached = "purge" not in flask.request.args
ctx.update({"images": k8s.client.get_images(cached=cached)})
except Exception:
app.logger.exception("Error collecting images")
return flask.render_template("images.html", **ctx)
@app.route("/images/<path:name>/")
def image(name):
"""List pods using an image."""
ctx = {
"image": name,
}
try:
cached = "purge" not in flask.request.args
ctx.update(
{"pods": k8s.client.get_images(cached=cached)["items"][name]}
)
except Exception:
app.logger.exception("Error collecting image '%s'", name)
return flask.render_template("image.html", **ctx)
@app.errorhandler(404)
def page_not_found(e):
"""Handle 404 errors."""
flask.flash("Requested URL not found.", "danger")
return flask.redirect(flask.url_for("home"))
@app.template_filter("contains")
def contains(haystack, needle):
"""Search a haystack for a needle."""
return needle in haystack
@app.template_filter("duration")
def duration(start, end=None, max_parts=3):
"""Compute a duration relative to the current time or a given time."""
if start is None:
return ""
if end is None:
end = datetime.datetime.now(tz=start.tzinfo)
diff_secs = abs((end - start).total_seconds())
parts = []
if diff_secs > 31556952:
parts.append("{}y".format(int(diff_secs // 31556952)))
diff_secs = diff_secs % 31556952
if diff_secs > 604800:
parts.append("{}w".format(int(diff_secs // 604800)))
diff_secs = diff_secs % 604800
if diff_secs > 86400:
parts.append("{}d".format(int(diff_secs // 86400)))
diff_secs = diff_secs % 86400
if diff_secs > 3600:
parts.append("{}h".format(int(diff_secs // 3600)))
diff_secs = diff_secs % 3600
if diff_secs > 60:
parts.append("{}m".format(int(diff_secs // 60)))
diff_secs = diff_secs % 60
if diff_secs >= 1:
parts.append("{}s".format(int(diff_secs)))
if not parts:
parts.append("{}ms".format(int(diff_secs * 1000)))
parts = parts[:max_parts]
return "".join(parts)
@app.template_filter("yaml")
def pprint_yaml(obj):
"""Dump an object as YAML."""
return yaml.dump(obj, explicit_start=True, width=79, indent=2)
@app.template_filter("parse_quantity")
def parse_quantity(obj):
"""Parse kubernetes quantity like 200Mi to a decimal number."""
return k8s.client.parse_quantity(obj)
@app.context_processor
def inject_base_variables():
"""Variables to be usable on all templates."""
return {
"project": app.config["PROJECT"],
"toolsadmin_url": app.config["TOOLSADMIN_URL"],
"banner": app.config.get("BANNER"),
}