Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a way to retrieve logs for previous pod instances (similar to --previous option on kubectl) #2408

Closed
longwa opened this issue Aug 18, 2020 · 8 comments · Fixed by #2477
Assignees

Comments

@longwa
Copy link

longwa commented Aug 18, 2020

Kubectl allows for the --previous option to logs to view previous instance of the logs.

You can use kubectl logs to retrieve logs from a previous instantiation of a container with --previous flag, in case the container has crashed. If your pod has multiple containers, you should specify which container's logs you want to access by appending a container name to the command. See the kubectl logs documentation for more details

I can't seem to find a way to do that in the fabric8 API.

Also, I'm not sure how to retrieve logs for a specific container within the pod in cases where there are multiple. Maybe this exists in the API, but I can't find an example of how to do it.

@rohanKanojia
Copy link
Member

Do you know what api is hit behind the scenes with --previous? Could you please share output of kubectl logs --previous pod/your-pod -v=8?

For latter part, Could you please check if this works for you?

log = client.pods().withName("pod3").inContainer("cnt3").getLog();
assertEquals(pod3Log, log);
log = client.pods().inNamespace("test4").withName("pod4").inContainer("cnt4").withPrettyOutput().getLog();
assertEquals(pod4Log, log);

@longwa
Copy link
Author

longwa commented Aug 19, 2020

Looks like it just passes the previous=true:

I0818 19:57:11.540778   81327 round_trippers.go:420] GET https://<ip>:6443/api/v1/namespaces/playarea-52688c/pods/tcs-6db8ff5bbc-fqzlg/log?previous=true
I0818 19:57:11.540802   81327 round_trippers.go:427] Request Headers:
I0818 19:57:11.540807   81327 round_trippers.go:431]     Accept: application/json, */*
I0818 19:57:11.540810   81327 round_trippers.go:431]     User-Agent: kubectl/v1.18.2 (darwin/amd64) kubernetes/52c56ce
I0818 19:57:11.587698   81327 round_trippers.go:446] Response Status: 200 OK in 46 milliseconds
I0818 19:57:11.587727   81327 round_trippers.go:449] Response Headers:
I0818 19:57:11.587731   81327 round_trippers.go:452]     Cache-Control: no-cache, private
I0818 19:57:11.587734   81327 round_trippers.go:452]     Content-Type: text/plain
I0818 19:57:11.587738   81327 round_trippers.go:452]     Date: Tue, 18 Aug 2020 23:57:11 GMT

Not sure the best approach, I guess either adding a getPreviousLog() method or maybe a boolean (previous = true) for the getLog() method (it already has pretty print option).

I'll try the specific container approach, I didn't realize that inContainer method existed. Thanks!

@rohanKanojia
Copy link
Member

Sorry, I think I said we don't support it without checking. I checked the code and seems like this flag is being passed:

I think this should do the trick for you:

client.pods().inNamespace(namespace).withName("test-pod1").terminated().getLog();

@rohanKanojia
Copy link
Member

The only thing we're missing is adding some documentation about these supported flags

@rohanKanojia rohanKanojia self-assigned this Sep 9, 2020
@longwa
Copy link
Author

longwa commented Sep 9, 2020

Thanks, that's what I was looking for, just didn't see it initially.

@rohanKanojia
Copy link
Member

rohanKanojia commented Sep 9, 2020

@longwa : No worries. I think one of the main problems in this project is that there is not much documentation present for users. I'll try adding documentation for Pod log options here: https://github.com/fabric8io/kubernetes-client/blob/master/doc/CHEATSHEET.md

@longwa
Copy link
Author

longwa commented Sep 9, 2020

@rohanKanojia Most of the API is very intuitive and easy to discover. It's is much easier to use than the official client.

@rohanKanojia
Copy link
Member

rohanKanojia commented Sep 9, 2020

@longwa : Thanks a lot for your feedback. I also think our API is more friendly to java developers(I used to think I was biased 🙄 )

rohanKanojia added a commit to rohanKanojia/kubernetes-client that referenced this issue Sep 10, 2020
rohanKanojia added a commit to rohanKanojia/kubernetes-client that referenced this issue Sep 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants