Skip to content

Instantly share code, notes, and snippets.

@jenrik
Last active February 6, 2025 09:31
Show Gist options
  • Save jenrik/61263668f2137cc957cd1af6530fc6b2 to your computer and use it in GitHub Desktop.
Save jenrik/61263668f2137cc957cd1af6530fc6b2 to your computer and use it in GitHub Desktop.
Find PDBs guarding single pods
#!/bin/sh
set -e
if ! type jq 2>/dev/null 1>/dev/null; then
echo "jq command is required, please install it" >&2
exit 1
fi
kubectl get pdb -A -o json | \
jq -r '.items[] | {name: .metadata.name, namespace: .metadata.namespace, selector: ((.spec.selector.matchLabels // {} | to_entries | .[] |= "\(.key)=\(.value)") + (.spec.selector.matchExpressions // [] | .[] |= .key + " " + (.operator | ascii_downcase) + " (" + (.values[] | @json) + ")")) | join(",") }' -c | \
while read -r pdb; do
export pdb
name="$(printenv pdb | jq '.name')"
namespace="$(printenv pdb | jq '.namespace')"
selector="$(printenv pdb | jq '.selector')"
if [ "$(kubectl -n "$namespace" get pod -l "$selector" -o go-template='{{ len .items }}' 2>/dev/null)" -eq 1 ]; then
echo "Found bad PDB in namespace $namespace by the name $name"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment