From 5662fa777c1716564284b33cb2250890b3f83695 Mon Sep 17 00:00:00 2001 From: Joshua Eason Date: Tue, 26 Apr 2022 11:31:44 -0400 Subject: [PATCH] Adding previous container log support (#436) Adding previous container log support Signed-off-by: Joshua Eason josh.eason@anchore.com SUMMARY Adds support for the previous parameter in kubectl logs. This allows for the retrieval of the previously terminated containers logs which is useful for troubleshooting. ISSUE TYPE Feature Pull Request COMPONENT NAME k8s_log ADDITIONAL INFORMATION Adds the previous parameter (bool) to k8s_log module. This matches the documentation for kubectl logs --previous parameter. This parameter allows for retrieving the previously terminated containers logs. Output of the module is identical with the exception being the logs returned are from the previously terminated container. Reviewed-by: Mike Graves Reviewed-by: Abhijeet Kasurde Reviewed-by: Joshua Eason --- .../437-k8s-add-support-for-previous-logs.yaml | 3 +++ plugins/modules/k8s_log.py | 13 +++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 changelogs/fragments/437-k8s-add-support-for-previous-logs.yaml diff --git a/changelogs/fragments/437-k8s-add-support-for-previous-logs.yaml b/changelogs/fragments/437-k8s-add-support-for-previous-logs.yaml new file mode 100644 index 00000000..3328e1af --- /dev/null +++ b/changelogs/fragments/437-k8s-add-support-for-previous-logs.yaml @@ -0,0 +1,3 @@ +--- +minor_changes: +- k8s_log - added the `previous` parameter for retrieving the previously terminated pod logs (https://github.com/ansible-collections/kubernetes.core/issues/437). diff --git a/plugins/modules/k8s_log.py b/plugins/modules/k8s_log.py index 0ba77912..db63e14b 100644 --- a/plugins/modules/k8s_log.py +++ b/plugins/modules/k8s_log.py @@ -60,6 +60,13 @@ options: required: no type: str version_added: '2.2.0' + previous: + description: + - If C(true), print the logs for the previous instance of the container in a pod if it exists. + required: no + type: bool + default: False + version_added: '2.4.0' requirements: - "python >= 3.6" @@ -138,6 +145,7 @@ def argspec(): container=dict(), since_seconds=dict(), label_selectors=dict(type="list", elements="str", default=[]), + previous=dict(type="bool", default=False), ) ) return args @@ -185,6 +193,11 @@ def execute_module(module, k8s_ansible_mixin): {"sinceSeconds": module.params["since_seconds"]} ) + if module.params.get("previous"): + kwargs.setdefault("query_params", {}).update( + {"previous": module.params["previous"]} + ) + log = serialize_log( resource.log.get(name=name, namespace=namespace, serialize=False, **kwargs) )