forked from beyondblog/k8s-web-terminal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
143 lines (119 loc) · 3.22 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"flag"
"fmt"
"github.com/kataras/iris"
"github.com/kataras/iris/websocket"
"log"
"strconv"
)
func init() {
iris.Static("/assets", "./public/assets", 1)
iris.Static("/public", "./public", 1)
iris.Config.Render.Template.Engine = iris.PongoEngine
iris.Config.Render.Rest.IndentJSON = true
}
var (
client *DockerClient
k8s *K8sClient
ws websocket.Server
k8sHost = flag.String("k8s_api", "http://127.0.0.1:8080", "Kubernetes api host")
port = flag.Int("port", 8088, "listen port")
)
func main() {
flag.Parse()
iris.Get("/", func(c *iris.Context) {
c.Render("index.html", nil)
})
iris.Get("/api/nodes", listNodes)
iris.Get("/api/nodes/containers", listContainers)
iris.Get("/api/nodes/containers/shell/ws", shellContainer)
iris.Get("/api/nodes/containers/shell/create", createContainer)
iris.Get("/api/nodes/containers/shell/resize", resizeContainer)
iris.Get("/container/terminal", containerTerminal)
k8s = &K8sClient{*k8sHost, make(map[string]*DockerClient)}
addr := fmt.Sprintf("0.0.0.0:%d", *port)
iris.Listen(addr)
}
func listContainers(ctx *iris.Context) {
node := ctx.URLParam("node")
if len(node) > 0 {
containers := k8s.GetContainer(node)
ctx.JSON(iris.StatusOK, containers)
return
}
ctx.Write("node require!")
}
func listNodes(ctx *iris.Context) {
nodes := k8s.Nodes()
ctx.JSON(iris.StatusOK, nodes)
}
func shellContainer(ctx *iris.Context) {
id := ctx.URLParam("id")
node := ctx.URLParam("node")
if len(id) > 0 && len(node) > 0 {
client := k8s.GetDockerClient(node)
input := make(chan []byte)
ws := websocket.NewServer(iris.Config.Websocket)
ws.OnConnection(func(c websocket.Connection) {
log.Printf("\nConnection with ID: %s ", c.ID())
log.Printf("\nCreateId: %s", id)
output, err := client.ExecStart(id, input)
if err != nil {
log.Println("Error: %v", err)
}
go func() {
for {
if data, ok := <-output; ok {
c.EmitMessage(data)
} else {
break
}
}
}()
c.OnMessage(func(data []byte) {
input <- data
})
c.OnDisconnect(func() {
log.Printf("\nConnection with ID: %s has been disconnected!", c.ID())
//send EOF to close chan
input <- []byte("EOF")
close(input)
})
})
if err := ws.Upgrade(ctx); err != nil {
ctx.Write("Upgrade error!")
}
return
}
ctx.Write("param error!")
}
func containerTerminal(ctx *iris.Context) {
ctx.Render("terminal.html", nil)
}
func createContainer(ctx *iris.Context) {
containerId := ctx.URLParam("containerId")
node := ctx.URLParam("node")
command := ctx.URLParam("command")
if len(containerId) > 0 && len(node) > 0 && len(command) > 0 {
client := k8s.GetDockerClient(node)
id, _ := client.CreateExec(containerId, command)
ctx.JSON(iris.StatusOK, map[string]string{"id": id})
return
}
ctx.Write("param error!")
}
func resizeContainer(ctx *iris.Context) {
node := ctx.URLParam("node")
id := ctx.URLParam("id")
cols, _ := strconv.Atoi(ctx.URLParam("cols"))
rows, _ := strconv.Atoi(ctx.URLParam("rows"))
if len(node) > 0 && len(id) > 0 && cols > 0 && rows > 0 {
log.Println("resize: ", id)
client := k8s.GetDockerClient(node)
client.ExecResize(id, cols, rows)
ctx.JSON(iris.StatusOK, nil)
return
}
ctx.Write("param error!")
}