Skip to content

Commit 5c3d516

Browse files
committed
delete_pod: appears to work
1 parent 336422f commit 5c3d516

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/warnet/k8s.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
import yaml
99
from kubernetes import client, config, watch
10-
from kubernetes.client.models import CoreV1Event, V1PodList
1110
from kubernetes.client.api import CoreV1Api
11+
from kubernetes.client.models import V1DeleteOptions, V1PodList, V1Status
1212
from kubernetes.client.rest import ApiException
1313
from kubernetes.dynamic import DynamicClient
1414
from kubernetes.stream import stream
@@ -154,9 +154,27 @@ def delete_namespace(namespace: str) -> V1Status:
154154
return resp
155155

156156

157-
def delete_pod(pod_name: str) -> bool:
158-
command = f"kubectl delete pod {pod_name}"
159-
return stream_command(command)
157+
def delete_pod(
158+
pod_name: str,
159+
namespace: str,
160+
grace_period: int = 30,
161+
force: bool = False,
162+
ignore_not_found: bool = True,
163+
) -> Optional[V1Status]:
164+
v1: CoreV1Api = get_static_client()
165+
delete_options = V1DeleteOptions(
166+
grace_period_seconds=grace_period,
167+
propagation_policy="Foreground" if force else "Background",
168+
)
169+
try:
170+
resp = v1.delete_namespaced_pod(name=pod_name, namespace=namespace, body=delete_options)
171+
return resp
172+
except ApiException as e:
173+
if e.status == 404 and ignore_not_found:
174+
print(f"Pod {pod_name} in namespace {namespace} not found, but ignoring as requested.")
175+
return None
176+
else:
177+
raise
160178

161179

162180
def get_default_namespace() -> str:

0 commit comments

Comments
 (0)