簡體   English   中英

Kube.netes python 客戶端相當於“kubectl wait --for”命令

[英]Kubernetes python client equivalent of "kubectl wait --for " command

我正在使用 kube.netes-client/python 並想編寫一個方法來阻止控制,直到一組 Pod 處於 Ready state(運行狀態)。 我發現 kube.netes 支持wait --for 命令來通過命令做同樣的事情。 有人可以幫助我如何使用 kube.netes 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM