Expose kubernetes logs to browser through websocket

12/11/2018

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('\n')
        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?

-- aisensiy
go
kubernetes
logging
web
websocket

1 Answer

12/29/2018

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

        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
            }
        }
    }
}
-- aisensiy
Source: StackOverflow