Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getPodlog无法获取stdout输出 #1

Open
lvelvis opened this issue Jun 1, 2020 · 0 comments
Open

getPodlog无法获取stdout输出 #1

lvelvis opened this issue Jun 1, 2020 · 0 comments

Comments

@lvelvis
Copy link

lvelvis commented Jun 1, 2020

修改成下面这也才可以获取到
`package controllers

import (
"bytes"
"encoding/json"
"fmt"
"io"

"github.com/astaxie/beego"
core_v1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"

)

type GetPodLog struct {
beego.Controller
}

type PodInfo struct {
Podns string json:"podns"
Podapp string json:"podapp"
//ContainerName string json:"containername"
}

func (c *GetPodLog) PodLog() {
var (
clientset *kubernetes.Clientset
//tailLines int64
req *rest.Request
//res rest.Result
//logs []byte
err error
)

podinfo := PodInfo{}
data := c.Ctx.Input.RequestBody
//json数据封装到user对象中
err = json.Unmarshal(data, &podinfo)
if err != nil {
	fmt.Println("传入正确的JSON格式, err:", err.Error())
}

podns := podinfo.Podns
podapp := podinfo.Podapp
//containername := podinfo.ContainerName

// 初始化k8s客户端
if clientset, err = InitClient(); err != nil {
	fmt.Println(err.Error())
}

// 生成获取POD日志请求
req = clientset.CoreV1().Pods(podns).GetLogs(podapp, &core_v1.PodLogOptions{})

// req.Stream()也可以实现Do的效果
podLogs, err := req.Stream()
if err != nil {
	fmt.Println("error in opening stream")
	return
}
defer podLogs.Close()
// 发送请求
buf := new(bytes.Buffer)
_, err = io.Copy(buf, podLogs)
if err != nil {
	fmt.Println("error in copy information from podLogs to buf")
	return
}
str := buf.String()

fmt.Printf("容器-%s\tstdout日志:\n%s", podapp, str)

c.Data["Email"] = str
c.TplName = "test.html"
c.Ctx.WriteString(str)

}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant