From 13a6ebfd2a0756f472c736b35caac0e0cf048a7f Mon Sep 17 00:00:00 2001 From: Alexander Markov Date: Thu, 25 Sep 2025 16:07:11 +0700 Subject: [PATCH 1/3] collect pod logs --- .pipelines/helm-chart-validation.yml | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/.pipelines/helm-chart-validation.yml b/.pipelines/helm-chart-validation.yml index 0b011ba..c91e835 100644 --- a/.pipelines/helm-chart-validation.yml +++ b/.pipelines/helm-chart-validation.yml @@ -198,6 +198,27 @@ steps: kubectl get deployments -n $(NAMESPACE) -o wide displayName: 'Verify Deployment' +- script: | + k8s_logs_dir=$(mktemp -d) + echo "##vso[task.setvariable variable=k8s_logs_dir;]$k8s_logs_dir" + ./pipelines/get_k8s_all_container_logs.sh $(NAMESPACE) $k8s_logs_dir + condition: always() + displayName: Collect k8s logs + +- script: | + for file in $(ls "$(k8s_logs_dir)"); do + echo "##[group]$file" + cat "$(k8s_logs_dir)/$file" + echo "##[endgroup]" + done + condition: always() + displayName: Print k8s logs + +- publish: $(k8s_logs_dir) + artifact: k8s_logs_$(System.JobAttempt) + condition: always() + displayName: Publish k8s logs + - script: | set -euo pipefail helm uninstall $(RELEASE_NAME) -n $(NAMESPACE) From 6497cec6479b9af21f667b4279d71b6182a4d32a Mon Sep 17 00:00:00 2001 From: Alexander Markov Date: Thu, 25 Sep 2025 16:17:19 +0700 Subject: [PATCH 2/3] forgot commit the script --- .pipelines/helm-chart-validation.yml | 2 +- .../scripts/get_k8s_all_container_logs.sh | 71 +++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100755 .pipelines/scripts/get_k8s_all_container_logs.sh diff --git a/.pipelines/helm-chart-validation.yml b/.pipelines/helm-chart-validation.yml index c91e835..ecf6709 100644 --- a/.pipelines/helm-chart-validation.yml +++ b/.pipelines/helm-chart-validation.yml @@ -201,7 +201,7 @@ steps: - script: | k8s_logs_dir=$(mktemp -d) echo "##vso[task.setvariable variable=k8s_logs_dir;]$k8s_logs_dir" - ./pipelines/get_k8s_all_container_logs.sh $(NAMESPACE) $k8s_logs_dir + ./pipelines/scripts/get_k8s_all_container_logs.sh $(NAMESPACE) $k8s_logs_dir condition: always() displayName: Collect k8s logs diff --git a/.pipelines/scripts/get_k8s_all_container_logs.sh b/.pipelines/scripts/get_k8s_all_container_logs.sh new file mode 100755 index 0000000..f2dd732 --- /dev/null +++ b/.pipelines/scripts/get_k8s_all_container_logs.sh @@ -0,0 +1,71 @@ +#!/bin/sh + +# Check if exactly two arguments are provided: and +if [ $# -ne 2 ]; then + echo "Usage: $0 " >&2 + exit 1 +fi + +NAMESPACE="$1" +OUTPUT_DIR="$2" + +# Create the output directory if it does not exist +mkdir -p "$OUTPUT_DIR" +if [ ! $? ]; then + echo "Failed to create output directory: $OUTPUT_DIR" >&2 + exit 1 +fi + +# Fetch all pod names in the specified namespace using kubectl +PODS=$(kubectl get pods -n "$NAMESPACE" -o jsonpath='{.items[*].metadata.name}' 2>/dev/null) +if [ ! $? ]; then + echo "Error retrieving pods from namespace: $NAMESPACE" >&2 + exit 1 +fi + +# Exit if no pods found in the namespace +if [ -z "$PODS" ]; then + echo "No pods found in namespace: $NAMESPACE" + exit 0 +fi + +# Process each pod to collect logs from all containers (regular and init) +for POD in $PODS; do + echo "Processing pod: $POD" + + # Fetch init container names for the current pod first + INIT_CONTAINERS=$(kubectl get pod "$POD" -n "$NAMESPACE" -o jsonpath='{.spec.initContainers[*].name}' 2>/dev/null) + if [ ! $? ]; then + echo "Failed to get init containers for pod: $POD" >&2 + continue + fi + + # Fetch regular container names for the current pod + CONTAINERS=$(kubectl get pod "$POD" -n "$NAMESPACE" -o jsonpath='{.spec.containers[*].name}' 2>/dev/null) + if [ ! $? ]; then + echo "Failed to get containers for pod: $POD" >&2 + continue + fi + + # Combine init container and regular names into a single list for logs + ALL_CONTAINERS="$INIT_CONTAINERS $CONTAINERS" + + # Skip if no containers are found for the pod + if [ -z "$ALL_CONTAINERS" ]; then + echo "No containers or init containers found for pod: $POD" + continue + fi + + # Save logs for each init container in the list + for CONTAINER in $ALL_CONTAINERS; do + echo "Fetching logs for container: $CONTAINER" + kubectl logs "$POD" -n "$NAMESPACE" -c "$CONTAINER" >> "${OUTPUT_DIR}/${POD}.log" + if [ ! $? ]; then + echo "Failed to retrieve logs for pod $POD, container $CONTAINER" >&2 + fi + done +done + + +echo "Logs saved to directory: $OUTPUT_DIR" + From 69d64b1a516bfbc070318e663fe1a1c75f852c08 Mon Sep 17 00:00:00 2001 From: Alexander Markov Date: Thu, 25 Sep 2025 16:27:28 +0700 Subject: [PATCH 3/3] fix path --- .pipelines/helm-chart-validation.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pipelines/helm-chart-validation.yml b/.pipelines/helm-chart-validation.yml index ecf6709..be3008c 100644 --- a/.pipelines/helm-chart-validation.yml +++ b/.pipelines/helm-chart-validation.yml @@ -201,7 +201,7 @@ steps: - script: | k8s_logs_dir=$(mktemp -d) echo "##vso[task.setvariable variable=k8s_logs_dir;]$k8s_logs_dir" - ./pipelines/scripts/get_k8s_all_container_logs.sh $(NAMESPACE) $k8s_logs_dir + ./.pipelines/scripts/get_k8s_all_container_logs.sh $(NAMESPACE) $k8s_logs_dir condition: always() displayName: Collect k8s logs