-
Notifications
You must be signed in to change notification settings - Fork 5
/
kron.py
474 lines (381 loc) · 13.8 KB
/
kron.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
import logging
from kubernetes import client
from kubernetes import config as kubeconfig
from kubernetes.config import ConfigException
from kubernetes.client.rest import ApiException
from datetime import datetime, timezone
from typing import List
import config
log = logging.getLogger("app.kron")
if not config.TEST:
try:
# Load configuration inside the Pod
kubeconfig.load_incluster_config()
except ConfigException:
# Load configuration from KUBECONFIG
kubeconfig.load_kube_config()
# Create the Api clients
v1 = client.CoreV1Api()
batch = client.BatchV1Api()
generic = client.ApiClient()
def namespace_filter(func):
"""Decorator that short-circuits and returns False if the wrapped function attempts to access an unlisted namespace
Args:
func (function): The function to wrap. Must have `namespace` as an arg to itself
"""
def wrapper(namespace: str = None, *args, **kwargs):
if config.ALLOW_NAMESPACES and namespace:
if namespace in config.ALLOW_NAMESPACES.split(","):
return func(namespace, *args, **kwargs)
else:
return func(namespace, *args, **kwargs)
return False
return wrapper
def _filter_dict_fields(items: List[dict], fields: List[str] = ["name"]) -> List[dict]:
"""
Filter a given list of API object down to only the metadata fields listed.
Args:
response (Obj): A kubernetes client API object or object list.
filds (List of str): The desired fields to retain from the object
Returns:
dict: The object is converted to a dict and retains only the fields
provided.
"""
return [
{field: item.get("metadata").get(field) for field in fields} for item in items
]
def _clean_api_object(api_object: object) -> dict:
"""Convert API object to JSON and strip managedFields"""
api_dict = generic.sanitize_for_serialization(api_object)
api_dict["metadata"].pop("managedFields", None)
return api_dict
def _get_time_since(datestring: str) -> str:
"""
Calculate the time difference between the input datestring and the current time
and return a human-readable string.
Args:
datestring (str): A string representing a timestamp in ISO format.
Returns:
str: A human-readable time difference string.
"""
current_time = datetime.now(timezone.utc)
input_time = datetime.fromisoformat(datestring)
time_difference = current_time - input_time
if time_difference.total_seconds() < 0:
return "In the future"
days = time_difference.days
hours, remainder = divmod(time_difference.seconds, 3600)
minutes, seconds = divmod(remainder, 60)
if days > 0:
return f"{days}d {hours}h {minutes}m {seconds}s"
elif hours > 0:
return f"{hours}h {minutes}m {seconds}s"
elif minutes > 0:
return f"{minutes}m {seconds}s"
else:
return f"{seconds}s"
def _has_label(api_object: object, k: str, v: str) -> bool:
"""
Return True if a label is present with the specified key and value.
Args:
api_object (dict): The API object with metadata.
k (str): The label key to check.
v (str): The label value to check.
Returns:
bool: True if the label is present with the specified key and value, otherwise False.
"""
metadata = api_object.get("metadata", {})
labels = metadata.get("labels", {})
return labels.get(k) == v
def pod_is_owned_by(api_dict: dict, owner_name: str) -> bool:
"""Return whether a job or pod contains an ownerReference to the given cronjob or job name
Args:
object (dict): A dict representation of a job or pod
owner_name (str): The name of a cronjob or job which may have created the given job or pod
Returns:
bool: True if an ownerReference contains the given owner_name
"""
owner_references = api_dict["metadata"].get("ownerReferences", [])
return any(owner_ref["name"] == owner_name for owner_ref in owner_references)
@namespace_filter
def get_cronjobs(namespace: str = None) -> List[dict]:
"""Get names of cronjobs in a given namespace. If namespace is not provided, return CronJobs
from all namespaces.
Args:
namespace (str, optional): namespace to examine. Defaults to "" (all namespaces).
Returns:
List of dict: A list of dicts containing the name and namespace of each cronjob.
"""
try:
cronjobs = []
if not namespace:
if not config.ALLOW_NAMESPACES:
cronjobs = [
_clean_api_object(item)
for item in batch.list_cron_job_for_all_namespaces().items
]
else:
cronjobs = []
for allowed in config.ALLOW_NAMESPACES.split(","):
cronjobs.extend(
[
_clean_api_object(item)
for item in batch.list_namespaced_cron_job(
namespace=allowed
).items
]
)
else:
cronjobs = [
_clean_api_object(item)
for item in batch.list_namespaced_cron_job(namespace=namespace).items
]
fields = ["name", "namespace"]
sorted_cronjobs = sorted(
_filter_dict_fields(cronjobs, fields), key=lambda x: x["name"]
)
return sorted_cronjobs
except ApiException as e:
log.error(e)
response = {
"error": 500,
"exception": {
"status": e.status,
"reason": e.reason,
"message": e.body["message"],
},
}
return response
@namespace_filter
def get_cronjob(namespace: str, cronjob_name: str) -> dict:
"""Get the details of a given CronJob as a dict
Args:
namespace (str): The namespace
cronjob_name (str): The name of an existing CronJob
Returns:
dict: A dict of the CronJob API object
"""
try:
cronjob = batch.read_namespaced_cron_job(cronjob_name, namespace)
return _clean_api_object(cronjob)
except ApiException:
return False
@namespace_filter
def get_jobs(namespace: str, cronjob_name: str) -> List[dict]:
"""Return jobs belonging to a given CronJob name
Args:
namespace (str): The namespace
cronjob_name (str): The CronJob which owns jobs to return
Returns:
List of dicts: A list of dicts of each job created by the given CronJob name
"""
try:
jobs = batch.list_namespaced_job(namespace=namespace)
cleaned_jobs = [_clean_api_object(job) for job in jobs.items]
filtered_jobs = [
job
for job in cleaned_jobs
if pod_is_owned_by(job, cronjob_name)
or _has_label(job, "kronic.mshade.org/created-from", cronjob_name)
]
for job in filtered_jobs:
job["status"]["age"] = _get_time_since(job["status"]["startTime"])
return filtered_jobs
except ApiException as e:
log.error(e)
response = {
"error": 500,
"exception": {
"status": e.status,
"reason": e.reason,
"message": e.body["message"],
},
}
return response
@namespace_filter
def get_pods(namespace: str, job_name: str = None) -> List[dict]:
"""Return pods related to jobs in a namespace
Args:
namespace (str): The namespace from which to fetch pods
job_name (str, optional): Fetch pods owned by jobs. Defaults to None.
Returns:
List of dicts: A list of pod dicts
"""
try:
all_pods = v1.list_namespaced_pod(namespace=namespace)
cleaned_pods = [_clean_api_object(pod) for pod in all_pods.items]
filtered_pods = [
pod for pod in cleaned_pods if pod_is_owned_by(pod, job_name) or (not job_name)
]
for pod in filtered_pods:
pod["status"]["age"] = _get_time_since(pod["status"]["startTime"])
return filtered_pods
except ApiException as e:
log.error(e)
response = {
"error": 500,
"exception": {
"status": e.status,
"reason": e.reason,
"message": e.body["message"],
},
}
return response
@namespace_filter
def get_jobs_and_pods(namespace: str, cronjob_name: str) -> List[dict]:
"""Get jobs and their pods under a `pods` element for display purposes
Args:
namespace (str): The namespace
cronjob_name (str): The CronJob name to filter jobs and pods by
Returns:
List of dicts: A list of job dicts, each with a jobs element containing a list of pods the job created
"""
jobs = get_jobs(namespace, cronjob_name)
all_pods = get_pods(namespace)
for job in jobs:
job["pods"] = [pod for pod in all_pods if pod_is_owned_by(pod, job["metadata"]["name"])]
return jobs
@namespace_filter
def get_pod_logs(namespace: str, pod_name: str) -> str:
"""Return plain text logs for <pod_name> in <namespace>"""
try:
logs = v1.read_namespaced_pod_log(
pod_name, namespace, tail_lines=1000, timestamps=True
)
return logs
except ApiException as e:
if e.status == 404:
return f"Kronic> Error fetching logs: {e.reason}"
@namespace_filter
def trigger_cronjob(namespace: str, cronjob_name: str) -> dict:
try:
# Retrieve the CronJob template
cronjob = batch.read_namespaced_cron_job(name=cronjob_name, namespace=namespace)
job_template = cronjob.spec.job_template
# Create a unique name indicating a manual invocation
date_stamp = datetime.now(timezone.utc).strftime("%Y%m%d%H%M%S-%f")
job_template.metadata.name = (
f"{cronjob.metadata.name[:16]}-manual-{date_stamp}"[:63]
)
# Set labels to identify jobs created by kronic
job_template.metadata.labels = {
"kronic.mshade.org/manually-triggered": "true",
"kronic.mshade.org/created-from": cronjob_name,
}
trigger_job = batch.create_namespaced_job(
body=job_template, namespace=namespace
)
return _clean_api_object(trigger_job)
except ApiException as e:
log.error(e)
response = {
"error": 500,
"exception": {
"status": e.status,
"reason": e.reason,
"message": e.body["message"],
},
}
return response
@namespace_filter
def toggle_cronjob_suspend(namespace: str, cronjob_name: str) -> dict:
"""Toggle a CronJob's suspend flag on or off
Args:
namespace (str): The namespace
cronjob_name (str): The cronjob name
Returns:
dict: The full cronjob object is returned as a dict
"""
try:
suspended_status = batch.read_namespaced_cron_job_status(
name=cronjob_name, namespace=namespace
)
patch_body = {"spec": {"suspend": not suspended_status.spec.suspend}}
cronjob = batch.patch_namespaced_cron_job(
name=cronjob_name, namespace=namespace, body=patch_body
)
return _clean_api_object(cronjob)
except ApiException as e:
log.error(e)
response = {
"error": 500,
"exception": {
"status": e.status,
"reason": e.reason,
"message": e.body["message"],
},
}
return response
@namespace_filter
def update_cronjob(namespace: str, spec: str) -> dict:
"""Update/edit a CronJob configuration via patch
Args:
namespace (str): The namespace
spec (dict): A cronjob spec as a dict object
Returns:
dict: Returns the updated cronjob spec as a dict, or an error response
"""
try:
name = spec["metadata"]["name"]
if get_cronjob(namespace, name):
cronjob = batch.patch_namespaced_cron_job(name, namespace, spec)
else:
cronjob = batch.create_namespaced_cron_job(namespace, spec)
return _clean_api_object(cronjob)
except ApiException as e:
log.error(e)
response = {
"error": 500,
"exception": {
"status": e.status,
"reason": e.reason,
"message": e.body["message"],
},
}
return response
@namespace_filter
def delete_cronjob(namespace: str, cronjob_name: str) -> dict:
"""Delete a CronJob
Args:
namespace (str): The namespace
cronjob_name (str): The name of the CronJob to delete
Returns:
dict: Returns a dict of the deleted CronJob, or an error status
"""
try:
deleted = batch.delete_namespaced_cron_job(cronjob_name, namespace)
return _clean_api_object(deleted)
except ApiException as e:
log.error(e)
response = {
"error": 500,
"exception": {
"status": e.status,
"reason": e.reason,
"message": e.body["message"],
},
}
return response
@namespace_filter
def delete_job(namespace: str, job_name: str) -> dict:
"""Delete a Job
Args:
namespace (str): The namespace
job_name (str): The name of the Job to delete
Returns:
str: Returns a dict of the deleted Job, or an error status
"""
try:
deleted = batch.delete_namespaced_job(job_name, namespace)
return _clean_api_object(deleted)
except ApiException as e:
log.error(e)
response = {
"error": 500,
"exception": {
"status": e.status,
"reason": e.reason,
"message": e.body["message"],
},
}
return response