2222
2323
2424def run_command (
25- cmd : List [str ], capture_output : bool = True
25+ cmd : List [str ], capture_output : bool = True , exit_on_error : bool = True
2626) -> subprocess .CompletedProcess :
2727 """Run a command and handle errors."""
2828 try :
@@ -37,7 +37,10 @@ def run_command(
3737 print (f"STDOUT: { e .stdout } " )
3838 if e .stderr :
3939 print (f"STDERR: { e .stderr } " )
40- sys .exit (1 )
40+ if exit_on_error :
41+ sys .exit (1 )
42+ else :
43+ raise
4144
4245
4346def check_kubectl_access (namespace : str ) -> None :
@@ -47,6 +50,55 @@ def check_kubectl_access(namespace: str) -> None:
4750 print ("✓ kubectl access confirmed" )
4851
4952
53+ def ensure_clean_access_pod (namespace : str ) -> str :
54+ """Ensure a clean PVC access pod deployment by deleting any existing pod first."""
55+
56+ # Check if pod exists and delete it if it does
57+ try :
58+ result = subprocess .run (
59+ [
60+ "kubectl" ,
61+ "get" ,
62+ "pod" ,
63+ PVC_ACCESS_POD_NAME ,
64+ "-n" ,
65+ namespace ,
66+ "-o" ,
67+ "jsonpath={.metadata.name}" ,
68+ ],
69+ capture_output = True ,
70+ text = True ,
71+ check = False ,
72+ )
73+ if result .returncode == 0 and result .stdout .strip () == PVC_ACCESS_POD_NAME :
74+ print (f"Found existing access pod '{ PVC_ACCESS_POD_NAME } ', deleting it..." )
75+ run_command (
76+ [
77+ "kubectl" ,
78+ "delete" ,
79+ "pod" ,
80+ PVC_ACCESS_POD_NAME ,
81+ "-n" ,
82+ namespace ,
83+ "--ignore-not-found" ,
84+ ],
85+ capture_output = False ,
86+ exit_on_error = False ,
87+ )
88+ print ("✓ Existing access pod deleted" )
89+ except Exception :
90+ pass # Pod doesn't exist, which is fine
91+
92+ try :
93+ return deploy_access_pod (namespace )
94+ except Exception as e :
95+ print (f"Deployment failed: { e } " )
96+ print (
97+ "Pod left running for debugging. Use 'kubectl delete pod pvc-access-pod -n <namespace>' to clean up manually."
98+ )
99+ raise
100+
101+
50102def deploy_access_pod (namespace : str ) -> str :
51103 """Deploy the PVC access pod and return pod name."""
52104
@@ -67,25 +119,19 @@ def deploy_access_pod(namespace: str) -> str:
67119 text = True ,
68120 check = False ,
69121 )
70-
71122 if result .returncode == 0 and result .stdout .strip () == "Running" :
72123 print (f"✓ Access pod '{ PVC_ACCESS_POD_NAME } ' already running" )
73124 return PVC_ACCESS_POD_NAME
74125 except Exception :
75- # Pod doesn't exist or isn't running
76- pass
126+ pass # Pod doesn't exist or isn't running
77127
78128 print (f"Deploying access pod '{ PVC_ACCESS_POD_NAME } ' in namespace '{ namespace } '..." )
79129
80- # Get the directory where this script is located
81- script_dir = Path (__file__ ).parent
82- pod_yaml_path = script_dir / "manifests" / "pvc-access-pod.yaml"
83-
130+ pod_yaml_path = Path (__file__ ).parent / "manifests" / "pvc-access-pod.yaml"
84131 if not pod_yaml_path .exists ():
85132 print (f"ERROR: Pod YAML not found at { pod_yaml_path } " )
86133 sys .exit (1 )
87134
88- # Deploy the pod
89135 run_command (
90136 ["kubectl" , "apply" , "-f" , str (pod_yaml_path ), "-n" , namespace ],
91137 capture_output = False ,
@@ -103,23 +149,28 @@ def deploy_access_pod(namespace: str) -> str:
103149 "--timeout=60s" ,
104150 ],
105151 capture_output = False ,
152+ exit_on_error = False ,
106153 )
107154 print ("✓ Access pod is ready" )
108155 return PVC_ACCESS_POD_NAME
109156
110157
111158def cleanup_access_pod (namespace : str ) -> None :
112159 print ("Cleaning up access pod..." )
113- run_command (
114- [
115- "kubectl" ,
116- "delete" ,
117- "pod" ,
118- PVC_ACCESS_POD_NAME ,
119- "-n" ,
120- namespace ,
121- "--ignore-not-found" ,
122- ],
123- capture_output = False ,
124- )
125- print ("✓ Access pod deleted" )
160+ try :
161+ run_command (
162+ [
163+ "kubectl" ,
164+ "delete" ,
165+ "pod" ,
166+ PVC_ACCESS_POD_NAME ,
167+ "-n" ,
168+ namespace ,
169+ "--ignore-not-found" ,
170+ ],
171+ capture_output = False ,
172+ exit_on_error = False ,
173+ )
174+ print ("✓ Access pod deleted" )
175+ except Exception as e :
176+ print (f"Warning: Failed to clean up access pod: { e } " )
0 commit comments