Kubernetes python 客户端相当于 "kubectl wait --for " 命令

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

我正在使用 kubernetes-client/python 并想编写一个方法来阻止控制,直到一组 Pods 处于就绪状态(运行 状态)。我发现 kubernetes 支持 wait --for 命令通过命令做同样的事情。有人可以帮助我如何使用 kubernetes python 客户端实现相同的功能。

准确地说,我最感兴趣的是相当于-

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

您可以使用客户端库中提供的 watch 功能。

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