Skip to content

Commit 808f46b

Browse files
DEVOPS-3090-added print pod logs in validation pipeline (#141)
1 parent f8c5cdf commit 808f46b

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

.pipelines/helm-chart-validation.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,27 @@ steps:
207207
kubectl get deployments -n $(NAMESPACE) -o wide
208208
displayName: 'Verify Deployment'
209209

210+
- script: |
211+
k8s_logs_dir=$(mktemp -d)
212+
echo "##vso[task.setvariable variable=k8s_logs_dir;]$k8s_logs_dir"
213+
./.pipelines/scripts/get_k8s_all_container_logs.sh $(NAMESPACE) $k8s_logs_dir
214+
condition: always()
215+
displayName: Collect k8s logs
216+
217+
- script: |
218+
for file in $(ls "$(k8s_logs_dir)"); do
219+
echo "##[group]$file"
220+
cat "$(k8s_logs_dir)/$file"
221+
echo "##[endgroup]"
222+
done
223+
condition: always()
224+
displayName: Print k8s logs
225+
226+
- publish: $(k8s_logs_dir)
227+
artifact: k8s_logs_$(System.JobAttempt)
228+
condition: always()
229+
displayName: Publish k8s logs
230+
210231
- script: |
211232
set -euo pipefail
212233
helm uninstall $(RELEASE_NAME) -n $(NAMESPACE)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/bin/sh
2+
3+
# Check if exactly two arguments are provided: <namespace> and <output_directory>
4+
if [ $# -ne 2 ]; then
5+
echo "Usage: $0 <namespace> <output_directory>" >&2
6+
exit 1
7+
fi
8+
9+
NAMESPACE="$1"
10+
OUTPUT_DIR="$2"
11+
12+
# Create the output directory if it does not exist
13+
mkdir -p "$OUTPUT_DIR"
14+
if [ ! $? ]; then
15+
echo "Failed to create output directory: $OUTPUT_DIR" >&2
16+
exit 1
17+
fi
18+
19+
# Fetch all pod names in the specified namespace using kubectl
20+
PODS=$(kubectl get pods -n "$NAMESPACE" -o jsonpath='{.items[*].metadata.name}' 2>/dev/null)
21+
if [ ! $? ]; then
22+
echo "Error retrieving pods from namespace: $NAMESPACE" >&2
23+
exit 1
24+
fi
25+
26+
# Exit if no pods found in the namespace
27+
if [ -z "$PODS" ]; then
28+
echo "No pods found in namespace: $NAMESPACE"
29+
exit 0
30+
fi
31+
32+
# Process each pod to collect logs from all containers (regular and init)
33+
for POD in $PODS; do
34+
echo "Processing pod: $POD"
35+
36+
# Fetch init container names for the current pod first
37+
INIT_CONTAINERS=$(kubectl get pod "$POD" -n "$NAMESPACE" -o jsonpath='{.spec.initContainers[*].name}' 2>/dev/null)
38+
if [ ! $? ]; then
39+
echo "Failed to get init containers for pod: $POD" >&2
40+
continue
41+
fi
42+
43+
# Fetch regular container names for the current pod
44+
CONTAINERS=$(kubectl get pod "$POD" -n "$NAMESPACE" -o jsonpath='{.spec.containers[*].name}' 2>/dev/null)
45+
if [ ! $? ]; then
46+
echo "Failed to get containers for pod: $POD" >&2
47+
continue
48+
fi
49+
50+
# Combine init container and regular names into a single list for logs
51+
ALL_CONTAINERS="$INIT_CONTAINERS $CONTAINERS"
52+
53+
# Skip if no containers are found for the pod
54+
if [ -z "$ALL_CONTAINERS" ]; then
55+
echo "No containers or init containers found for pod: $POD"
56+
continue
57+
fi
58+
59+
# Save logs for each init container in the list
60+
for CONTAINER in $ALL_CONTAINERS; do
61+
echo "Fetching logs for container: $CONTAINER"
62+
kubectl logs "$POD" -n "$NAMESPACE" -c "$CONTAINER" >> "${OUTPUT_DIR}/${POD}.log"
63+
if [ ! $? ]; then
64+
echo "Failed to retrieve logs for pod $POD, container $CONTAINER" >&2
65+
fi
66+
done
67+
done
68+
69+
70+
echo "Logs saved to directory: $OUTPUT_DIR"
71+

0 commit comments

Comments
 (0)