Skip to content

Instantly share code, notes, and snippets.

@samba
Last active May 22, 2019 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save samba/c0e2ac0eda825b9e08041079e138641e to your computer and use it in GitHub Desktop.
Save samba/c0e2ac0eda825b9e08041079e138641e to your computer and use it in GitHub Desktop.
Dump cluster state to YAML
#!/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