Watch kubernetes pod status to be completed in client-go

戏子无情 提交于 2019-12-05 15:41:49

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)

You keep could keep checking the pod status in a loop and whenever the status changes to successful, you're done

for {
    pod, _ := clientset.CoreV1().Pods(Namespace).Get(podName, metav1.GetOptions{})
    if pod.Status.Phase != corev1.PodPending {
        break
    }
}
pod, _ := clientset.CoreV1().Pods(corev1.NamespaceDefault).Get(podName, metav1.GetOptions{})
if pod.Status.Phase != corev1.PodSucceeded {
    return false, fmt.Errorf("Pod did not succeed/complete")
}
return true, nil
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!