且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Kubernetes使用客户端API记录特定Pod

更新时间:2022-05-13 21:49:10

在了解如何使用客户端库时,查看kubectl如何实现其命令可能会有所帮助.在这种情况下, kubectl对日志命令的实现看起来像这样:

Looking at how kubectl implements its commands can be helpful when getting a feel for how to use the client library. In this case, kubectl's implementation of the logs command looks like this:

req := client.RESTClient.Get().
    Namespace(namespace).
    Name(podID).
    Resource("pods").
    SubResource("log").
    Param("follow", strconv.FormatBool(logOptions.Follow)).
    Param("container", logOptions.Container).
    Param("previous", strconv.FormatBool(logOptions.Previous)).
    Param("timestamps", strconv.FormatBool(logOptions.Timestamps))

if logOptions.SinceSeconds != nil {
    req.Param("sinceSeconds", strconv.FormatInt(*logOptions.SinceSeconds, 10))
}
if logOptions.SinceTime != nil {
    req.Param("sinceTime", logOptions.SinceTime.Format(time.RFC3339))
}
if logOptions.LimitBytes != nil {
    req.Param("limitBytes", strconv.FormatInt(*logOptions.LimitBytes, 10))
}
if logOptions.TailLines != nil {
    req.Param("tailLines", strconv.FormatInt(*logOptions.TailLines, 10))
}
readCloser, err := req.Stream()
if err != nil {
    return err
}

defer readCloser.Close()
_, err = io.Copy(out, readCloser)
return err