-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjob_creator.py
More file actions
488 lines (441 loc) · 19.9 KB
/
Copy pathjob_creator.py
File metadata and controls
488 lines (441 loc) · 19.9 KB
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
"""
Communicate to a kubernetes API to spawn a pod with the metadata passed by message to the RunMaker
"""
from typing import Any
from kubernetes import client # type: ignore[import-untyped]
from jobcreator.utils import load_kubernetes_config, logger
def _setup_smb_pv(pv_name: str, secret_name: str, secret_namespace: str, source: str, mount_options: list[str]) -> None:
"""
Sets up an smb PV using the loaded kubeconfig as a destination
:param pv_name: str, The name given to the smb-pv when it's made
:param secret_name: str, The name of the secret that contains the credentials for the smb share
:param secret_namespace: str, the namespace of the secret
:param source: str, The IP/url/uri that is used to mount the smb share
:param mount_options: list, The mount options for the smb share
:return: str, the name of the archive PV
"""
metadata = client.V1ObjectMeta(name=pv_name, annotations={"pv.kubernetes.io/provisioned-by": "smb.csi.k8s.io"})
secret_ref = client.V1SecretReference(name=secret_name, namespace=secret_namespace)
csi = client.V1CSIPersistentVolumeSource(
driver="smb.csi.k8s.io",
read_only=True,
volume_handle=pv_name,
volume_attributes={"source": source},
node_stage_secret_ref=secret_ref,
)
spec = client.V1PersistentVolumeSpec(
capacity={"storage": "1000Gi"},
access_modes=["ReadOnlyMany"],
persistent_volume_reclaim_policy="Retain",
mount_options=mount_options,
csi=csi,
)
archive_pv = client.V1PersistentVolume(api_version="v1", kind="PersistentVolume", metadata=metadata, spec=spec)
client.CoreV1Api().create_persistent_volume(archive_pv)
def _setup_pvc(pvc_name: str, pv_name: str, namespace: str, access_mode: str = "ReadOnlyMany") -> None:
"""
Set up a PVC for the given pvc_name and pv_name in the given namespace
:param pvc_name: str, The name of the pvc to make
:param pv_name: str, The name of the pv to be claimed
:param namespace: str, The namespace to create the pvc in
"""
metadata = client.V1ObjectMeta(name=pvc_name)
resources = client.V1ResourceRequirements(requests={"storage": "1000Gi"})
spec = client.V1PersistentVolumeClaimSpec(
access_modes=[access_mode],
resources=resources,
volume_name=pv_name,
storage_class_name="",
)
archive_pvc = client.V1PersistentVolumeClaim(
api_version="v1",
kind="PersistentVolumeClaim",
metadata=metadata,
spec=spec,
)
client.CoreV1Api().create_namespaced_persistent_volume_claim(namespace=namespace, body=archive_pvc)
def _setup_extras_pvc(job_name: str, job_namespace: str, pv_name: str) -> str:
"""
Sets up the extras Manila PVC using the loaded kubeconfig as a destination
:param job_name: str, the name of the job that the PVC is made for
:param job_namespace: str, the namespace that the job is in
:param pv_name: str, the name of the PV the PVC is being made for
:return: str, the name of the PVC
"""
pvc_name = f"{job_name}-extras-pvc"
metadata = client.V1ObjectMeta(name=pvc_name)
resources = client.V1ResourceRequirements(requests={"storage": "1000Gi"})
match_expression = client.V1LabelSelectorRequirement(key="name", operator="In", values=[pv_name])
selector = client.V1LabelSelector(match_expressions=[match_expression])
spec = client.V1PersistentVolumeClaimSpec(
access_modes=["ReadOnlyMany"],
resources=resources,
selector=selector,
storage_class_name="",
)
extras_pvc = client.V1PersistentVolumeClaim(
api_version="v1",
kind="PersistentVolumeClaim",
metadata=metadata,
spec=spec,
)
client.CoreV1Api().create_namespaced_persistent_volume_claim(namespace=job_namespace, body=extras_pvc)
return pvc_name
def _setup_extras_pv(job_name: str, secret_namespace: str, manila_share_id: str, manila_share_access_id: str) -> str:
"""
Setups up the extras PV using the loaded kubeconfig as destination
:param job_name: str, the name of the job the PV is for
:param manila_share_id: The id of the manila share to mount for extras
:param manila_share_access_id: the id of the access rule for the manila share that provides access to the
manila share
:param secret_namespace: the namespace where the manila-creds secret is.
:return: str, the name of the PV
"""
pv_name = f"{job_name}-extras-pv"
metadata = client.V1ObjectMeta(name=pv_name, labels={"name": pv_name})
secret_ref = client.V1SecretReference(name="manila-creds", namespace=secret_namespace)
csi = client.V1CSIPersistentVolumeSource(
driver="cephfs.manila.csi.openstack.org",
read_only=True,
volume_handle=pv_name,
volume_attributes={"shareID": manila_share_id, "shareAccessID": manila_share_access_id},
node_stage_secret_ref=secret_ref,
node_publish_secret_ref=secret_ref,
)
spec = client.V1PersistentVolumeSpec(
capacity={"storage": "1000Gi"},
access_modes=["ReadOnlyMany"],
csi=csi,
)
archive_pv = client.V1PersistentVolume(api_version="v1", kind="PersistentVolume", metadata=metadata, spec=spec)
client.CoreV1Api().create_persistent_volume(archive_pv)
return pv_name
def _setup_ceph_pv(
pv_name: str,
ceph_creds_k8s_secret_name: str,
ceph_creds_k8s_namespace: str,
cluster_id: str,
fs_name: str,
ceph_mount_path: str,
) -> str:
"""
Sets up the ceph deneb PV using the loaded kubeconfig as a destination
:param pv_name: str, the name of the PV
:return: str, the name of the ceph deneb PV
"""
metadata = client.V1ObjectMeta(name=pv_name)
secret_ref = client.V1SecretReference(name=ceph_creds_k8s_secret_name, namespace=ceph_creds_k8s_namespace)
csi = client.V1CSIPersistentVolumeSource(
driver="cephfs.csi.ceph.com",
node_stage_secret_ref=secret_ref,
volume_handle=pv_name,
volume_attributes={
"clusterID": cluster_id,
"mounter": "fuse",
"fsName": fs_name,
"staticVolume": "true",
"rootPath": ceph_mount_path,
},
)
spec = client.V1PersistentVolumeSpec(
capacity={"storage": "1000Gi"},
storage_class_name="",
access_modes=["ReadWriteMany"],
persistent_volume_reclaim_policy="Retain",
volume_mode="Filesystem",
csi=csi,
)
ceph_pv = client.V1PersistentVolume(api_version="v1", kind="PersistentVolume", metadata=metadata, spec=spec)
client.CoreV1Api().create_persistent_volume(ceph_pv)
return pv_name
def _setup_imat_pv_and_pvcs(job_name: str, namespace: str, pv_names: list[str], pvc_names: list[str]) -> None:
imat_pv_name = f"{job_name}-ndximat-pv-smb"
imat_pvc_name = f"{job_name}-ndximat-pvc"
_setup_smb_pv(imat_pv_name, "imat-creds", namespace, "//NDXIMAT.isis.cclrc.ac.uk/data$/", [])
_setup_pvc(imat_pvc_name, imat_pv_name, namespace)
pv_names.append(imat_pv_name)
pvc_names.append(imat_pvc_name)
def _generate_tolerations_from_taints(taints: list[dict[str, Any]]) -> list[client.V1Toleration]:
tolerations = []
for taint in taints:
toleration = client.V1Toleration(
value=taint.get("value", None),
key=taint.get("key", None),
operator=taint.get("operator", None),
effect=taint.get("effect", None),
)
tolerations.append(toleration)
return tolerations
def _generate_affinities(node_affinity_dict: dict[str, Any] | None = None) -> client.V1Affinity:
# Add the anti-affinity that we always use
pod_affinity_label_selector = client.V1LabelSelector(
match_labels={"reduce.isis.cclrc.ac.uk/job-source": "automated-reduction"},
)
pod_affinity_term = client.V1PodAffinityTerm(
topology_key="kubernetes.io/hostname",
label_selector=pod_affinity_label_selector,
)
weighted_pod_affinity = client.V1WeightedPodAffinityTerm(weight=100, pod_affinity_term=pod_affinity_term)
anti_affinity = client.V1PodAntiAffinity(
preferred_during_scheduling_ignored_during_execution=[weighted_pod_affinity],
)
# Create new node affinities based on the list
if node_affinity_dict is not None and node_affinity_dict != {}:
expected_keys = ["key", "operator", "values"]
for expected_key in expected_keys:
if expected_key not in node_affinity_dict:
logger.error(
"Expected key for node affinity not found: %s. Spawning without node affinity.", expected_key
)
return client.V1Affinity(pod_anti_affinity=anti_affinity)
node_affinity = client.V1NodeAffinity(
required_during_scheduling_ignored_during_execution=client.V1NodeSelector(
node_selector_terms=[
client.V1NodeSelectorTerm(
match_expressions=[
client.V1NodeSelectorRequirement(
key=node_affinity_dict["key"],
operator=node_affinity_dict["operator"],
values=node_affinity_dict["values"],
)
]
)
]
)
)
return client.V1Affinity(pod_anti_affinity=anti_affinity, node_affinity=node_affinity)
return client.V1Affinity(pod_anti_affinity=anti_affinity)
class JobCreator:
"""
This class is responsible for loading the kubernetes config and handling methods for creating new pods.
"""
def __init__(self, watcher_sha: str, dev_mode: bool) -> None:
"""
Takes the runner_sha and ensures that the kubernetes config is loaded before continuing.
:param watcher_sha: str, The sha256 used for the watcher, often made by the watcher.D file in this repo's
container folder
:param dev_mode: bool, Whether the jobwatcher is launched in development mode
:return: None
"""
load_kubernetes_config()
self.watcher_sha = watcher_sha
self.dev_mode = dev_mode
def spawn_job( # noqa: PLR0913
self,
job_name: str,
script: str,
job_namespace: str,
ceph_creds_k8s_secret_name: str,
ceph_creds_k8s_namespace: str,
cluster_id: str,
fs_name: str,
ceph_mount_path: str,
job_id: int,
max_time_to_complete_job: int,
fia_api_host: str,
fia_api_api_key: str,
runner_image: str,
manila_share_id: str,
manila_share_access_id: str,
special_pvs: list[str],
taints: list[dict[str, Any]],
affinity: dict[str, Any] | None,
queue_host: str,
queue_user: str,
queue_password: str,
failure_queue_name: str,
filepath: str | None = None,
) -> None:
"""
Takes the meta_data from the message and uses that dictionary for generating the deployment of the pod.
:param job_name: The name that the job should be created as
:param script: The script that should be executed
:param job_namespace: The namespace that the job should be created in
:param ceph_creds_k8s_secret_name: The secret name of the ceph credentials
:param ceph_creds_k8s_namespace: The secret namespace of the ceph credentials
:param cluster_id: The cluster id for the ceph cluster to connect to
:param fs_name: The file system name for the ceph cluster
:param ceph_mount_path: the path on the ceph cluster to mount
:param job_id: The id used in the DB for the reduction
:param max_time_to_complete_job: The maximum time to allow for completion of a job in seconds
:param fia_api_host: The fia api host for the fia cluster
:param fia_api_api_key: The fia api key
:param runner_image: the container image that has is to be used the containers have permission to use the
directories required for outputting data.
:param manila_share_id: The id of the manila share to mount for extras
:param manila_share_access_id: the id of the access rule for the manila share that provides access to the
manila share
:param special_pvs: A list of special PV strings, that represent PVs that can be implemented.
:param taints: A list of taints that the runner pods should have for example:
[{"key": "gpu", "effect": "NoSchedule", "operator": "Exists"}]
:param affinity: A dict that describes the node affinity of the job for example:
{"key": "node-type", "operator": "In", "values": ["gpu-worker"]}
:return: None
"""
logger.info("Creating PV and PVC for: %s", job_name)
pv_names = []
pvc_names = []
# Setup Archive PV and PVC
archive_pv_name = f"{job_name}-archive-pv-smb"
_setup_smb_pv(
archive_pv_name,
"archive-creds",
job_namespace,
"//isisdatar55.isis.cclrc.ac.uk/inst$/",
["noserverino", "_netdev", "vers=2.1"],
)
pv_names.append(archive_pv_name)
archive_pvc_name = f"{job_name}-archive-pvc"
_setup_pvc(archive_pvc_name, archive_pv_name, job_namespace)
pvc_names.append(archive_pvc_name)
# Setup Extras PV and PVC
extras_pv_name = _setup_extras_pv(
job_name=job_name,
secret_namespace=job_namespace,
manila_share_id=manila_share_id,
manila_share_access_id=manila_share_access_id,
)
pv_names.append(extras_pv_name)
extras_pvc_name = f"{job_name}-extras-pvc"
_setup_pvc(extras_pvc_name, extras_pv_name, job_namespace)
pvc_names.append(extras_pvc_name)
# Setup ceph PV and PVC
if not self.dev_mode:
ceph_pv_name = f"{job_name}-ceph-pv"
(
_setup_ceph_pv(
ceph_pv_name,
ceph_creds_k8s_secret_name,
ceph_creds_k8s_namespace,
cluster_id,
fs_name,
ceph_mount_path,
),
)
pv_names.append(ceph_pv_name)
ceph_pvc_name = f"{job_name}-ceph-pvc"
_setup_pvc(ceph_pvc_name, ceph_pv_name, job_namespace, access_mode="ReadWriteMany")
pvc_names.append(ceph_pvc_name)
ceph_volume = client.V1Volume(
name="ceph-mount",
persistent_volume_claim=client.V1PersistentVolumeClaimVolumeSource(
claim_name=ceph_pvc_name,
read_only=False,
),
)
else:
ceph_volume = client.V1Volume(
name="ceph-mount",
empty_dir=client.V1EmptyDirVolumeSource(size_limit="100Gi"),
)
# Create the Job
logger.info("Spawning job: %s", job_name)
volumes = [
client.V1Volume(
name="archive-mount",
persistent_volume_claim=client.V1PersistentVolumeClaimVolumeSource(
claim_name=archive_pvc_name,
read_only=True,
),
),
ceph_volume,
client.V1Volume(
name="extras-mount",
persistent_volume_claim=client.V1PersistentVolumeClaimVolumeSource(
claim_name=extras_pvc_name,
read_only=True,
),
),
]
volumes_mounts = [
client.V1VolumeMount(name="archive-mount", mount_path="/archive"),
client.V1VolumeMount(name="ceph-mount", mount_path="/output"),
client.V1VolumeMount(name="extras-mount", mount_path="/extras"),
]
# Setup special PVs and add them to the volume mounts
if "imat" in special_pvs:
_setup_imat_pv_and_pvcs(job_name, job_namespace, pv_names, pvc_names)
imat_pvc_source = client.V1PersistentVolumeClaimVolumeSource(
claim_name=f"{job_name}-ndximat-pvc", read_only=True
)
volumes.append(client.V1Volume(name="imat-mount", persistent_volume_claim=imat_pvc_source))
volumes_mounts.append(client.V1VolumeMount(name="imat-mount", mount_path="/imat"))
# Because imat is special and uses mantid imaging to load large .tiff files, we need to ensure the /dev/shm
# is larger than 64mb. We do however have a soft-ish limit of around 32GiB on the size of datasets when
# doing this.
volumes.append(
client.V1Volume(
name="dev-shm", empty_dir=client.V1EmptyDirVolumeSource(size_limit="32Gi", medium="Memory")
)
)
volumes_mounts.append(client.V1VolumeMount(name="dev-shm", mount_path="/dev/shm")) # noqa: S108
# Decide whether this is a GPU workload. IMAT jobs run mantid imaging on GPU nodes and need
# the NVIDIA runtime + a GPU resource request so the GPU Operator injects the matching
# userspace driver libraries (libcuda.so.*) into the container.
gpu_job = "imat" in special_pvs
main_container = client.V1Container(
name=job_name,
image=runner_image,
args=[script],
env=[client.V1EnvVar(name="PYTHONUNBUFFERED", value="1")],
volume_mounts=volumes_mounts,
resources=client.V1ResourceRequirements(
limits={"nvidia.com/gpu": "1"},
)
if gpu_job
else None,
)
watcher_container = client.V1Container(
name="job-watcher",
image=f"ghcr.io/fiaisis/jobwatcher@sha256:{self.watcher_sha}",
env=[
client.V1EnvVar(name="FIA_API_HOST", value=fia_api_host),
client.V1EnvVar(name="FIA_API_API_KEY", value=fia_api_api_key),
client.V1EnvVar(name="MAX_TIME_TO_COMPLETE_JOB", value=str(max_time_to_complete_job)),
client.V1EnvVar(name="CONTAINER_NAME", value=job_name),
client.V1EnvVar(name="JOB_NAME", value=job_name),
client.V1EnvVar(name="POD_NAME", value=job_name),
client.V1EnvVar(name="QUEUE_HOST", value=queue_host),
client.V1EnvVar(name="QUEUE_USER", value=queue_user),
client.V1EnvVar(name="QUEUE_PASSWORD", value=queue_password),
client.V1EnvVar(name="FAILURE_QUEUE_NAME", value=failure_queue_name),
],
)
affinity = _generate_affinities(node_affinity_dict=affinity)
tolerations = _generate_tolerations_from_taints(taints)
pod_spec = client.V1PodSpec(
affinity=affinity,
service_account_name="jobwatcher",
containers=[main_container, watcher_container],
restart_policy="Never",
tolerations=tolerations,
volumes=volumes,
runtime_class_name="nvidia" if gpu_job else None,
)
pod_metadata = client.V1ObjectMeta(
labels={"reduce.isis.cclrc.ac.uk/job-source": "automated-reduction"},
)
template = client.V1PodTemplateSpec(spec=pod_spec, metadata=pod_metadata)
spec = client.V1JobSpec(
template=template,
backoff_limit=0,
ttl_seconds_after_finished=21600, # 6 hours
)
job_metadata = client.V1ObjectMeta(
name=job_name,
annotations={
"job-id": str(job_id),
"pvs": str(pv_names),
"pvcs": str(pvc_names),
"kubectl.kubernetes.io/default-container": main_container.name,
},
)
if filepath:
job_metadata.annotations["filepath"] = filepath
job = client.V1Job(
api_version="batch/v1",
kind="Job",
metadata=job_metadata,
spec=spec,
)
client.BatchV1Api().create_namespaced_job(namespace=job_namespace, body=job)