我正在使用kubernetes-client/python,并希望编写一个方法,该方法将阻止控制,直到一组pod处于就绪状态(运行状态)。我发现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