[Python] Using Python to watch Kubernetes pod status but doesn't break out of the for loop

Discussão em 'Python' iniciado por Stack, Julho 11, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to use Python to watch pod status and when they're all ready to proceed with my script. But for some reason, after it lists the pod's being ready the script just hangs and I'm not sure why. Here is my code:

    from kubernetes import client, config, watch
    import time
    config.load_kube_config()
    core_v1 = client.CoreV1Api()
    w = watch.Watch()

    namespace = "kube-system" # Replace with your target namespace

    print(f"Waiting for pods in namespace '{namespace}' if specified) to become Ready...")
    def pod_status():
    for event in w.stream(func=core_v1.list_namespaced_pod,
    namespace=namespace,
    timeout_seconds=600): # Set a reasonable timeout
    pod = event['object']
    if pod.status.phase == "Running":
    all_containers_ready = True
    if pod.status.container_statuses:
    for container_status in pod.status.container_statuses:
    if not container_status.ready:
    all_containers_ready = False
    break
    if all_containers_ready:
    print(f"Pod '{pod.metadata.name}' is Ready.")
    print("stopping")
    print("All specified pods are potentially ready.")
    w.stop()

    pod_status()


    I'm not sure what I'm missing

    Continue reading...

Compartilhe esta Página