-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.py
308 lines (253 loc) · 9.63 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
# Copyright (C) 2022 Nokia
# Licensed under the MIT License
# SPDX-License-Identifier: MIT
import json
import logging
import os
import subprocess
import urllib.parse
import kubernetes.client
import kubernetes.config
import requests
from flask import Flask, abort, request
from flask.helpers import send_file, send_from_directory
from flask.json import jsonify
from flask_httpauth import HTTPTokenAuth
from werkzeug.datastructures import Headers
application = app = Flask(__name__)
if __name__ != "__main__":
gunicorn_logger = logging.getLogger("gunicorn.error")
app.logger.handlers = gunicorn_logger.handlers
app.logger.setLevel(gunicorn_logger.level)
app.config["NO_TOKENS"] = os.getenv("NO_TOKENS") == "1"
app.config["DAEMONSET"] = os.getenv("DAEMONSET") == "1"
if port := os.getenv("KOREDUMP_DAEMONSET_PORT"):
app.config["DAEMONSET_PORT"] = int(port)
auth = HTTPTokenAuth()
cores = {}
cores_stat = None
decompression_methods = {
".lz4": ["lz4", "-c", "-q", "-d"],
".xz": ["xz", "-c", "-q", "-d"],
".zst": ["zstd", "-c", "-q", "-d"],
}
@auth.verify_token
def verify_token(token):
if app.config["NO_TOKENS"]:
return True
if len(token) == 0:
return False
try:
body = kubernetes.client.V1TokenReview(spec={"token": token})
ret: kubernetes.client.V1TokenReview = (
kubernetes.client.AuthenticationV1Api().create_token_review(body)
)
return ret.status.authenticated
except Exception as ex:
app.logger.debug("Token review failed: %s", ex)
return False
def read_cores():
"""
Read available cores from /koredump/index.json
The file is generated by koremonitor.
"""
if not app.config["DAEMONSET"]:
return
global cores, cores_stat
index_path = "/koredump/index.json"
try:
st = os.stat(index_path)
if cores_stat:
if st.st_mtime <= cores_stat.st_mtime and st.st_size == cores_stat.st_size:
return
app.logger.info("File has changed, reloading: %s", index_path)
cores_stat = st
with open(index_path) as fp:
cores = json.load(fp)
app.logger.info("Reloaded %s with %d cores.", index_path, len(cores))
except Exception:
pass
if app.config["DAEMONSET"]:
read_cores()
if not os.getenv("FAKE_K8S"):
kubernetes.config.load_incluster_config()
def get_ds_pods():
return kubernetes.client.CoreV1Api().list_pod_for_all_namespaces(
label_selector="koredump.daemonset=1"
)
def get_ds_pod_ips():
if os.getenv("FAKE_K8S"):
return ["127.0.0.1"]
pods = get_ds_pods()
ret = []
for pod in pods.items:
ret.append(pod.status.pod_ip)
return ret
def get_ds_pod_ip(node_name):
if os.getenv("FAKE_K8S"):
return "127.0.0.1"
pods = get_ds_pods()
for pod in pods.items:
if pod.spec.node_name == node_name:
return pod.status.pod_ip
return None
@app.get("/health")
def health():
return "OK\n"
def filtered_core_metadata(core):
"""Filter out some internal metadata, to avoid returning them via REST API."""
core = core.copy()
for key in ["_systemd_coredump", "_systemd_journal", "_core_dir"]:
if key in core:
del core[key]
return core
def sorted_cores(cores):
"""List of cores sorted by timestamp, oldest core is last."""
return sorted(
cores,
key=lambda core: core["COREDUMP_TIMESTAMP"]
if "COREDUMP_TIMESTAMP" in core
else core["id"],
)
if app.config["DAEMONSET"]:
@app.get("/apiv1/cores")
@auth.login_required
def get_cores():
arg_namespace = request.args.get("namespace", "")
arg_pod = request.args.get("pod", "")
read_cores()
ret = []
for core_id in cores:
if "_DELETED" in cores[core_id]:
continue
if len(arg_namespace) > 0 and arg_namespace != cores[core_id].get(
"namespace"
):
continue
if len(arg_pod) > 0 and arg_pod != cores[core_id].get("pod"):
continue
ret.append(filtered_core_metadata(cores[core_id]))
return jsonify(sorted_cores(ret))
@app.get("/apiv1/cores/metadata/<string:core_id>")
@auth.login_required
def get_core_metadata(core_id):
read_cores()
if core_id not in cores or "_DELETED" in cores[core_id]:
abort(404)
return filtered_core_metadata(cores[core_id])
@app.get("/apiv1/cores/download/<string:core_id>")
@auth.login_required
def get_core_download(core_id: str):
read_cores()
if core_id not in cores or "_DELETED" in cores[core_id]:
abort(404)
if request.args.get("decompress") != "true":
return send_from_directory(
cores[core_id]["_core_dir"], core_id, as_attachment=True
)
(download_name, ext) = os.path.splitext(core_id)
if ext not in decompression_methods:
abort(415)
core_path = os.path.join(cores[core_id]["_core_dir"], core_id)
app.logger.debug(f"decompress requested: {core_path}")
try:
with open(core_path, "rb") as fp:
process = subprocess.Popen(
decompression_methods[ext], stdin=fp, stdout=subprocess.PIPE
)
app.logger.debug("%s", process)
return send_file(
process.stdout, as_attachment=True, download_name=download_name
)
except FileNotFoundError:
abort(404)
@app.delete("/apiv1/cores/delete/<string:core_id>")
@auth.login_required
def delete_core(core_id):
read_cores()
if core_id not in cores or "_DELETED" in cores[core_id]:
abort(404)
cores[core_id]["_DELETED"] = True
return jsonify({})
else:
@app.get("/apiv1/cores")
def get_cores():
headers = {"Authorization": request.headers.get("Authorization")}
args = urllib.parse.urlencode(request.args)
ret = []
for pod_ip in get_ds_pod_ips():
url = f"http://{pod_ip}:{app.config['DAEMONSET_PORT']}/apiv1/cores?{args}"
app.logger.debug("GET %s", url)
resp = requests.get(url, headers=headers)
if not resp.ok:
abort(resp.status_code)
resp.encoding = "utf-8"
ret.extend(resp.json())
return jsonify(sorted_cores(ret))
@app.get("/apiv1/cores/metadata/<string:node>/<string:core_id>")
def get_node_core_metadata(node, core_id):
pod_ip = get_ds_pod_ip(node)
if not pod_ip:
abort(404)
headers = {"Authorization": request.headers.get("Authorization")}
url = f"http://{pod_ip}:{app.config['DAEMONSET_PORT']}/apiv1/cores/metadata/{core_id}"
app.logger.debug("GET %s", url)
resp = requests.get(url, headers=headers)
if not resp.ok:
abort(resp.status_code)
resp.encoding = "utf-8"
return resp.json()
@app.get("/apiv1/cores/download/<string:node>/<string:core_id>")
def get_node_core_download(node, core_id):
pod_ip = get_ds_pod_ip(node)
if not pod_ip:
abort(404)
headers = {"Authorization": request.headers.get("Authorization")}
args = urllib.parse.urlencode(request.args)
url = f"http://{pod_ip}:{app.config['DAEMONSET_PORT']}/apiv1/cores/download/{core_id}?{args}"
# Check headers from the DaemonSet server API, and pass them forward.
# Content-Type: application/zstd
# Content-Length: 19495
# Last-Modified: Mon, 10 Jan 2022 12:45:06 GMT
# Cache-Control: no-cache
resp_headers = Headers()
with requests.head(url, headers=headers) as resp:
if not resp.ok:
abort(resp.status_code)
for k, v in resp.headers.items():
if k in ("Date", "Server"):
continue
resp_headers.add(k, v)
if "Content-Type" not in resp_headers:
resp_headers.add("Content-Type", "application/octet-stream")
def stream_core():
with requests.get(url, headers=headers, stream=True) as resp:
if not resp.ok:
abort(resp.status_code)
for chunk in resp.iter_content(chunk_size=64 * 1024):
yield chunk
return app.response_class(stream_core(), headers=resp_headers)
@app.delete("/apiv1/cores/delete/<string:node>/<string:core_id>")
def delete_node_core(node, core_id):
pod_ip = get_ds_pod_ip(node)
if not pod_ip:
abort(404)
headers = {"Authorization": request.headers.get("Authorization")}
url = f"http://{pod_ip}:{app.config['DAEMONSET_PORT']}/apiv1/cores/delete/{core_id}"
app.logger.debug("DELETE %s", url)
resp = requests.delete(url, headers=headers)
if not resp.ok:
abort(resp.status_code)
resp.encoding = "utf-8"
return resp.json()
if __name__ == "__main__":
#
# Run with Flask in development mode:
# NO_TOKENS=1 FLASK_ENV=development PORT=5001 DAEMONSET=1 FAKE_K8S=1 python3 ./app.py
# NO_TOKENS=1 FLASK_ENV=development PORT=5000 KOREDUMP_DAEMONSET_PORT=5001 DAEMONSET=0 FAKE_K8S=1 python3 ./app.py
#
# Run with Gunicorn in development mode:
# NO_TOKENS=1 FLASK_ENV=development PORT=5001 DAEMONSET=1 FAKE_K8S=1 gunicorn --access-logfile=- --log-level=debug app
# NO_TOKENS=1 FLASK_ENV=development PORT=5000 KOREDUMP_DAEMONSET_PORT=5001 DAEMONSET=0 FAKE_K8S=1 gunicorn --access-logfile=- --log-level=debug app
#
app.run(port=int(os.getenv("PORT")))