How can I get logs from all containers via corev1.PodLogOptions?

5/13/2020

I am using kubernetes/client-go to retrieve some pod logs. I am able to retrieve logs should the pod have one container as such

req := client.CoreV1().Pods("namespace").GetLogs("mypod", &corev1.PodLogOptions{})
logs, err := req.Stream()
[...]

This works well, until I encounter a pod that has more than one container, to which I get the following error

a container name must be specified for pod xxx, choose one of: [aaa bbb] or one of the init containers: [aaa bbb]

I was hoping to find an accommodating field on the corev1.PodLogOptions object, but am only finding a specific Container field.

I'm searching for an --all-containers equivalent as offered with the REST client.

$ kubectl logs mypod --all-containers

Is this possible? Any alternatives?

-- scniro
go
kubernetes

2 Answers

5/13/2020

Your question is quite specialized and not easy for me to have snippet in head, but the strategy was always the following: if you want to mimic the behaviour of kubectl you obviously should first inspect sources of kubectl. You can start from here looking for the pattern you want, then you have to inspect supplementary packages, tests and etc. having a nice trip. In my experience, some sugar operations in kubectl are possible not because they are supported natively by REST API, but because of iteration cycles with dispatching of many different requests.

Hope it helps!

-- Olegs
Source: StackOverflow

5/13/2020

If you take a look in kubectl code they just get all relevant containers in a pod and then iterate over them and gather logs container by container. So I don't think there's REST API endpoint that would do that for you.

See here: https://github.com/kubernetes/kubectl/blob/19fd05792d8c806a5024d6bbbdd7d66d3234cbcb/pkg/polymorphichelpers/logsforobject.go#L86

-- blami
Source: StackOverflow