Last active
March 27, 2025 22:07
-
-
Save samba/c0e2ac0eda825b9e08041079e138641e to your computer and use it in GitHub Desktop.
Dump cluster state to YAML
This file contains 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
#!/usr/bin/env bash | |
# USAGE | |
# bash kubescan.sh kubeconfig.yaml > state.yaml | |
# Goal: | |
# - Provide a quick way to dump the state of a cluster for analysis | |
# - Simplify verification of live state of the cluster | |
KUBECONFIG=${1:-${KUBECONFIG}} | |
shift; | |
# NB: this should run AFTER the KUBECONFIG default above, in case environment does not provide KUBECONFIG | |
set -euo pipefail | |
fail () { | |
echo "$2" >&2 | |
exit $1 | |
} | |
test -f ${KUBECONFIG} || fail 1 "Provide a valid kubeconfig file" | |
kubedo () { | |
kubectl --kubeconfig=${KUBECONFIG} -o jsonpath='{range .items[*]}{.kind}{"\t| "}{.metadata.namespace}{"\t| "}{.metadata.name}{"\t| "}{.metadata.deletionTimestamp}{"\n"}{end}' "${@}" | |
} | |
yaml_doc () { | |
echo '# --- # BEGIN ' $1 | |
cat | |
echo '# ... # END ' $1 | |
} | |
while read t; do | |
echo "Reading type: ${t}" >&2 | |
( kubedo get ${t} --all-namespaces || true) | yaml_doc "${t}" | |
done < <(kubectl api-resources --verbs=list --namespaced=false -o name || true) | |
while read t; do | |
echo "Reading type: ${t}" >&2 | |
( kubedo get ${t} --all-namespaces || true) | yaml_doc "${t}" | |
done < <(kubectl api-resources --verbs=list --namespaced=true -o name || true) | |
This file contains 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
#!/usr/bin/env bash | |
# USAGE | |
# bash kubescan.sh kubeconfig.yaml > state.yaml | |
# Goal: | |
# - Provide a quick way to dump the state of a cluster for analysis | |
# - Simplify verification of live state of the cluster | |
KUBECONFIG=${1:-${KUBECONFIG}} | |
shift; | |
# NB: this should run AFTER the KUBECONFIG default above, in case environment does not provide KUBECONFIG | |
set -euo pipefail | |
fail () { | |
echo "$2" >&2 | |
exit $1 | |
} | |
test -f ${KUBECONFIG} || fail 1 "Provide a valid kubeconfig file" | |
kubedo () { | |
kubectl --kubeconfig=${KUBECONFIG} -o yaml "${@}" | |
} | |
yaml_doc () { | |
echo '%YAML 1.1' | |
echo '--- #' $1 | |
cat | |
echo '...' | |
} | |
while read t; do | |
echo "Reading type: ${t}" >&2 | |
kubedo get ${t} | yaml_doc "${t}" | |
done < <(kubectl api-resources --verbs=list --namespaced=false -o name) | |
while read t; do | |
echo "Reading type: ${t}" >&2 | |
kubedo get ${t} --all-namespaces | yaml_doc "${t}" | |
done < <(kubectl api-resources --verbs=list --namespaced=true -o name) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment