duanjipiao7076
2018-12-11 06:18
浏览 570
已采纳

通过websocket将kubernetes日志公开到浏览器

I am trying to use sidecar mode in kubernetes to create a logs sidecar to expose specific container logs. And I am using kubernetes client to fetch logs from kubernetes api and send it out by websocket. The code shows below:

func serveWs(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Access-Control-Allow-Origin", "*")
    conn, err := upgrader.Upgrade(w, r, nil)
    if err != nil {
        if _, ok := err.(websocket.HandshakeError); !ok {
            log.Println(err)
        }
        return
    }

    defer conn.Close()

    logsClient, err := InitKubeLogsClient(config.InCluster)
    if err != nil {
        log.Fatalln(err)
    }

    stream, err := logsClient.GetLogs(config.Namespace, config.PodName, config.ContainerName)
    if err != nil {
        log.Fatalln(err)
    }
    defer stream.Close()
    reader := bufio.NewReader(stream)

    for {
        line, err := reader.ReadString('
')
        if err != nil {
            log.Fatalln(err)
        }
        conn.WriteMessage(websocket.TextMessage, []byte(line))
    }
}

I am using https://github.com/gorilla/websocket as the websocket lib. And on the browser

Is this the best way to do what I want? Is there some better way to just expose the logs api from k8s to websocket?

图片转代码服务由CSDN问答提供 功能建议

我试图在kubernetes中使用sidecar模式来创建日志sidecar,以暴露特定的容器日志。 我正在使用kubernetes客户端从kubernetes api获取日志并通过websocket发送出去。 代码如下所示:

  func serveWs(w http.ResponseWriter,r * http.Request){
 w.Header()。Set(“ Access-Control-Allow  -Origin“,” *“)
 conn,err:=升级程序。Upgrade(w,r,nil)
如果err!= nil {
如果_,ok:= err。(websocket.HandshakeError);  !ok {
 log.Println(err)
} 
 return 
} 
 
延迟conn.Close()
 
 logsClient,err:= InitKubeLogsClient(config.InCluster)
如果出现错误!  = nil {
 log.Fatalln(err)
} 
 
流,err:= logsClient.GetLogs(config.Namespace,config.PodName,config.ContainerName)
如果err!= nil {
 log  .Fatalln(err)
} 
延迟stream.Close()
 reader:= bufio.NewReader(stream)
 
 for {
 line,err:= reader.ReadString('
')\  n如果err!= nil {
 log.Fatalln(err)
} 
 conn.WriteMessage(websocket.TextMessage,[] byte(line))
} 
} 
   
 
 

我正在使用 https://github.com/gorilla/websocket 作为websocket库。 在浏览器上

这是做我想要的最好的方法吗? 有什么更好的方法可以将日志api从k8s公开到websocket?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanqiao1926 2018-12-29 00:10
    已采纳

    Put my final code here, thanks for the tips from @Peter:

    func serveWs(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Access-Control-Allow-Origin", "*")
        conn, err := upgrader.Upgrade(w, r, nil)
        if err != nil {
            if _, ok := err.(websocket.HandshakeError); !ok {
                log.Println(err)
            }
            return
        }
    
        log.Println("create new connection")
    
        defer func() {
            conn.Close()
            log.Println("connection close")
        }()
    
        logsClient, err := InitKubeLogsClient(config.InCluster)
        if err != nil {
            log.Println(err)
            return
        }
    
        stream, err := logsClient.GetLogs(config.Namespace, config.PodName, config.ContainerName)
        if err != nil {
            log.Println(err)
            return
        }
        defer stream.Close()
        reader := bufio.NewReaderSize(stream, 16)
        lastLine := ""
        for {
            data, isPrefix, err := reader.ReadLine()
            if err != nil {
                log.Println(err)
                return
            }
    
            lines := strings.Split(string(data), "")
    
            length := len(lines)
    
            if len(lastLine) > 0 {
                lines[0] = lastLine + lines[0]
                lastLine = ""
            }
    
            if isPrefix {
                lastLine = lines[length-1]
                lines = lines[:(length - 1)]
            }
    
            for _, line := range lines {
                if err := conn.WriteMessage(websocket.TextMessage, []byte(line)); err != nil {
                    log.Println(err)
                    return
                }
            }
        }
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题