Skip to content

refactor(k8s): decompose kubernetes.py into focused mixin modules#120

Open
coketaste wants to merge 2 commits intodevelopfrom
coketaste/v2-k8s-refactor
Open

refactor(k8s): decompose kubernetes.py into focused mixin modules#120
coketaste wants to merge 2 commits intodevelopfrom
coketaste/v2-k8s-refactor

Conversation

@coketaste
Copy link
Copy Markdown
Collaborator

Summary

  • Decompose kubernetes.py (3,535 lines) into 4 focused mixin modules using the same pattern as the existing KubernetesLauncherMixin, reducing the main file from 3,535 to 778 lines
  • No behavioral changes — pure structural refactoring with all 47 original methods preserved

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

┌───────────────────────────┬───────┬───────────────────────────────────────────────────────────────────┐
│          Module           │ Lines │                          Responsibility                           │
├───────────────────────────┼───────┼───────────────────────────────────────────────────────────────────┤
│ k8s_pvc.py                │   239 │ PVC lifecycle — create, delete, storage class resolution          │
├───────────────────────────┼───────┼───────────────────────────────────────────────────────────────────┤
│ k8s_scripts.py            │   352 │ Script/tool loading for ConfigMap embedding                       │
├───────────────────────────┼───────┼───────────────────────────────────────────────────────────────────┤
│ k8s_template_context.py   │   858 │ Jinja2 template context preparation, env vars, tools config       │
├───────────────────────────┼───────┼───────────────────────────────────────────────────────────────────┤
│ k8s_results.py            │ 1,389 │ Result collection, CSV parsing, perf record building              │
├───────────────────────────┼───────┼───────────────────────────────────────────────────────────────────┤
│ kubernetes.py (remaining) │   778 │ Core lifecycle: init, validate, prepare, deploy, monitor, cleanup │
└───────────────────────────┴───────┴───────────────────────────────────────────────────────────────────┘

MRO

KubernetesDeployment → KubernetesLauncherMixin → KubernetesResultsMixin
→ KubernetesTemplateContextMixin → KubernetesScriptsMixin
→ KubernetesPVCMixin → BaseDeployment → ABC → object

Test plan

  • All 426 unit tests pass (including 24 K8s-specific)
  • All 139 integration tests pass
  • Python syntax validated for all 5 files
  • Import chain and MRO verified
  • Zero methods lost or duplicated (47/47 accounted for)
  • Deployment public API (DeploymentFactory, KubernetesDeployment) unchanged
  • Flake8 clean (no new lint issues introduced)

coketaste and others added 2 commits May 1, 2026 20:05
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 coketaste self-assigned this May 1, 2026
Copilot AI review requested due to automatic review settings May 1, 2026 21:34
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 KubernetesDeployment to compose these mixins while leaving core lifecycle methods in kubernetes.py.
  • Reduced kubernetes.py to 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.

Comment on lines 386 to 388
# Delete existing collector pod (must be done before PVC to allow PVC deletion)
collector_pod_name = f"collector-{self.job_name}"
try:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants