无法从监视流解码事件:流错误:流ID 3; INTERNAL_ERROR

时间:2019-02-19 07:29:33

标签: go kubernetes client-go

我想监听群集中部署的添加事件,我使用的是Client-go Watch Api,起初效果很好,但是过一会儿,它将报告错误。

我尝试了两种API监听方式

一种方法:

func startWatchDeployment(clientSet *kubernetes.Clientset){
    defer func() {
        err := recover()
        if err != nil {
            fmt.Println(err)
        }
    }()

    Log.Info("正在监听deployment...")
    count := 0
    deploymentsClient := clientSet.AppsV1beta1().Deployments(metav1.NamespaceAll)
    list,_ := deploymentsClient.List(metav1.ListOptions{})
    items := list.Items
    w, _ := deploymentsClient.Watch(metav1.ListOptions{})
    for {
        select {
            case e, _ := <-w.ResultChan():
                Log.Infof("162: %s",e)
                if e.Type == watch.Added || e.Type == watch.Deleted{
                    if count != len(items){
                        count += 1
                    }else{
                        // go的reflect获取运行时的struct
                        nname := e.Object.(*v1beta1.Deployment).Namespace
                        if r, _ := regexp.Compile("^(p|u|user)-");nname != "default" && nname != "cattle-system" &&
                            nname != "kube-system" && nname != "dsky-system" &&
                            nname != "kube-public" && nname != "local" && nname != "tools" && !r.MatchString(nname) {
                            data := make(map[string]interface{},1)
                            data["type"] = e.Type
                            data["name"] = e.Object.(*v1beta1.Deployment).Name
                            data["namespace"] = e.Object.(*v1beta1.Deployment).Namespace
                            watchChannel <- data
                        }
                    }
                }
        }
    }
}

另一种方式:

func startWatchDp(clientSet *kubernetes.Clientset){
    watchlist := cache.NewListWatchFromClient(
        clientSet.AppsV1().RESTClient(),
        "deployments",
        metav1.NamespaceAll,
        fields.Everything())

    _, controller := cache.NewInformer(
        watchlist,
        &v13.Deployment{},
        time.Millisecond*100,
        cache.ResourceEventHandlerFuncs{
            AddFunc: func(obj interface{}) {
                watchChannel <- 1
                //fmt.Println(obj)
            },
            DeleteFunc: func(obj interface{}) {
                watchChannel <- 1
            },
        },
    )

    stop := make(chan struct{})
    go controller.Run(stop)

    for {
        time.Sleep(10 * time.Second)
    }
}

这是我的结果:

E0219 15:20:02.274210   19272 streamwatcher.go:109] Unable to decode an event from the watch stream: stream error: stream ID 3; INTERNAL_ERROR
time="2019-02-19T15:20:02+08:00" level=info msg="162: { <nil>}"
time="2019-02-19T15:20:02+08:00" level=info msg="162: { <nil>}"
time="2019-02-19T15:20:02+08:00" level=info msg="162: { <nil>}"
time="2019-02-19T15:20:02+08:00" level=info msg="162: { <nil>}"
time="2019-02-19T15:20:02+08:00" level=info msg="162: { <nil>}"

我该如何解决?请帮助!谢谢

0 个答案:

没有答案