refactor(k8s): decompose kubernetes.py into focused mixin modules#120
Merged
Conversation
Split the 3,535-line kubernetes.py into 6 focused files using the mixin pattern (same approach as existing KubernetesLauncherMixin): - kubernetes.py (779 lines): core lifecycle (init, validate, prepare, deploy, monitor, cleanup) - k8s_results.py (1,390 lines): results collection, PVC artifacts, perf CSV reporting - k8s_template_context.py (856 lines): Jinja2 context, env vars, data config, tools - k8s_scripts.py (352 lines): script/tool loading for ConfigMap embedding - k8s_pvc.py (239 lines): PVC lifecycle management All 426 unit tests and 139 integration tests pass unchanged. Co-Authored-By: Claude Opus 4 <noreply@anthropic.com>
Remove unused imports (re, sanitize_k8s_container_name), fix blank lines before nested class methods, and strip trailing whitespace. Co-Authored-By: Claude Opus 4 <noreply@anthropic.com>
coketaste
requested review from
Cemberk,
Rohan138,
gargrahul and
leconcio
as code owners
May 1, 2026 21:34
There was a problem hiding this comment.
Pull request overview
Refactors the Kubernetes deployment implementation by decomposing the previously monolithic kubernetes.py into focused mixin modules, keeping the public deployment API intact while improving navigability and separation of concerns.
Changes:
- Split Kubernetes deployment responsibilities into new mixins for PVC lifecycle, script/tool bundling, template context building, and results collection/reporting.
- Updated
KubernetesDeploymentto compose these mixins while leaving core lifecycle methods inkubernetes.py. - Reduced
kubernetes.pyto primarily orchestration logic (init/validate/prepare/deploy/monitor/cleanup).
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
src/madengine/deployment/kubernetes.py |
Switches KubernetesDeployment to a mixin-based composition and removes inlined implementations moved to mixin modules. |
src/madengine/deployment/k8s_template_context.py |
New mixin for building the Jinja2 template context, env var merging, data config, and tools config enrichment. |
src/madengine/deployment/k8s_scripts.py |
New mixin for bundling common scripts/tool wrappers (and Primus overlays) into ConfigMaps. |
src/madengine/deployment/k8s_results.py |
New mixin for pod log/PVC artifact collection, performance parsing/aggregation, and perf reporting outputs. |
src/madengine/deployment/k8s_pvc.py |
New mixin for results/data PVC storage class resolution and PVC create/delete workflows. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Promote Unreleased to [2.0.3], add K8s mixin refactor entry, and document known issue where a node is reported FAILED due to kubelet log collection errors despite the pod succeeding. Add corresponding troubleshooting section to deployment.md. Co-Authored-By: Claude Opus 4 <noreply@anthropic.com>
Cleanup used the full job name while creation truncated to 15 chars, so stale collector pods were never deleted for jobs with names longer than 15 characters. Extract a shared collector_pod_name() helper in k8s_results.py and use it in both sites. Co-Authored-By: Claude Opus 4 <noreply@anthropic.com>
Comment on lines
+807
to
+811
| # Import Context and create minimal instance | ||
| # Data provider needs this to function | ||
| self.context_for_data = type('obj', (object,), { | ||
| 'ctx': {}, | ||
| 'sh': lambda cmd: os.popen(cmd).read().strip() |
Comment on lines
+672
to
+676
| pod_names: Full Kubernetes pod names for this job (ordered) | ||
| """ | ||
| from .kubernetes import assign_pvc_subdirs_to_pods | ||
|
|
||
| pvc_name = f"{deployment_id}-results" |
Comment on lines
+412
to
+420
| To verify: | ||
| ```bash | ||
| # Check actual pod status — if Succeeded, the workload ran fine | ||
| kubectl describe pod <pod-name> | grep Status | ||
|
|
||
| # Check the node's kubelet health | ||
| kubectl get nodes | ||
| kubectl describe node <node-name> | grep -A5 Conditions | ||
| ``` |
Comment on lines
12
to
+18
| - **Profiling**: `rocm_trace_lite` now sets `RTL_MODE=lite` explicitly; added tool `rocm_trace_lite_default` with `RTL_MODE=default` for A/B overhead comparison. `rtl_trace_wrapper.sh` passes `rtl trace --mode …` when `RTL_MODE` is set. | ||
|
|
||
| - **Kubernetes deployment refactor**: Decomposed the monolithic `kubernetes.py` (~2800 lines) into focused mixin modules — `k8s_pvc.py` (PVC lifecycle), `k8s_results.py` (log/artifact collection and performance aggregation), `k8s_scripts.py` (script extraction and ConfigMap building), and `k8s_template_context.py` (Jinja2 template context assembly). `KubernetesDeployment` now composes these mixins; no functional changes. | ||
|
|
||
| ### Fixed | ||
|
|
||
| - **K8s collector pod name mismatch**: The cleanup code in `kubernetes.py` used the full job name (`collector-{job_name}`) while the creation code in `k8s_results.py` truncated it (`collector-{deployment_id[:15]}`). For any job name longer than 15 characters (i.e. virtually all real jobs), cleanup would fail to delete the collector pod, leaving it running and potentially blocking PVC deletion on the next deploy. Extracted a shared `collector_pod_name()` helper so both sites use the same truncated name. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Motivation
kubernetes.py had grown to 3,535 lines with 40+ methods spanning 5+ distinct responsibilities, making it difficult to navigate and review. The largest single method
(_prepare_template_context) was 538 lines.
New modules
MRO
KubernetesDeployment → KubernetesLauncherMixin → KubernetesResultsMixin
→ KubernetesTemplateContextMixin → KubernetesScriptsMixin
→ KubernetesPVCMixin → BaseDeployment → ABC → object
Test plan