<p>I'm looking for the solution of how to get logs from a pod in Kubernetes cluster using Go. I've looked at &quot;https://github.com/kubernetes/client-go&quot; and &quot;https://godoc.org/sigs.k8s.io/controller-runtime/pkg/client&quot;, but couldn't understand how to use them for this purpose. I have no issues getting information of a pod or any other object in K8S except for logs.</p>
<p>For example, I'm using Get() from &quot;https://godoc.org/sigs.k8s.io/controller-runtime/pkg/client#example-Client--Get&quot; to get K8S job info:</p>
<pre><code>found := &amp;batchv1.Job{}
err = r.client.Get(context.TODO(), types.NamespacedName{Name: job.Name, Namespace: job.Namespace}, found)
</code></pre>
<p>Please share of how you get pod's logs nowadays.
Any suggestions would be appreciated!</p>
<p>Update:
The solution provided in <a href="https://stackoverflow.com/questions/32983228/kubernetes-go-client-api-for-log-of-a-particular-pod">Kubernetes go client api for log of a particular pod</a> is out of date. It have some tips, but it is not up to date with current libraries.</p>

Stepan Maksimchuk
<p>Here is what we came up with eventually using client-go library:</p>

<pre><code>func getPodLogs(pod corev1.Pod) string {
    podLogOpts := corev1.PodLogOptions{}
    config, err := rest.InClusterConfig()
    if err != nil {
        return "error in getting config"
    }
    // creates the clientset
    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {
        return "error in getting access to K8S"
    }
    req := clientset.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &amp;podLogOpts)
    podLogs, err := req.Stream()
    if err != nil {
        return "error in opening stream"
    }
    defer podLogs.Close()

    buf := new(bytes.Buffer)
    _, err = io.Copy(buf, podLogs)
    if err != nil {
        return "error in copy information from podLogs to buf"
    }
    str := buf.String()

    return str
}
</code></pre>

<p>I hope it will help someone. Please share your thoughts or solutions of how you get logs from pods in Kubernetes.</p>
