Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: embedded cluster manager websocket #5015

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pkg/apiserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ func Start(params *APIServerParams) {

handlers.RegisterUnauthenticatedRoutes(handler, kotsStore, debugRouter, loggingRouter)

/**********************************************************************
* Websocket routes (only for embedded cluster)
**********************************************************************/

if util.IsEmbeddedCluster() {
wsRouter := r.NewRoute().Subrouter()
wsRouter.HandleFunc("/ec-ws", handler.ConnectToECWebsocket)
}

/**********************************************************************
* KOTS token auth routes
**********************************************************************/
Expand Down
3 changes: 3 additions & 0 deletions pkg/handlers/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,7 @@ type KOTSHandler interface {
// Upgrade service
StartUpgradeService(w http.ResponseWriter, r *http.Request)
GetUpgradeServiceStatus(w http.ResponseWriter, r *http.Request)

// EC Websocket
ConnectToECWebsocket(w http.ResponseWriter, r *http.Request)
}
12 changes: 12 additions & 0 deletions pkg/handlers/mock/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

145 changes: 145 additions & 0 deletions pkg/handlers/websocket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package handlers

import (
"fmt"
"math/rand"
"net"
"net/http"
"sync"
"time"

"github.com/gorilla/websocket"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/logger"
)

var wsUpgrader = websocket.Upgrader{}
var wsClients = make(map[string]*websocket.Conn)
var wsMutex = sync.Mutex{}

type ConnectToECWebsocketResponse struct {
Error string `json:"error,omitempty"`
}

func (h *Handler) ConnectToECWebsocket(w http.ResponseWriter, r *http.Request) {
response := ConnectToECWebsocketResponse{}

nodeName := r.URL.Query().Get("nodeName")
if nodeName == "" {
response.Error = "missing node name"
logger.Error(errors.New(response.Error))
JSON(w, http.StatusBadRequest, response)
return
}

conn, err := wsUpgrader.Upgrade(w, r, nil)
if err != nil {
response.Error = "failed to upgrade to ws connection"
logger.Error(errors.Wrap(err, response.Error))
JSON(w, http.StatusInternalServerError, response)
return
}
defer conn.Close()

conn.SetPingHandler(wsPingHandler(nodeName, conn))
conn.SetPongHandler(wsPongHandler(nodeName, conn))
conn.SetCloseHandler(wsCloseHandler(nodeName, conn))

// register the client
registerWSClient(nodeName, conn)

// ping client on a regular interval to make sure it's still connected
go pingWSClient(nodeName, conn)

// listen to client messages
listenToWSClient(nodeName, conn)
}

func pingWSClient(nodeName string, conn *websocket.Conn) {
for {
sleepDuration := time.Second * time.Duration(5+rand.Intn(16)) // 5-20 seconds
time.Sleep(sleepDuration)

pingMsg := fmt.Sprintf("%d", rand.Intn(1000))
logger.Infof("Sending ping message '%s' to %s", pingMsg, nodeName)

if err := conn.WriteControl(websocket.PingMessage, []byte(pingMsg), time.Now().Add(1*time.Second)); err != nil {
if isWSConnClosed(nodeName, err) {
return
sgalsaleh marked this conversation as resolved.
Show resolved Hide resolved
}
logger.Errorf("Failed to send ping message to %s: %v", nodeName, err)
}
}
}

func listenToWSClient(nodeName string, conn *websocket.Conn) {
for {
_, _, err := conn.ReadMessage() // this is required to receive ping/pong messages
if err != nil {
if isWSConnClosed(nodeName, err) {
return
}
logger.Errorf("Error reading websocket message from %s: %v", nodeName, err)
}
}
}

func registerWSClient(nodeName string, conn *websocket.Conn) {
wsMutex.Lock()
defer wsMutex.Unlock()

if existingConn, ok := wsClients[nodeName]; ok {
existingConn.Close()
delete(wsClients, nodeName)
}
wsClients[nodeName] = conn

logger.Infof("Registered new websocket for %s", nodeName)
}

func wsPingHandler(nodeName string, conn *websocket.Conn) func(message string) error {
return func(message string) error {
logger.Infof("Received ping message '%s' from %s", message, nodeName)
logger.Infof("Sending pong message '%s' to %s", message, nodeName)
if err := conn.WriteControl(websocket.PongMessage, []byte(message), time.Now().Add(1*time.Second)); err != nil {
logger.Errorf("Failed to send pong message to %s: %v", nodeName, err)
}
return nil
}
}

func wsPongHandler(nodeName string, conn *websocket.Conn) func(message string) error {
return func(message string) error {
logger.Infof("Received pong message '%s' from %s", message, nodeName)
return nil
}
}

func wsCloseHandler(nodeName string, conn *websocket.Conn) func(code int, text string) error {
return func(code int, text string) error {
logger.Errorf("Websocket connection closed for %s: %d (exit code), message: %q", nodeName, code, text)

wsMutex.Lock()
delete(wsClients, nodeName)
wsMutex.Unlock()

closeMessage := websocket.FormatCloseMessage(code, text)
if err := conn.WriteControl(websocket.CloseMessage, closeMessage, time.Now().Add(time.Second)); err != nil {
logger.Errorf("Failed to send close message to %s: %v", nodeName, err)
}
return nil
}
}

func isWSConnClosed(nodeName string, err error) bool {
sgalsaleh marked this conversation as resolved.
Show resolved Hide resolved
if _, ok := wsClients[nodeName]; !ok {
return true
}
if _, ok := err.(*websocket.CloseError); ok {
return true
}
if _, ok := err.(*net.OpError); ok {
return true
}
return false
}
Loading