2

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

确切地说,我最感兴趣的是等效的-

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

1 回答 1

7

您可以使用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
于 2021-01-28T14:29:28.757 回答