Skip to content

Commit 8f0bde7

Browse files
committed
feat(utils): add central function for Dask K8s component names (#478)
Closes reanahub/reana#841
1 parent dcb9749 commit 8f0bde7

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

reana_commons/utils.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,32 @@ def build_unique_component_name(component_type, id=None):
414414
)
415415

416416

417+
def get_trimmed_workflow_id(workflow_id: str, trim_level: int) -> str:
418+
"""Trim the given workflow id and return it. For a workflow with id '9eef9a08-5629-420d-8e97-29d498d88e20' with trim level 4, it returns '9eef9a08'."""
419+
return str(workflow_id).rsplit("-", trim_level)[0]
420+
421+
422+
def get_dask_component_name(
423+
workflow_id: str, name_type: str, dashboard_service_namespace: str = "default"
424+
) -> str:
425+
"""Generate the name of a Dask-related component based on the workflow ID and name type. Note that we trim the end of the uuid so uniqueness is not 100% guaranteed."""
426+
trimmed_workflow_id = get_trimmed_workflow_id(str(workflow_id), 4)
427+
name_map = {
428+
"cluster": f"reana-run-dask-{trimmed_workflow_id}",
429+
"autoscaler": f"dask-autoscaler-{trimmed_workflow_id}",
430+
"dashboard_ingress": f"dask-dashboard-ingress-{trimmed_workflow_id}",
431+
"dashboard_service": f"reana-run-dask-{trimmed_workflow_id}-scheduler",
432+
"dashboard_service_uri": f"reana-run-dask-{trimmed_workflow_id}-scheduler.{dashboard_service_namespace}.svc.cluster.local:8786",
433+
"dashboard_ingress_middleware": f"dask-dashboard-ingress-{trimmed_workflow_id}-replacepath",
434+
"database_model_service": f"dask-service-{trimmed_workflow_id}",
435+
}
436+
if name_type not in name_map:
437+
raise ValueError(
438+
f"Invalid name type: '{name_type}'. Valid types are: {', '.join(name_map.keys())}."
439+
)
440+
return name_map[name_type]
441+
442+
417443
def get_usage_percentage(usage: int, limit: int) -> str:
418444
"""Usage percentage."""
419445
if limit == 0:

tests/test_utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
click_table_printer,
2525
format_cmd,
2626
get_workflow_status_change_verb,
27+
get_trimmed_workflow_id,
2728
)
2829

2930

@@ -150,3 +151,22 @@ def test_get_workflow_status_change_verb_invalid():
150151
"""Test get_workflow_status_change_verb with an invalid status."""
151152
with pytest.raises(ValueError, match="invalid"):
152153
get_workflow_status_change_verb("invalid")
154+
155+
156+
@pytest.mark.parametrize(
157+
"workflow_id, trim_level, expected",
158+
[
159+
("9eef9a08-5629-420d-8e97-29d498d88e20", 4, "9eef9a08"),
160+
("9eef9a08-5629-420d-8e97-29d498d88e20", 3, "9eef9a08-5629"),
161+
("9eef9a08-5629-420d-8e97-29d498d88e20", 2, "9eef9a08-5629-420d"),
162+
("9eef9a08-5629-420d-8e97-29d498d88e20", 1, "9eef9a08-5629-420d-8e97"),
163+
(
164+
"9eef9a08-5629-420d-8e97-29d498d88e20",
165+
0,
166+
"9eef9a08-5629-420d-8e97-29d498d88e20",
167+
),
168+
],
169+
)
170+
def test_get_trimmed_workflow_id(workflow_id, trim_level, expected):
171+
"""Test get_trimmed_workflow_id function with several different inputs."""
172+
assert get_trimmed_workflow_id(workflow_id, trim_level) == expected

0 commit comments

Comments
 (0)