Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion backend/styrmin_backend/agent/tasks/orchestration.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,14 @@ async def run_job_command(
raise


# Kubernetes removes a finished ``job-run-*`` Job (and its pod) this long after
# it reaches Complete/Failed, since nothing in the codebase ever deletes it
# otherwise (#634). The exact retention duration is a product decision still
# open on the issue; this value is a placeholder pending that decision.
TASK_ID_LABEL = "styrmin.io/task-id"
_JOB_FINISHED_TTL_SECONDS = 3600


def _build_job_manifest(
*,
task_id: UUID,
Expand All @@ -526,8 +534,13 @@ def _build_job_manifest(
"metadata": {
"namespace": namespace,
"generateName": f"job-run-{task_id}-",
"labels": {
"app.kubernetes.io/managed-by": "styrmin",
TASK_ID_LABEL: str(task_id),
},
},
"spec": {
"ttlSecondsAfterFinished": _JOB_FINISHED_TTL_SECONDS,
"template": {
"spec": {
"parallelism": 1,
Expand Down Expand Up @@ -571,7 +584,7 @@ def _build_job_manifest(
}
],
}
}
},
},
}

Expand Down
23 changes: 22 additions & 1 deletion backend/tests/unit/agent/tasks/test_run_job_flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@ def _expected_manifest(task_id: UUID) -> dict[str, Any]:
"metadata": {
"namespace": "styrmin-app",
"generateName": f"job-run-{task_id}-",
"labels": {
"app.kubernetes.io/managed-by": "styrmin",
orchestration.TASK_ID_LABEL: str(task_id),
},
},
"spec": {
"ttlSecondsAfterFinished": orchestration._JOB_FINISHED_TTL_SECONDS,
"template": {
"spec": {
"parallelism": 1,
Expand Down Expand Up @@ -105,7 +110,7 @@ def _expected_manifest(task_id: UUID) -> dict[str, Any]:
}
],
}
}
},
},
}

Expand Down Expand Up @@ -348,3 +353,19 @@ async def test_run_job_command_without_runtime_class_keeps_the_default(
assert kubernetes_job.await_args is not None
manifest = kubernetes_job.await_args.kwargs["manifest"]
assert manifest["spec"]["template"]["spec"]["runtimeClassName"] is None


def test_build_job_manifest_is_findable_by_id_and_bounded_in_lifetime() -> None:
"""Without an id label and a ``ttlSecondsAfterFinished``, every ``runJob``
call permanently leaks one more Job (and its pod) in the namespace, since
nothing in the codebase ever deletes a ``job-run-*`` Job (#634). Per
ADR-073, the Job must also be selectable by a Styrmin record id label, not
only by its ``generateName`` prefix."""
task_id = uuid4()

manifest = orchestration._build_job_manifest(**_job(task_id))

labels = manifest["metadata"].get("labels") or {}
assert str(task_id) in labels.values()
ttl = manifest["spec"].get("ttlSecondsAfterFinished")
assert isinstance(ttl, int) and ttl > 0
1 change: 1 addition & 0 deletions changelog/634.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Gave the Job that runJob creates an id label and a bounded lifetime (ttlSecondsAfterFinished), so Kubernetes garbage-collects it instead of leaking a Job and its pod in the deployment namespace on every call
Loading