观看kubernetes窗格状态以在客户端运行中完成 [英] Watch kubernetes pod status to be completed in client-go
本文介绍了观看kubernetes窗格状态以在客户端运行中完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在k8 client go中创建一个pod,并制作一个手表以通知pod完成的时间,以便我可以读取pod的日志.监视界面似乎未在频道上提供任何事件.这是代码,如何通知我吊舱状态已完成并准备读取日志
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 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
}
推荐答案
可以使用以下代码段列出事件.然后,您可以根据需要处理pod事件.
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)
这篇关于观看kubernetes窗格状态以在客户端运行中完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文