From 51027a94d5dd8355b84280916d2b05583e4852da Mon Sep 17 00:00:00 2001 From: jmarchel Date: Fri, 22 Sep 2023 12:36:11 +0200 Subject: [PATCH] Add role for collecting pods logs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I83f86839d15e61f815428ff7218b2a0a685c6791 Co-Authored-By: Radosław Piliszek --- playbooks/kubernetes/post.yaml | 3 ++ .../defaults/main.yaml | 2 ++ .../collect-pods-logs-for-namespace.yaml | 19 ++++++++++++ .../tasks/main.yaml | 30 +++++++++++++++++++ zuul.d/jobs.yaml | 1 + 5 files changed, 55 insertions(+) create mode 100644 playbooks/kubernetes/post.yaml create mode 100644 roles/collect-kubernetes-pods-logs/defaults/main.yaml create mode 100644 roles/collect-kubernetes-pods-logs/tasks/collect-pods-logs-for-namespace.yaml create mode 100644 roles/collect-kubernetes-pods-logs/tasks/main.yaml diff --git a/playbooks/kubernetes/post.yaml b/playbooks/kubernetes/post.yaml new file mode 100644 index 0000000..423e888 --- /dev/null +++ b/playbooks/kubernetes/post.yaml @@ -0,0 +1,3 @@ +- hosts: all + roles: + - collect-kubernetes-pods-logs diff --git a/roles/collect-kubernetes-pods-logs/defaults/main.yaml b/roles/collect-kubernetes-pods-logs/defaults/main.yaml new file mode 100644 index 0000000..2d8f4c0 --- /dev/null +++ b/roles/collect-kubernetes-pods-logs/defaults/main.yaml @@ -0,0 +1,2 @@ +collect_kubernetes_pods_logs_all_namespaces: false +collect_kubernetes_pods_logs_namespaces: [""] diff --git a/roles/collect-kubernetes-pods-logs/tasks/collect-pods-logs-for-namespace.yaml b/roles/collect-kubernetes-pods-logs/tasks/collect-pods-logs-for-namespace.yaml new file mode 100644 index 0000000..f787d6e --- /dev/null +++ b/roles/collect-kubernetes-pods-logs/tasks/collect-pods-logs-for-namespace.yaml @@ -0,0 +1,19 @@ +- name: List pods for namespace + command: "kubectl get pod {% if namespace != '' %}-n {{ namespace }}{% endif %} -o=custom-columns=NAME:.metadata.name --no-headers" + register: zj_pods + failed_when: false + +- name: Create pods logs dir for namespace + file: + path: "{{ ansible_user_dir }}/zuul-output/logs/pods_logs/{{ namespace }}" + state: directory + mode: 0755 + +- name: Save pods logs for namespace + loop: "{{ zj_pods.stdout_lines | default([]) }}" + loop_control: + loop_var: zj_pod_name + shell: "kubectl logs {{ zj_pod_name }} --all-containers=true {% if namespace != '' %}-n {{ namespace }}{% endif %} &> {{ ansible_user_dir }}/zuul-output/logs/pods_logs/{{ namespace }}/{{ zj_pod_name }}.txt" + args: + executable: /bin/bash + failed_when: false diff --git a/roles/collect-kubernetes-pods-logs/tasks/main.yaml b/roles/collect-kubernetes-pods-logs/tasks/main.yaml new file mode 100644 index 0000000..c0dfc47 --- /dev/null +++ b/roles/collect-kubernetes-pods-logs/tasks/main.yaml @@ -0,0 +1,30 @@ +- name: Create pods logs dir + file: + path: "{{ ansible_user_dir }}/zuul-output/logs/pods_logs" + state: directory + mode: 0755 + +- name: List namespaces + command: "kubectl get namespaces -o=custom-columns=NAME:.metadata.name --no-headers" + register: zj_kubectl_get_namespaces + failed_when: false + when: collect_kubernetes_pods_logs_all_namespaces + +- name: Save namespaces to a fact + set_fact: + collect_kubernetes_pods_logs_namespaces: "{{ zj_kubectl_get_namespaces.stdout_lines }}" + when: collect_kubernetes_pods_logs_all_namespaces + +- name: Collect pods logs for namespace + include_tasks: collect-pods-logs-for-namespace.yaml + vars: + namespace: "{{ zj_namespace }}" + loop: "{{ collect_kubernetes_pods_logs_namespaces }}" + loop_control: + loop_var: zj_namespace + +- name: Open pods logs permissions + file: + dest: "{{ ansible_user_dir }}/zuul-output/logs/pods_logs" + mode: u=rwX,g=rX,o=rX + recurse: true diff --git a/zuul.d/jobs.yaml b/zuul.d/jobs.yaml index ddfeba8..01e144d 100644 --- a/zuul.d/jobs.yaml +++ b/zuul.d/jobs.yaml @@ -22,6 +22,7 @@ roles: - zuul: zuul/zuul-jobs pre-run: playbooks/helm/pre.yaml + post-run: playbooks/kubernetes/post.yaml vars: helm_version: *helm_version kubernetes_runtime: cri-o