Retrieves logs from a Kubernetes pod and container.
Args:
pod_name (str): The name of the pod.
namespace (str): The namespace of the pod.
container_name (str, optional): The name of the container within the pod.
If None, defaults to the first container.
tail (int, optional): Number of lines to return from the end of the log.
If None, defaults to the last 1000 lines. Pass a
larger value to retrieve more history, or a small
value for a bounded recent slice.
since_seconds (int, optional): If set, only return logs newer than this
many seconds. Combines with `tail` (both limits
apply).
previous (bool, default False): If True, return logs from the *previous*
terminated instance of the container instead of the
current one. Indispensable for crashloop analysis,
where the current instance's logs are empty or
post-restart. Fails if there is no previous instance.
Three Kubernetes behaviors make `previous=True`
look as though it were ignored or broken; none is a
fault in this tool, and all are worth knowing before
drawing a conclusion from the output:
* While a container sits in CrashLoopBackOff there
is no running instance, so the kubelet serves the
most recently *terminated* one for `previous=False`
as well - both calls then return the same text.
Check the container's state with
`get_pod_container_statuses` before concluding the
flag had no effect.
* A restart between two calls shifts the window: the
instance that was current becomes the previous one,
so a container crash-looping every few seconds can
legitimately return identical bytes for both.
Compare the log timestamps, not just the flag.
* Once the previous instance's log file has been
reclaimed, the API answers 200 with the text
`unable to retrieve container logs for <id>` -
a successful call whose body is an error message,
not a raised error.
Only the single most recent terminated instance is
retained by the kubelet; there is no way to reach
further back than one.
Returns:
str: The log content as text, with real newlines. An empty string if the
container has produced no log output (never None).
Raises
------
K8sConfigError
If unable to initialize the K8S API.
K8sApiError
If the API call to fetch logs fails or an unexpected error occurs.