-
Notifications
You must be signed in to change notification settings - Fork 1
/
jlab.py
463 lines (431 loc) · 16.8 KB
/
jlab.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
import logging
import os
from jinja2 import Template
import yaml
import jobutils
from kubernetes import client, config
from kubernetes.client.rest import ApiException
import envvars
import pytz
import datetime
import requests
STATUS_OK = 'ok'
STATUS_ERROR = 'error'
logger = logging.getLogger(__name__)
config.load_incluster_config()
api_v1 = client.CoreV1Api()
apps_v1_api = client.AppsV1Api()
networking_v1_beta1_api = client.NetworkingV1beta1Api()
namespace = jobutils.get_namespace()
def create_deployment(apps_v1_api, username, token, gpu):
name = 'jlab-{}'.format(username)
try:
init_container = client.V1Container(
name='{}-init'.format(name),
image="ubuntu:18.04",
image_pull_policy="IfNotPresent",
command=["/bin/sh"],
args=["-c", "chown 1001:1001 /persistent_volume /jobs/query /jobs/cutout"],
volume_mounts=[
client.V1VolumeMount(
name='persistent-volume',
mount_path="/persistent_volume",
sub_path='{}/jupyter'.format(username)
),
client.V1VolumeMount(
name='persistent-volume',
mount_path="/jobs/query",
sub_path='{}/query'.format(username)
),
client.V1VolumeMount(
name='persistent-volume',
mount_path="/jobs/cutout",
sub_path='{}/cutout'.format(username)
),
]
)
if gpu == True:
limits = {
'nvidia.com/gpu': 1
}
else:
limits = None
container = client.V1Container(
name=name,
image=envvars.DOCKER_IMAGE_JLAB_SERVER,
resources=client.V1ResourceRequirements(
limits=limits
),
image_pull_policy="Always",
ports=[client.V1ContainerPort(container_port=8888)],
env=[
client.V1EnvVar(
name='DES_USER',
value=username
),
client.V1EnvVar(
name='PIP_TARGET',
value='/home/jovyan/work/.pip'
),
client.V1EnvVar(
name='PYTHONPATH',
value='/home/jovyan/work/.pip'
)
],
volume_mounts=[
client.V1VolumeMount(
name='jupyter-config',
mount_path="/home/jovyan/.jupyter/"
),
client.V1VolumeMount(
name='persistent-volume',
mount_path="/home/jovyan/jobs/cutout",
sub_path='{}/cutout'.format(username)
),
client.V1VolumeMount(
name='persistent-volume',
mount_path="/home/jovyan/jobs/query",
sub_path='{}/query'.format(username)
),
client.V1VolumeMount(
name='persistent-volume',
mount_path="/home/jovyan/work",
sub_path='{}/jupyter'.format(username)
)
]
)
volume_config = client.V1Volume(
name='jupyter-config',
config_map=client.V1ConfigMapVolumeSource(
name=name,
items=[client.V1KeyToPath(
key=name,
path="jupyter_notebook_config.py"
)]
)
)
volume_persistent = client.V1Volume(
name='persistent-volume',
persistent_volume_claim=client.V1PersistentVolumeClaimVolumeSource(
claim_name=envvars.PVC_NAME_BASE
)
)
# Template
template = client.V1PodTemplateSpec(
metadata=client.V1ObjectMeta(labels={"app": name}),
spec=client.V1PodSpec(
image_pull_secrets=[
client.V1LocalObjectReference(
name='registry-auth-jupyter'
)
],
init_containers=[
init_container
],
containers=[
container
],
volumes=[
volume_config,
volume_persistent
],
node_selector = {'gpu': '{}'.format(gpu).lower()}
)
)
# Spec
spec = client.V1DeploymentSpec(
replicas=1,
template=template,
selector=client.V1LabelSelector(
match_labels=dict({'app': name})
)
)
# Deployment
deployment = client.V1Deployment(
api_version="apps/v1",
kind="Deployment",
metadata=client.V1ObjectMeta(name=name),
spec=spec)
# Creation of the Deployment in specified namespace
api_response = apps_v1_api.create_namespaced_deployment(
namespace=namespace, body=deployment
)
# logger.info('Deployment created:\n{}'.format(api_response))
except ApiException as e:
error_msg = str(e).strip()
logger.error(error_msg)
def create_service(core_v1_api, username):
name = 'jlab-{}'.format(username)
try:
body = client.V1Service(
api_version="v1",
kind="Service",
metadata=client.V1ObjectMeta(
name=name
),
spec=client.V1ServiceSpec(
selector={"app": name},
ports=[client.V1ServicePort(
port=8888,
target_port=8888
)]
)
)
# Creation of the Service in specified namespace
core_v1_api.create_namespaced_service(namespace=namespace, body=body)
except ApiException as e:
error_msg = str(e).strip()
logger.error(error_msg)
def create_ingress(networking_v1_beta1_api, username):
name = 'jlab-{}'.format(username)
class CustomIngressApi(object):
# https://stackoverflow.com/a/42788119
def __init__(self):
# config = client.Configuration()
# try:
# self.api_client = config.api_client
# except:
# self.api_client = client.ApiClient()
self.api_client = client.ApiClient(configuration=config.load_incluster_config())
# https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.21/#-strong-write-operations-ingress-v1-networking-k8s-io-strong-
def create_ingress(self, body, namespace='default'):
import json
resource_path = f'/apis/networking.k8s.io/v1/namespaces/{namespace}/ingresses'
header_params = {}
header_params['Accept'] = self.api_client.select_header_accept(['application/json'])
header_params['Content-Type'] = self.api_client.select_header_content_type(['*/*'])
token = ''
with open("/var/run/secrets/kubernetes.io/serviceaccount/token") as f:
token = f.read()
header_params['Authorization'] = f'Bearer {token}'
(resp, code, header) = self.api_client.call_api(
resource_path, 'POST', {'namespace': namespace}, {}, header_params, body, [], _preload_content=False)
return json.loads(resp.data.decode('utf-8'))
try:
body = {
'apiVersion': 'networking.k8s.io/v1',
'kind': 'Ingress',
'metadata': {
'name': name,
'annotations': {
'kubernetes.io/ingress.class': envvars.INGRESS_CLASS_JLAB_SERVER,
},
},
'spec': {
'rules': [
{
'host': envvars.BASE_DOMAIN,
'http': {
'paths': [
{
'path': f"{envvars.FRONTEND_BASE_PATH}/jlab/{username}",
'pathType': 'Prefix',
'backend': {
'service': {
'name': name,
'port': {
'number': 8888
}
}
}
}
]
}
}
]
}
}
# Creation of the Ingress in specified namespace
resp = CustomIngressApi().create_ingress(body=body, namespace=namespace)
logger.debug(f'k8s API (creating ingress) response:\n{resp}')
except ApiException as e:
error_msg = str(e).strip()
logger.error(error_msg)
def delete_deployment(api_instance, username):
name = 'jlab-{}'.format(username)
try:
api_response = api_instance.delete_namespaced_deployment(
name=name,
namespace=namespace,
body=client.V1DeleteOptions(
propagation_policy='Foreground',
grace_period_seconds=5))
# logger.info("Deployment deleted. status='%s'" % str(api_response.status))
except ApiException as e:
error_msg = str(e).strip()
logger.error(error_msg)
def delete_service(api_instance, username):
name = 'jlab-{}'.format(username)
try:
api_response = api_instance.delete_namespaced_service(
name=name,
namespace=namespace,
body={}
)
# logger.info("Service deleted. status='%s'" % str(api_response.status))
except ApiException as e:
error_msg = str(e).strip()
logger.error(error_msg)
def delete_ingress(api_instance, username):
name = 'jlab-{}'.format(username)
try:
api_response = api_instance.delete_namespaced_ingress(
name=name,
namespace=namespace,
body={}
)
# logger.info("Ingress deleted. status='%s'" % str(api_response.status))
except ApiException as e:
error_msg = str(e).strip()
logger.error(error_msg)
def delete_config_map(api_instance, username):
name = 'jlab-{}'.format(username)
try:
api_response = api_instance.delete_namespaced_config_map(
name=name,
namespace=namespace,
body={}
)
# logger.info("Ingress deleted. status='%s'" % str(api_response.status))
except ApiException as e:
error_msg = str(e).strip()
logger.error(error_msg)
def create_config_map(api_v1, username, base_path, token):
name = 'jlab-{}'.format(username)
try:
meta = client.V1ObjectMeta(
name=name,
namespace=namespace,
)
body = client.V1ConfigMap(
api_version="v1",
kind="ConfigMap",
metadata=meta,
data={
name: '''c.NotebookApp.token = u'{}'
c.NotebookApp.base_url = '{}'
'''.format(token, base_path)},
)
api_response = api_v1.create_namespaced_config_map(
namespace=namespace,
body=body
)
# logger.info("ConfigMap created")
except ApiException as e:
error_msg = str(e).strip()
logger.error(error_msg)
def delete(username):
error_msg = ''
try:
# sync_to_user_folder(username)
delete_config_map(api_v1, username)
delete_deployment(apps_v1_api, username)
delete_service(api_v1, username)
delete_ingress(networking_v1_beta1_api, username)
except Exception as e:
error_msg = str(e).strip()
logger.error(error_msg)
return error_msg
def deploy(username, base_path, token, gpu):
error_msg = ''
try:
create_config_map(api_v1, username, base_path, token)
create_deployment(apps_v1_api, username, token, gpu)
create_service(api_v1, username)
create_ingress(networking_v1_beta1_api, username)
except Exception as e:
error_msg = str(e).strip()
logger.error(error_msg)
return error_msg
def create(username, base_path, token, gpu):
# logger.info('Deleting existing Kubernetes resources...')
error_msg = delete(username)
if error_msg == '':
# logger.info('Deploying new Kubernetes resources...')
error_msg = deploy(username, base_path, token, gpu)
return error_msg
def status(username):
error_msg = ''
response = {
'unavailable_replicas': -1,
'ready_replicas': -1,
'token': '',
'creation_timestamp': '',
'latest_condition_type': 'Unknown',
}
name = 'jlab-{}'.format(username)
try:
api_response = apps_v1_api.read_namespaced_deployment_status(namespace=namespace,name=name)
response['ready_replicas'] = api_response.status.ready_replicas
response['unavailable_replicas'] = api_response.status.unavailable_replicas
last_transition_time = None
for condition in api_response.status.conditions:
if last_transition_time == None or condition.last_transition_time > last_transition_time:
last_transition_time = condition.last_transition_time
response['latest_condition_type'] = condition.type
response['latest_condition_type'] = api_response.status.conditions[0].type
response['creation_timestamp'] = api_response.metadata.creation_timestamp
api_response = api_v1.read_namespaced_config_map(namespace=namespace,name=name)
# Parse the ConfigMap based on its well-defined construction
# logger.info('Config map: {}'.format(api_response))
config_map = api_response.data
response['token'] = config_map[name].split("'")[1]
except ApiException as e:
response['latest_condition_type'] = ''
error_msg = str(e).strip()
logger.error(error_msg)
return response, error_msg
def prune(users, current_time):
error_msg = ''
pruned = []
for username in users:
name = 'jlab-{}'.format(username)
try:
api_response = api_v1.read_namespaced_config_map(namespace=namespace,name=name)
# Parse the ConfigMap based on its well-defined construction
config_map = api_response.data
token = config_map[name].split("'")[1]
# Get the list of running JupyterLab kernels using the JupyterLab API
r = requests.get(
'{}/jlab/{}/api/kernels'.format(envvars.FRONTEND_BASE_URL, username),
params={
'token': token
}
)
kernels = r.json()
# Iterate through the list of kernels and find the most recently active
last_activity = None
for k in kernels:
try:
last_activity_k = datetime.datetime.strptime(k['last_activity'], '%Y-%m-%dT%H:%M:%S.%fZ')
if not last_activity or last_activity_k > last_activity:
last_activity = last_activity_k
except Exception as e:
error_msg = str(e).strip()
logger.error(error_msg)
pass
# If an active kernel was discovered, delete the JupyterLab server if it has been idle over 24 hours
if last_activity:
# Delete the deployment if it has been idle over 24 hours
last_activity_tz = pytz.utc.localize(last_activity)
# logger.info('Seconds idle: {}'.format((current_time - last_activity_tz).seconds))
if (current_time - last_activity_tz).seconds > 24*60*60:
error_msg = delete(username)
pruned.append(username)
# If no active kernel was found, delete the JupyterLab deployment if it was created over 24 hours ago
else:
try:
api_response = apps_v1_api.read_namespaced_deployment_status(namespace=namespace,name=name)
creation_timestamp = api_response.metadata.creation_timestamp
current_time = pytz.utc.localize(current_time)
# Delete the deployment if it has been online over 24 hours
if (current_time - creation_timestamp).seconds > 24*60*60:
error_msg = delete(username)
pruned.append(username)
except ApiException as e:
error_msg = str(e).strip()
logger.error(error_msg)
except Exception as e:
error_msg = str(e).strip()
logger.error(error_msg)
return pruned, error_msg