Skip to content

Instantly share code, notes, and snippets.

@vadikgo
Created March 18, 2019 11:11
Show Gist options
  • Save vadikgo/908b24501a59fc7aa25aa849cdb1f345 to your computer and use it in GitHub Desktop.
Save vadikgo/908b24501a59fc7aa25aa849cdb1f345 to your computer and use it in GitHub Desktop.
Clean failed kubernetes pod
#!/bin/bash
export PATH=$PATH:/usr/local/bin
namespaces=(jenkins worker gitlab)
for ns in "${namespaces[@]}"
do
kubectl get pods -n "${ns}" --show-all --no-headers=true --field-selector=status.phase=Failed -o=custom-columns=NAME:.metadata.name | xargs -I {} -P 8 kubectl delete pod -n "${ns}" {}
kubectl get pods -n "${ns}" --show-all --no-headers=true --field-selector=status.phase=Succeeded -o=custom-columns=NAME:.metadata.name | xargs -I {} -P 8 kubectl delete pod -n "${ns}" {}
kubectl get pods -n "${ns}" --show-all --no-headers=true -o json | jq -r '.items[] | select([.status.containerStatuses[] | select(.state.terminated.reason == "Error")] | length == 1) | .metadata.name' | xargs -I {} -P 8 kubectl delete pod -n "${ns}" {}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment