<p>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:</p>

<pre><code>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('\n')
        if err != nil {
            log.Fatalln(err)
        }
        conn.WriteMessage(websocket.TextMessage, []byte(line))
    }
}
</code></pre>

<p>I am using <a href="https://github.com/gorilla/websocket" rel="nofollow noreferrer">https://github.com/gorilla/websocket</a> as the websocket lib. And on the browser</p>

<p>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?</p>

<p>Put my final code here, thanks for the tips from @Peter:</p>

<pre><code>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), "\r")

        length := len(lines)

        if len(lastLine) &gt; 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
            }
        }
    }
}
</code></pre>
