From 6afb54057accc9977913665f59c3e1ba0d34e2eb Mon Sep 17 00:00:00 2001 From: Will Clark Date: Thu, 12 Sep 2024 09:39:08 +0100 Subject: [PATCH] add k8s log collector (#553) --- .github/workflows/test.yml | 21 +++++++++++++++++++++ resources/scripts/k8s-log-collector.sh | 23 +++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100755 resources/scripts/k8s-log-collector.sh diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d26a10fa5..ac678a67f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -91,6 +91,27 @@ jobs: run: | source .venv/bin/activate ./test/${{matrix.test}} + - name: Collect Kubernetes logs + if: always() + run: | + echo "Installing stern..." + STERN_VERSION="1.30.0" + curl -Lo stern.tar.gz https://github.com/stern/stern/releases/download/v${STERN_VERSION}/stern_${STERN_VERSION}_linux_amd64.tar.gz + tar zxvf stern.tar.gz + chmod +x stern + sudo mv stern /usr/local/bin/ + + # Run script + curl -O https://raw.githubusercontent.com/willcl-ark/warnet/main/resources/scripts/k8s-log-collector.sh + chmod +x k8s-log-collector.sh + ./k8s-log-collector.sh default + - name: Upload log artifacts + if: always() + uses: actions/upload-artifact@v4 + with: + name: kubernetes-logs-${{ matrix.test }} + path: ./k8s-logs + retention-days: 5 test-without-mk: runs-on: ubuntu-latest strategy: diff --git a/resources/scripts/k8s-log-collector.sh b/resources/scripts/k8s-log-collector.sh new file mode 100755 index 000000000..d98c5a38c --- /dev/null +++ b/resources/scripts/k8s-log-collector.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +# Set variables +NAMESPACE=${1:-default} +LOG_DIR="./k8s-logs" +TIMESTAMP=$(date +"%Y%m%d_%H%M%S") + +# Ensure log directory exists +mkdir -p "$LOG_DIR" + +# Collect logs using stern (includes logs from terminated pods) +echo "Collecting stern logs..." +stern "(tank|commander).*" --namespace="$NAMESPACE" --output default --since 1h --no-follow > "$LOG_DIR/${TIMESTAMP}_stern_logs" + +# Collect descriptions of all resources +echo "Collecting resource descriptions..." +kubectl describe all --namespace="$NAMESPACE" > "$LOG_DIR/${TIMESTAMP}_resource_descriptions.txt" + +# Collect events +echo "Collecting events..." +kubectl get events --namespace="$NAMESPACE" --sort-by='.metadata.creationTimestamp' > "$LOG_DIR/${TIMESTAMP}_events.txt" + +echo "Log collection complete. Logs saved in $LOG_DIR"