-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
210 lines (179 loc) · 5.26 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main
import (
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
func main() {
var username, password, uiPath string
port := *flag.Int("port", 8080, "HTTP port to listen")
flag.StringVar(&username, "user", "", "Username to enable basic-authentication")
flag.StringVar(&password, "password", "", "Password to enable basic-authentication")
flag.StringVar(&uiPath, "ui-path", "./www/", "Path of static web sites")
flag.Parse()
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
var r *gin.RouterGroup
if len(username) > 0 && len(password) > 0 {
r = router.Group("/", gin.BasicAuth(gin.Accounts{
username: password,
}))
} else {
r = router.Group("/")
}
// allow CORS request from localhost
r.Use(cors.New(cors.Config{
AllowMethods: []string{"PUT", "PATCH", "GET", "POST", "DELETE"},
AllowHeaders: []string{"Origin"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
AllowOriginFunc: func(origin string) bool {
if u, err := url.Parse(origin); err == nil {
hostname := u.Hostname()
return hostname == "localhost" || hostname == "127.0.0.1"
}
return false
},
MaxAge: time.Minute,
}))
r.GET("/env", getEnv)
r.GET("/api/pods", getPods)
r.GET("/api/pod/:pod/:container/file/list", getFiles)
r.GET("/api/pod/:pod/:container/file/view", viewFile)
r.GET("/api/pod/:pod/:container/file/download", downloadFile)
r.GET("/api/pod/:pod/:container/process/list", getProcesses)
dirPath, err := filepath.Abs(uiPath)
if err != nil {
panic(err)
}
files, err := ioutil.ReadDir(dirPath)
if err != nil {
panic(err)
}
// static web sites
for _, f := range files {
if f.IsDir() {
if f.Name() != "." && f.Name() != ".." {
r.Static("/"+f.Name()+"/", filepath.Join(dirPath, f.Name()))
}
} else {
r.StaticFile("/"+f.Name(), filepath.Join(dirPath, f.Name()))
}
}
r.StaticFile("/", filepath.Join(dirPath, "index.html")) // default page
fmt.Printf("HTTP listening on port %d\n", port)
panic(router.Run(fmt.Sprintf(":%d", port)))
}
func getEnv(c *gin.Context) {
script := fmt.Sprintf("window.POD_NAMESPACE='%s';", os.Getenv("POD_NAMESPACE"))
c.Data(http.StatusOK, "text/javascript", []byte(script))
}
func getPods(c *gin.Context) {
namespace := c.Query("namespace")
token := c.Query("token")
pods, err := GetPods(namespace, token)
if err != nil {
c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
} else {
c.JSON(http.StatusOK, pods)
}
}
func getFiles(c *gin.Context) {
podName := c.Param("pod")
containerName := c.Param("container")
path := c.DefaultQuery("path", "/")
namespace := c.Query("namespace")
token := c.Query("token")
files, err := GetFiles(podName, containerName, path, namespace, token)
if err != nil {
c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
} else {
c.JSON(http.StatusOK, files)
}
}
func downloadFile(c *gin.Context) {
podName := c.Param("pod")
containerName := c.Param("container")
path := c.DefaultQuery("path", "/")
namespace := c.Query("namespace")
token := c.Query("token")
stdout, err := DownloadSingleFile(podName, containerName, path, namespace, token)
if err != nil {
c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
return
}
defer stdout.Close()
_, filename := filepath.Split(path)
w := c.Writer
header := w.Header()
header.Set("Content-Description", "File Transfer")
header.Set("Content-Transfer-Encoding", "binary")
header.Set("Content-Disposition", "attachment; filename="+filename)
header.Set("Content-Type", "application/octet-stream")
header.Set("Transfer-Encoding", "chunked")
w.WriteHeader(http.StatusOK)
for {
select {
case bufOrErr := <-stdout.Channel():
{
if bufOrErr.err != nil {
fmt.Println("Unable to download file", path, bufOrErr.err)
goto lbExit
}
if bufOrErr.buf == nil {
goto lbExit
}
_, err = w.Write(bufOrErr.buf)
if err != nil {
goto lbExit
}
}
case _ = <-time.After(10 * time.Second):
_, err = w.Write([]byte{})
if err != nil {
goto lbExit
}
w.(http.Flusher).Flush()
}
}
lbExit:
w.(http.Flusher).Flush()
}
func viewFile(c *gin.Context) {
podName := c.Param("pod")
containerName := c.Param("container")
path := c.DefaultQuery("path", "/")
namespace := c.Query("namespace")
token := c.Query("token")
tempFile, err := GetFileContent(podName, containerName, path, namespace, token)
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
return
}
defer os.Remove(tempFile)
_, filename := filepath.Split(path)
c.Header("Content-Description", "File Transfer")
c.Header("Content-Transfer-Encoding", "binary")
c.Header("Content-Disposition", "attachment; filename="+filename)
c.Header("Content-Type", "application/octet-stream")
c.File(tempFile)
}
func getProcesses(c *gin.Context) {
podName := c.Param("pod")
containerName := c.Param("container")
namespace := c.Query("namespace")
token := c.Query("token")
result, err := GetPsResult(podName, containerName, namespace, token)
if err != nil {
c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
} else {
c.JSON(http.StatusOK, map[string]string{"result": string(result)})
}
}