Kubernetes sent an api client for a specific container log

I am using kube go client with kube api to access cube data. I currently do not find any api call for the logs of a particular module.

kubectl logs pod-name

returns logs for a specific item. How to do this using go client? I am using v1.0.6 kubernet.

I can get the module using

client.Pods("namespace").Get("pod-name")
+4
source share
1 answer

A look at how kubectl implements its commands can be useful when you feel how to use the client library. In this case, the implementation of the kubectl command is as follows:

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
+5
source

Source: https://habr.com/ru/post/1610521/


All Articles