Skip to content

Commit 8744ecb

Browse files
committed
delete_pod: appears to work
1 parent 5475d34 commit 8744ecb

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
@@ -148,9 +148,27 @@ def delete_namespace(namespace: str) -> V1Status:
148148
return resp
149149

150150

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

155173

156174
def get_default_namespace() -> str:

0 commit comments

Comments
 (0)