[英]Watch kubernetes pod status to be completed in client-go
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. 我在k8 client go中创建了一个容器,并制作了一个手表以通知容器完成的时间,以便我可以读取容器的日志。 The watch interface doesnt seem to provide any events on the channel.
监视界面似乎未在频道上提供任何事件。 Here is the code, how would I get notified that the pod status is now completed and is ready to read the logs
这是代码,如何通知我吊舱状态现已完成并准备读取日志
func readLogs(clientset *kubernetes.Clientset) {
// namespace := "default"
// label := "cithu"
var (
pod *v1.Pod
// watchface watch.Interface
err error
)
// returns a pod after creation
pod, err = createPod(clientset)
fmt.Println(pod.Name, pod.Status, err)
if watchface, err = clientset.CoreV1().Pods(namespace).Watch(metav1.ListOptions{
LabelSelector: pod.Name,
}); err != nil {
log.Fatalf(err.Error())
}
// How do I get notified when the pod.Status == completed
}
The events can be listed using the following snippet. 可以使用以下代码段列出事件。 You can then process the pod events as needed.
然后,您可以根据需要处理pod事件。
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 您可以保持循环检查Pod状态,只要状态更改为成功,就可以完成
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.