Watch kubernetes pod status to be completed in client-go

前端 未结 2 1224
别跟我提以往
别跟我提以往 2020-12-16 23:22

I am creating a pod in k8 client go and making a watch to get notified for when the pod has completed so that i can read the logs of the pod. The watch interface doesnt seem

2条回答
  •  难免孤独
    2020-12-16 23:58

    The events can be listed using the following snippet. You can then process the pod events as needed.

    label := ""
    for k := range pod.GetLabels() {
        label = k
        break
    }
    watch, err := clientset.CoreV1().Pods(namespace).Watch(metav1.ListOptions{
        LabelSelector: label,
    })
    if err != nil {
        log.Fatal(err.Error())
    }
    go func() {
        for event := range watch.ResultChan() {
            fmt.Printf("Type: %v\n", event.Type)
            p, ok := event.Object.(*v1.Pod)
            if !ok {
                log.Fatal("unexpected type")
            }
            fmt.Println(p.Status.ContainerStatuses)
            fmt.Println(p.Status.Phase)
        }
    }()
    time.Sleep(5 * time.Second)
    

提交回复
热议问题