|
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
|
@@ -154,9 +154,27 @@ def delete_namespace(namespace: str) -> V1Status:
|
154 | 154 | return resp
|
155 | 155 |
|
156 | 156 |
|
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 |
160 | 178 |
|
161 | 179 |
|
162 | 180 | def get_default_namespace() -> str:
|
|
0 commit comments