From 1881ae0230b6c7c42f5baedfb6168ac631431aec Mon Sep 17 00:00:00 2001 From: Hao Liu <44379968+TheRealHaoLiu@users.noreply.github.com> Date: Thu, 23 May 2024 11:52:27 -0400 Subject: [PATCH] Storing each containers log seperately - also add init container log gathering --- .../output_all_container_logs_for_pod.yml | 60 +++++++++++++++---- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/molecule/default/utils/output_all_container_logs_for_pod.yml b/molecule/default/utils/output_all_container_logs_for_pod.yml index d4e32461c..edab531c3 100644 --- a/molecule/default/utils/output_all_container_logs_for_pod.yml +++ b/molecule/default/utils/output_all_container_logs_for_pod.yml @@ -1,15 +1,53 @@ --- -- name: Get all container log in pod - kubernetes.core.k8s_log: - namespace: '{{ namespace }}' - name: '{{ item.metadata.name }}' - all_containers: true - register: all_container_logs - -- name: Store logs in file - ansible.builtin.copy: - content: "{{ all_container_logs.log_lines | join('\n') }}" - dest: '{{ debug_output_dir }}/{{ item.metadata.name }}.log' +- name: Get all container name from pod + set_fact: + container_names: "{{ item.spec.containers | map(attribute='name') | list }}" + when: item.spec.containers is defined + +- name: Get log from all container in pod + block: + - name: Get log from all container in pod + kubernetes.core.k8s_log: + namespace: '{{ namespace }}' + name: '{{ item.metadata.name }}' + container: '{{ container }}' + register: container_log + ignore_errors: true + + - name: Store logs in file + ansible.builtin.copy: + content: "{{ container_log.log_lines | join('\n') }}" + dest: '{{ debug_output_dir }}/{{ item.metadata.name }}-{{ container }}.log' + ignore_errors: true + loop: "{{ container_names }}" + loop_control: + loop_var: container + when: container_names is defined + +- name: Get all init container name from pod if any + set_fact: + init_container_names: "{{ item.spec.initContainers | map(attribute='name') | list }}" + when: item.spec.initContainers is defined + +- name: Get log from all init container in pod + block: + - name: Get log from all init container in pod + kubernetes.core.k8s_log: + namespace: '{{ namespace }}' + name: '{{ item.metadata.name }}' + container: '{{ container }}' + register: container_log + ignore_errors: true + + - name: Store logs in file + ansible.builtin.copy: + content: "{{ container_log.log_lines | join('\n') }}" + dest: '{{ debug_output_dir }}/{{ item.metadata.name }}-{{ container }}.log' + ignore_errors: true + loop: "{{ init_container_names }}" + loop_control: + loop_var: container + when: init_container_names is defined # TODO: all_containser option dump all of the output in a single output make it hard to read we probably should iterate through each of the container to get specific logs # also we should probably investigate toolings to do OpenShift style sosreport/must-gather for kind cluster or switch to microshift where sosreport is supported