/
logging.go
60 lines (57 loc) · 1.64 KB
/
logging.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
package cluster
import (
"github.com/KubeOperator/kubepi/internal/service/v1/common"
"github.com/KubeOperator/kubepi/pkg/kubernetes"
"github.com/KubeOperator/kubepi/pkg/logging"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/context"
)
func (h *Handler) LoggingHandler() iris.Handler {
return func(ctx *context.Context) {
clusterName := ctx.Params().GetString("name")
namespace := ctx.URLParam("namespace")
podName := ctx.URLParam("podName")
containerName := ctx.URLParam("containerName")
tailLines := 100
follow := false
if ctx.URLParamExists("tailLines") {
lines, err := ctx.URLParamInt("tailLines")
if err != nil {
ctx.StatusCode(iris.StatusBadRequest)
return
}
tailLines = lines
}
if ctx.URLParamExists("follow") {
f, err := ctx.URLParamBool("follow")
if err != nil {
ctx.StatusCode(iris.StatusBadRequest)
return
}
follow = f
}
sessionId, err := logging.GenLoggingSessionId()
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
}
c, err := h.clusterService.Get(clusterName, common.DBOptions{})
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.Values().Set("message", err)
return
}
k := kubernetes.NewKubernetes(c)
client, err := k.Client()
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.Values().Set("message", err)
return
}
logging.LogSessions.Set(sessionId, logging.LogSession{
Id: sessionId,
Bound: make(chan error),
})
go logging.WaitForLoggingStream(client, namespace, podName, containerName, tailLines, follow, sessionId)
ctx.Values().Set("data", TerminalResponse{ID: sessionId})
}
}