Kubernetes go client api 获取特定 pod 的日志
Kubernetes go client api for log of a particular pod
我正在使用 kube go 客户端和 kube api 来访问 kube 数据。我目前没有找到任何 api 调用特定 pod 的日志。
kubectl logs pod-name
returns 特定 pod 的日志。我如何使用 go 客户端执行此操作?
我正在使用 kubernetes v1.0.6。
我可以使用
获取 pod
client.Pods("namespace").Get("pod-name")
Client Go has offered a function GetLogs
for this, which has been answered in
了解 kubectl 如何实现其命令有助于了解如何使用客户端库。在这种情况下,kubectl's implementation of the logs command 看起来像这样:
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
type Pipe struct {
InReader *io.PipeReader
InWriter *io.PipeWriter
OutReader *io.PipeReader
OutWriter *io.PipeWriter
}
req := client.RESTClient().Get().Resource("pods").
Name(option.Name).Namespace(option.Namespace).SubResource("log")
opt := &coreV1.PodLogOptions{
Follow: option.Follow,
Previous: option.Previous,
SinceSeconds: option.SinceSeconds,
Timestamps: option.Timestamps,
TailLines: option.TailLines,
LimitBytes: option.LimitBytes,
}
if option.Container != "" {
opt.Container = option.Container
}
req.VersionedParams(
opt,
scheme.ParameterCodec,
)
exec, err := remotecommand.NewSPDYExecutor(k.cli.kubeConfig, http.MethodGet, req.URL())
if err != nil {
return err
}
err = exec.Stream(remotecommand.StreamOptions{
Stdin: pipe.InReader,
Stdout: pipe.OutWriter,
Stderr: pipe.OutWriter,
Tty: true,
})
if err != nil {
return err
}
return nil
我正在使用 kube go 客户端和 kube api 来访问 kube 数据。我目前没有找到任何 api 调用特定 pod 的日志。
kubectl logs pod-name
returns 特定 pod 的日志。我如何使用 go 客户端执行此操作? 我正在使用 kubernetes v1.0.6。
我可以使用
获取 podclient.Pods("namespace").Get("pod-name")
Client Go has offered a function GetLogs
for this, which has been answered in
了解 kubectl 如何实现其命令有助于了解如何使用客户端库。在这种情况下,kubectl's implementation of the logs command 看起来像这样:
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
type Pipe struct {
InReader *io.PipeReader
InWriter *io.PipeWriter
OutReader *io.PipeReader
OutWriter *io.PipeWriter
}
req := client.RESTClient().Get().Resource("pods").
Name(option.Name).Namespace(option.Namespace).SubResource("log")
opt := &coreV1.PodLogOptions{
Follow: option.Follow,
Previous: option.Previous,
SinceSeconds: option.SinceSeconds,
Timestamps: option.Timestamps,
TailLines: option.TailLines,
LimitBytes: option.LimitBytes,
}
if option.Container != "" {
opt.Container = option.Container
}
req.VersionedParams(
opt,
scheme.ParameterCodec,
)
exec, err := remotecommand.NewSPDYExecutor(k.cli.kubeConfig, http.MethodGet, req.URL())
if err != nil {
return err
}
err = exec.Stream(remotecommand.StreamOptions{
Stdin: pipe.InReader,
Stdout: pipe.OutWriter,
Stderr: pipe.OutWriter,
Tty: true,
})
if err != nil {
return err
}
return nil