forked from Qihoo360/wayne
/
log.go
67 lines (61 loc) · 1.86 KB
/
log.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
package log
import (
"github.com/Qihoo360/wayne/src/backend/client"
"github.com/Qihoo360/wayne/src/backend/controllers/base"
"github.com/Qihoo360/wayne/src/backend/models"
"github.com/Qihoo360/wayne/src/backend/resources/log"
"github.com/Qihoo360/wayne/src/backend/util/hack"
"github.com/Qihoo360/wayne/src/backend/util/logs"
"k8s.io/api/core/v1"
)
type KubeLogController struct {
base.APIController
}
func (c *KubeLogController) URLMapping() {
c.Mapping("List", c.List)
}
func (c *KubeLogController) Prepare() {
// Check administration
c.APIController.Prepare()
perAction := ""
_, method := c.GetControllerAndAction()
switch method {
case "List":
perAction = models.PermissionRead
}
if perAction != "" {
c.CheckPermission(models.PermissionTypeDeployment, perAction)
}
}
// @Title log
// @Description pod logs
// @Param tailLines query int true "log tail lines."
// @Param cluster path string true "cluster name."
// @Param namespace path string true "namespace name."
// @Param pod path string true "pod name."
// @Param container path string true "container name."
// @Success 200 {object} "log text" success
// @router /:pod/containers/:container/namespaces/:namespace/clusters/:cluster [get]
func (c *KubeLogController) List() {
tailLines := c.GetIntParamFromQuery("tailLines")
cluster := c.Ctx.Input.Param(":cluster")
namespace := c.Ctx.Input.Param(":namespace")
pod := c.Ctx.Input.Param(":pod")
container := c.Ctx.Input.Param(":container")
opt := &v1.PodLogOptions{
Container: container,
TailLines: &tailLines,
}
cli, err := client.Client(cluster)
if err == nil {
result, err := log.GetLogsByPod(cli, namespace, pod, opt)
if err != nil {
logs.Info("get kubernetes log by pod error.", cluster, namespace, pod, err)
c.HandleError(err)
return
}
c.Success(hack.String(result))
} else {
c.AbortBadRequestFormat("Cluster")
}
}