Using the python client, I've written a function to evict all pods on a node. How can i monitor/watch for all pods to be fully evicted?
I'm using the create_namespaced_pod_eviction method to evict all pods on a single node. While this works, it doesn't wait for the process to finish before continuing. I need the eviction process 100% complete before moving on in my script. How can i monitor the status of this process? Similar to kubectl, i'd like my function to wait for each pod to evict before returning.
# passes list of pods to evict function
def drain_node(self):
print("Draining node", self._node_name)
self.cordon_node()
pods = self._get_pods()
response = []
for pod in pods:
response.append(self._evict_pod(pod))
return response
# calls the eviction api on each pod
def _evict_pod(self, pod, delete_options=None):
name = pod.metadata.name
namespace = pod.metadata.namespace
body = client.V1beta1Eviction(metadata=client.V1ObjectMeta(name=name, namespace=namespace))
response = self._api.create_namespaced_pod_eviction(name, namespace, body)
return response
# gets list of pods to evict
def _get_pods(self):
all_pods = self._api.list_pod_for_all_namespaces(watch=True, field_selector='spec.nodeName=' + self._node_name)
user_pods = [p for p in all_pods.items
if (p.metadata.namespace != 'kube-system')]
return user_pods
As given in this link, the create_namespaced_pod_eviction
call returns V1Beta1Eviction
object. It has ObjectMeta
object, that contains the deletion_timestamp
field. Perhaps you can use that to determine if the pod is already deleted. Or probably polling the pod status might give the same ObjectMeta.