|
7 | 7 |
|
8 | 8 | import yaml
|
9 | 9 | from kubernetes import client, config, watch
|
10 |
| -from kubernetes.client.models import CoreV1Event, V1PodList |
11 | 10 | from kubernetes.client.api import CoreV1Api
|
| 11 | +from kubernetes.client.models import V1DeleteOptions, V1PodList, V1Status |
12 | 12 | from kubernetes.client.rest import ApiException
|
13 | 13 | from kubernetes.dynamic import DynamicClient
|
14 | 14 | from kubernetes.stream import stream
|
@@ -148,9 +148,27 @@ def delete_namespace(namespace: str) -> V1Status:
|
148 | 148 | return resp
|
149 | 149 |
|
150 | 150 |
|
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 |
154 | 172 |
|
155 | 173 |
|
156 | 174 | def get_default_namespace() -> str:
|
|
0 commit comments