Skip to content

Commit

Permalink
Merge pull request #16 from mogenius/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
behrangalavi authored Oct 10, 2023
2 parents 4d42af7 + 46ea60c commit cc641d2
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 72 deletions.
4 changes: 0 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ BINARY_NAME=punq
GO=go
GOBUILD=$(GO) build
GOCLEAN=$(GO) clean
GOTEST=$(GO) test
GOGET=$(GO) get

# Ensure linker embeds versioning information
Expand Down Expand Up @@ -40,9 +39,6 @@ clean:
rm -f $(BINARY_NAME)-$(VERSION)-windows-amd64
rm -f $(BINARY_NAME)-$(VERSION)-windows-386

test:
$(GOTEST) -v ./...

darwin_arm64:
GOOS=darwin GOARCH=arm64 $(GOBUILD) -ldflags="$(LDFLAGS)" -o builds/$(BINARY_NAME)-$(VERSION)-darwin-arm64 -v

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.21

require (
github.com/cert-manager/cert-manager v1.12.3
github.com/creack/pty v1.1.18
github.com/fatih/color v1.15.0
github.com/gin-contrib/cors v1.4.0
github.com/gin-gonic/gin v1.9.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLI
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY=
github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
4 changes: 2 additions & 2 deletions operator/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func InitFrontend() {
router := gin.New()
config := cors.DefaultConfig()
config.AllowAllOrigins = true
config.AllowHeaders = []string{"Origin", "Content-Length", "Content-Type", "authorization"}
config.AllowHeaders = []string{"Origin", "Content-Length", "Content-Type", "authorization", "x-context-id"}

router.Use(cors.New(config))
router.Use(CreateLogger("ANGULAR"))
Expand All @@ -46,7 +46,7 @@ func InitBackend() {
router := gin.New()
config := cors.DefaultConfig()
config.AllowAllOrigins = true
config.AllowHeaders = []string{"Origin", "Content-Length", "Content-Type", "authorization"}
config.AllowHeaders = []string{"Origin", "Content-Length", "Content-Type", "authorization", "x-context-id"}

router.Use(cors.New(config))
router.Use(CreateLogger("BACKEND"))
Expand Down
118 changes: 56 additions & 62 deletions operator/routes-websocket.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package operator

import (
"bufio"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/exec"

"github.com/creack/pty"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/mogenius/punq/dtos"
"github.com/mogenius/punq/utils"
"log"
"net/http"
"os"
"os/exec"
"strings"
)

type WindowSize struct {
Rows uint16 `json:"rows"`
Cols uint16 `json:"cols"`
}

func InitWebsocketRoutes(router *gin.Engine) {
router.GET("/exec-sh", AuthByParameter(dtos.ADMIN), connectWs)
}
Expand Down Expand Up @@ -49,83 +55,71 @@ func connectWs(c *gin.Context) {

ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
log.Printf("Failed to upgrade ws: %+v", err)
log.Printf("Failed to upgrade ws: %s", err.Error())
return
}
defer func() {
ws.Close()
}()

cmd := exec.Command("sh", "-c", fmt.Sprintf("kubectl exec -i --tty -c %s -n %s %s -- /bin/sh", container, namespace, podName))
cmd.Env = os.Environ()
stdin, err := cmd.StdinPipe()
if err != nil {
log.Fatal("Error creating stdin pipe:", err)
}
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal("Error creating stdout pipe:", err)
log.Printf("Unable to upgrade connection: %s", err.Error())
return
}
stderr, err := cmd.StderrPipe()

cmd := exec.Command("sh", "-c", fmt.Sprintf("kubectl exec -it -c %s -n %s %s -- sh -c \"clear; (bash || ash || sh || ksh || csh || zsh )\"", container, namespace, podName))
cmd.Env = append(os.Environ(), "TERM=xterm-color")

tty, err := pty.Start(cmd)
if err != nil {
log.Fatal("Error creating stderr pipe:", err)
log.Printf("Unable to start pty/cmd: %s", err.Error())
ws.WriteMessage(websocket.TextMessage, []byte(err.Error()))
return
}

go func() {
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
data := scanner.Bytes()
if utils.CONFIG.Misc.Debug {
fmt.Printf("Response-Line: '%s'\n", string(data))
}
err = ws.WriteMessage(websocket.TextMessage, data)
if err != nil {
log.Printf("Error writing to ws: %+v", err)
}
}
defer func() {
cmd.Process.Kill()
cmd.Process.Wait()
tty.Close()
ws.Close()
}()

go func() {
scanner := bufio.NewScanner(stderr)
for scanner.Scan() {
data := scanner.Bytes()
if utils.CONFIG.Misc.Debug {
fmt.Printf("Response-Line: '%s'\n", string(data))
}
err = ws.WriteMessage(websocket.TextMessage, data)
for {
buf := make([]byte, 1024)
read, err := tty.Read(buf)
if err != nil {
log.Printf("Error writing to ws: %+v", err)
ws.WriteMessage(websocket.TextMessage, []byte(err.Error()))
log.Printf("Unable to read from pty/cmd: %s", err.Error())
return
}
ws.WriteMessage(websocket.BinaryMessage, buf[:read])
}
}()

go func() {
for {
_, msg, err := ws.ReadMessage()
if utils.CONFIG.Misc.Debug {
fmt.Printf("Received Cmd: '%s'", string(msg))
}
msg = append(msg, '\n')
for {
_, reader, err := ws.ReadMessage()
if err != nil {
log.Printf("Unable to grab next reader: %s", err.Error())
return
}

if strings.HasPrefix(string(reader), "\x04") {
str := strings.TrimPrefix(string(reader), "\x04")

var resizeMessage WindowSize
err := json.Unmarshal([]byte(str), &resizeMessage)
if err != nil {
log.Printf("Error reading from ws: %+v", err)
log.Printf("CLOSE: exec-sh: %s %s %s\n", namespace, container, podName)
break
log.Printf("%s", err.Error())
continue
}
_, err = stdin.Write(msg)
if err != nil {
log.Printf("Error writing to stdin: %+v", err)

if err := pty.Setsize(tty, &pty.Winsize{Rows: uint16(resizeMessage.Rows), Cols: uint16(resizeMessage.Cols)}); err != nil {
log.Printf("Unable to resize: %s", err.Error())
continue
}
continue
}
}()

err = cmd.Start()
if err != nil {
log.Printf("Error starting cmd: %+v", err)
return
}

err = cmd.Wait()
if err != nil {
log.Printf("Cmd returned error: %+v", err.Error())
return
tty.Write(reader)
}
}
8 changes: 4 additions & 4 deletions punq-dev.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"version": "1.4.0",
"version": "1.4.1-develop.1",
"license": "MIT",
"homepage": "https://punq.dev",
"bin": "punq-dev.exe",
"pre_install": "Rename-Item \"$dir\\punq-dev-v1.4.0-windows-amd64\" punq-dev.exe",
"pre_install": "Rename-Item \"$dir\\punq-dev-v1.4.1-develop.1-windows-amd64\" punq-dev.exe",
"description": "A slim open-source workload manager for Kubernetes with team collaboration, WebApp, and CLI.",
"architecture": {
"64bit": {
"url": "https://github.com/mogenius/punq/releases/download/v1.4.0/punq-v1.4.0-windows-amd64",
"hash": ""
"url": "https://github.com/mogenius/punq/releases/download/v1.4.1-develop.1/punq-v1.4.1-develop.1-windows-amd64",
"hash": "b8a74e4a98c664be18553cf834eaf30cf65d5c72c4379db0fb0fccaa96b8b0aa"
}
}
}

0 comments on commit cc641d2

Please sign in to comment.