Created
May 2, 2025 14:28
-
-
Save jbogard/fd8594fa754aaf7ba783ab3703eed0c9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
echo "k8s can die in a 🔥 after being consumed and 💩 out by satan after eating a triple-spicy kebab" | |
echo "$NAMESPACE" | |
echo "$DEPLOYMENT" | |
# Get latest replica set version | |
rev="`kubectl get deployment -n $NAMESPACE $DEPLOYMENT -o jsonpath=\"{.metadata.annotations.deployment\.kubernetes\.io/revision}\"`" | |
echo "Latest replica set version: $rev" | |
# Get replica set name from latest version | |
rs=$(kubectl get rs -n mizeees-dev -o json | \ | |
jq -r --arg REV "$rev" --arg OWNER "$DEPLOYMENT" '.items[] | select(.metadata.annotations["deployment.kubernetes.io/revision"] == $REV and .metadata.ownerReferences[].name == $OWNER) | .metadata.name') | |
echo "Replica set name: $rs" | |
# Get label selector from the ReplicaSet | |
selector=$(kubectl get rs $rs -n $NAMESPACE -o json | jq -r '.spec.selector.matchLabels | to_entries | map("\(.key)=\(.value)") | join(",")') | |
echo "Selector: $selector" | |
phase_pending=true | |
has_crashloop=false | |
status_ready=false | |
while [[ ("$phase_pending" == true || "$status_ready" == false) && ! "$has_crashloop" == true ]]; do | |
sleep 1 | |
phases=$(kubectl get pods -l "$selector" -n $NAMESPACE -o jsonpath='{range .items[*]}{.status.phase}{";"}{end}') | |
phase_pending=$(echo "$phases" | grep -q "Pending" && echo true || echo false) | |
echo "Phases: $phases" | |
# Only check init container status if pods are still pending (trying to start) | |
if [ "$phase_pending" == true ]; then | |
# Getting statuses | |
statuses=$(kubectl get pods -l "$selector" -n $NAMESPACE -o jsonpath='{range .items[*]}{range .status.initContainerStatuses[*]}{.state.waiting.reason}{";"}{end}') | |
echo "Init Container Statuses: $statuses" | |
if [ -n "$statuses" ]; then | |
# Check for CrashLoopBackOff in any init containers | |
has_crashloop=$(echo "$statuses" | grep -q "CrashLoopBackOff" && echo true || echo false) | |
echo "Init Container CrashLoopBackOff detected: $has_crashloop" | |
fi | |
else # Check container statuses | |
# Getting statuses | |
statuses=$(kubectl get pods -l "$selector" -n $NAMESPACE -o jsonpath='{range .items[*]}{range .status.containerStatuses[*]}{.state.waiting.reason}{";"}{end}') | |
echo "Container Waiting Reasons: $statuses" | |
if [ -n "$statuses" ]; then | |
# Check for CrashLoopBackOff in any containers | |
has_crashloop=$(echo "$statuses" | grep -q "CrashLoopBackOff" && echo true || echo false) | |
echo "Container CrashLoopBackOff detected: $has_crashloop" | |
fi | |
# If no crash loop detected, check if all containers are ready | |
if [ "$has_crashloop" == false ]; then | |
ready_statuses=$(kubectl get pods -l "$selector" -n $NAMESPACE -o jsonpath='{range .items[*]}{range .status.containerStatuses[*]}{.ready}{";"}{end}') | |
echo "Ready statuses: $ready_statuses" | |
# Create array from statutes | |
IFS=';' read -ra ready_status_arr <<<"$ready_statuses" | |
status_ready=true | |
for ready_status in "${ready_status_arr[@]}"; do | |
if [[ "$ready_status" != true && "$status_ready" == true ]]; then | |
echo "Detected non-true ready status: $ready_status" | |
status_ready=false | |
fi | |
done | |
echo "All containers ready: $status_ready" | |
fi | |
fi | |
done | |
if [[ "$has_crashloop" == true ]]; then | |
echo "❌ One or more pods are in CrashLoopBackOff" | |
exit 1 | |
else | |
echo "✅ No CrashLoopBackOff detected" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment