<p>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?</p>

<p>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. </p>

<pre class="lang-py prettyprint-override"><code># 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
</code></pre>

<p>As given in this <a href="https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#create_namespaced_pod_eviction" rel="nofollow noreferrer">link</a>, the <code>create_namespaced_pod_eviction</code> call returns <a href="https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1beta1Eviction.md" rel="nofollow noreferrer"><code>V1Beta1Eviction</code></a> object. It has <a href="https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1ObjectMeta.md" rel="nofollow noreferrer"><code>ObjectMeta</code></a> object, that contains the <code>deletion_timestamp</code> 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.</p>
