Skip to content

Instantly share code, notes, and snippets.

@sneako
Last active July 11, 2022 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sneako/663789801eb94cafc82e09fd737fec75 to your computer and use it in GitHub Desktop.
Save sneako/663789801eb94cafc82e09fd737fec75 to your computer and use it in GitHub Desktop.
Dump Kubernetes state as yaml
#!/usr/bin/env bash
# set -x
mkdir output
KUBECONFIG=/Users/nico/real-kubeconfig
SKIP_RESOURCES=(
apiservices
bindings
blockaffinities
certificaterequests
certificatesigningrequests
challenges
clusterinformations
componentstatuses
controllerrevisions
csidrivers
csinodes
customresourcedefinitions
endpoints
events
felixconfigurations
globalnetworkpolicies
globalnetworksets
hostendpoints
ipamblocks
ipamconfigs
ipamhandles
ippools
limitranges
localsubjectaccessreviews
namespaces
flowschemas
prioritylevelconfigurations
tlscertificatedelegations
ingressclasses
nodes
orders
persistentvolumeclaims
persistentvolumes
pods
priorityclasses
selfsubjectaccessreviews
selfsubjectrulesreviews
subjectaccessreviews
volumeattachments
)
SKIP_NAMESPACES=(
cert-manager
)
k8s() {
kubectl "--kubeconfig=$KUBECONFIG" "$@"
}
# get the first column of the output, skipping the header row
first_col() {
awk '{if (NR!=1) {print $1}}' "$1"
}
NAMESPACES=$(k8s get ns | first_col | sort | uniq)
RESOURCES=$(k8s api-resources | first_col | sort | uniq)
for resource in $RESOURCES
do
if [[ " ${SKIP_RESOURCES[*]} " =~ " $resource " ]]; then
echo "skipping $resource"
continue
fi
for namespace in $NAMESPACES
do
if [[ " ${SKIP_NAMESPACES[*]} " =~ " $namespace " ]]; then
echo "skipping $namespace"
continue
fi
path="output/$namespace/$resource"
mkdir -p "$path"
NS_RESOURCES=$(k8s -n "$namespace" get "$resource" | first_col)
for ns_resource in $NS_RESOURCES
do
k8s -n "$namespace" get "$resource" "$ns_resource" -o yaml > "$path/$ns_resource.yml"
done
done
done
@sneako
Copy link
Author

sneako commented Jul 10, 2022

This is very slow and takes a long time to run! You may want adjust the skipped resources and namespaces

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment