简体   繁体   中英

Kubernetes python client equivalent of "kubectl wait --for " command

I am using kube.netes-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 kube.netes supports wait --for command for doing same thing via command. Can someone please help me with how to achieve same functionality using kube.netes python client.

To be precise i am mostly interested in equivalent of-

kubectl wait --for condition=Ready pod -l 'app in (kafka,elasticsearch)'

You can use the watch functionality available in the client library.

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["object"].status.phase == "Running":
        w.stop()
        end_time = time.time()
        logger.info("%s started in %0.2f sec", full_name, end_time-start_time)
        return
    # event.type: ADDED, MODIFIED, DELETED
    if event["type"] == "DELETED":
        # Pod was deleted while we were waiting for it to start.
        logger.debug("%s deleted before it started", full_name)
        w.stop()
        return

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM