diff --git a/stack_orchestrator/deploy/k8s/helm/chart_generator.py b/stack_orchestrator/deploy/k8s/helm/chart_generator.py index 01976ea3..5964d755 100644 --- a/stack_orchestrator/deploy/k8s/helm/chart_generator.py +++ b/stack_orchestrator/deploy/k8s/helm/chart_generator.py @@ -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: """ diff --git a/stack_orchestrator/deploy/k8s/helm/job_runner.py b/stack_orchestrator/deploy/k8s/helm/job_runner.py index 7a8e3726..05637c54 100644 --- a/stack_orchestrator/deploy/k8s/helm/job_runner.py +++ b/stack_orchestrator/deploy/k8s/helm/job_runner.py @@ -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 ]