Add annotation for PVC retention in generated charts

This commit is contained in:
Prathamesh Musale 2025-12-02 14:16:25 +05:30
parent f2c6fb28d1
commit 854e24e1c5
2 changed files with 53 additions and 4 deletions

View File

@ -70,6 +70,46 @@ def _wrap_job_templates_with_conditionals(chart_dir: Path, jobs: list) -> None:
print(f"Wrapped job template with conditional: {job_template_file.name}")
def _add_pvc_retention_policy(chart_dir: Path) -> None:
"""
Add helm.sh/resource-policy: keep annotation to PVC templates.
This ensures PVCs are not deleted when the Helm release is uninstalled,
preserving data for future deployments or job executions.
"""
templates_dir = chart_dir / "templates"
if not templates_dir.exists():
return
yaml = get_yaml()
# Find all PVC template files
pvc_files = list(templates_dir.glob("*-persistentvolumeclaim.yaml"))
for pvc_file in pvc_files:
try:
# Read the PVC template
content = yaml.load(open(pvc_file, "r"))
# Add the resource policy annotation
if "metadata" not in content:
content["metadata"] = {}
if "annotations" not in content["metadata"]:
content["metadata"]["annotations"] = {}
content["metadata"]["annotations"]["helm.sh/resource-policy"] = "keep"
# Write back
with open(pvc_file, "w") as f:
yaml.dump(content, f)
if opts.o.debug:
print(f"Added retention policy to: {pvc_file.name}")
except Exception as e:
if opts.o.debug:
print(f"Warning: Failed to add retention policy to {pvc_file.name}: {e}")
def _post_process_chart(chart_dir: Path, chart_name: str, jobs: list) -> None:
"""
Post-process Kompose-generated chart to fix common issues.
@ -77,6 +117,7 @@ def _post_process_chart(chart_dir: Path, chart_name: str, jobs: list) -> None:
Fixes:
1. Chart.yaml name, description and keywords
2. Add conditional wrappers to job templates (default: disabled)
3. Add resource retention policy to PVCs (prevent deletion on uninstall)
TODO:
- Add defaultMode: 0755 to ConfigMap volumes containing scripts (.sh files)
@ -105,6 +146,9 @@ def _post_process_chart(chart_dir: Path, chart_name: str, jobs: list) -> None:
if jobs:
_wrap_job_templates_with_conditionals(chart_dir, jobs)
# Add resource retention policy to PVCs
_add_pvc_retention_policy(chart_dir)
def generate_helm_chart(stack_path: str, spec_file: str, deployment_dir_path: Path) -> None:
"""

View File

@ -97,7 +97,8 @@ def run_helm_job(
try:
# Render job template with job enabled
# Use --set-json to properly handle job names with dashes
values_json = json.dumps({"jobs": {job_name: {"enabled": True}}})
jobs_dict = {job_name: {"enabled": True}}
values_json = json.dumps(jobs_dict)
helm_cmd = [
"helm", "template", release_name, str(chart_dir),
"--show-only", job_template_file,
@ -114,18 +115,22 @@ def run_helm_job(
if verbose:
print(f"Generated job manifest:\n{result.stdout}")
# Parse the manifest to get the actual job name
yaml = get_yaml()
manifest = yaml.load(result.stdout)
actual_job_name = manifest.get("metadata", {}).get("name", job_name)
# Apply the job manifest
kubectl_apply_cmd = ["kubectl", "apply", "-f", tmp_file.name, "-n", namespace]
subprocess.run(kubectl_apply_cmd, check=True, capture_output=True, text=True)
if verbose:
print(f"Job {job_name} created, waiting for completion...")
print(f"Job {actual_job_name} created, waiting for completion...")
# Wait for job completion
job_full_name = f"{release_name}-{job_name}"
wait_cmd = [
"kubectl", "wait", "--for=condition=complete",
f"job/{job_full_name}",
f"job/{actual_job_name}",
f"--timeout={timeout}s",
"-n", namespace
]