<p>I am using kubernetes-client/python and want to write a method which will block control until a set of Pods is in Ready state (Running state). I found that kubernetes supports <strong>wait --for</strong> command for doing same thing via command. Can someone please help me with how to achieve same functionality using kubernetes python client.</p>
<p>To be precise i am mostly interested in equivalent of-</p>
<pre><code>kubectl wait --for condition=Ready pod -l 'app in (kafka,elasticsearch)'
</code></pre>

<p>You can use the <a href="https://github.com/kubernetes-client/python/blob/master/examples/pod_namespace_watch.py" rel="nofollow noreferrer"><code>watch</code></a> functionality available in the client library.</p>
<pre class="lang-py prettyprint-override"><code>from kubernetes import client, config, watch

config.load_kube_config()
w = watch.Watch()
core_v1 = client.CoreV1Api()
for event in w.stream(func=core_v1.list_namespaced_pod,
                          namespace=namespace,
                          label_selector=label,
                          timeout_seconds=60):
    if event[&quot;object&quot;].status.phase == &quot;Running&quot;:
        w.stop()
        end_time = time.time()
        logger.info(&quot;%s started in %0.2f sec&quot;, full_name, end_time-start_time)
        return
    # event.type: ADDED, MODIFIED, DELETED
    if event[&quot;type&quot;] == &quot;DELETED&quot;:
        # Pod was deleted while we were waiting for it to start.
        logger.debug(&quot;%s deleted before it started&quot;, full_name)
        w.stop()
        return
</code></pre>
