def delete_unschedulable_pods(api: CoreV1Api, namespace: str): """ Delete pods that are unschedulable due to a missing persistent volume claim. A stateful set may create a pod attached to a missing persistent volume claim if the pod is recreated while the persistent volume claim is pending delete. When this happens, delete the pod so that the stateful set will create a new persistent volume claim when it next creates the pod. """ for pod in api.list_namespaced_pod(namespace).items: if _unschedulable_due_to_pvc(pod): logger.info(f"deleting unschedulable pod: {pod.metadata.name}") try: api.delete_namespaced_pod( name=pod.metadata.name, namespace=namespace, body=V1DeleteOptions( grace_period_seconds=0, propagation_policy="Background", preconditions=V1Preconditions( resource_version=pod.metadata.resource_version, uid=pod.metadata.uid, ), ), ) except ApiException as e: if e.reason not in (CONFLICT, NOT_FOUND): raise logger.info(f"pod already deleted or updated: {pod.metadata.name}")
def restart_web_pods(api: CoreV1Api): for pod in api.list_namespaced_pod("default").items: if pod.metadata.labels.get("app") != "web": continue api.delete_namespaced_pod( pod.metadata.name, pod.metadata.namespace, body=V1DeleteOptions( grace_period_seconds=0, propagation_policy="Background", preconditions=V1Preconditions( resource_version=pod.metadata.resource_version, uid=pod.metadata.uid, ), ), )
def delete_pod(core: CoreV1Api, namespace: str, pod_name: str): core.delete_namespaced_pod(pod_name, namespace, V1DeleteOptions())