Skip to content

Instantly share code, notes, and snippets.

@pit
Last active November 16, 2019 10:53
Show Gist options
  • Save pit/7d96c68e7bdd3e287f71c61637a21f35 to your computer and use it in GitHub Desktop.
Save pit/7d96c68e7bdd3e287f71c61637a21f35 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -e
CONTEXT="$1"
if [[ -z ${CONTEXT} ]]; then
echo "Usage: $0 KUBE-CONTEXT"
exit 1
fi
NAMESPACES=$(kubectl --context ${CONTEXT} get -o json namespaces|jq -r '.items[].metadata.name')
RESOURCES=$(kubectl --context ${CONTEXT} api-resources --no-headers | awk '{ print $1 }')
rm -rf $CONTEXT/*
echo 'Getting resources list to dump'
echo '' > commands_to_run.sh
for ns in ${NAMESPACES};do
for resource in ${RESOURCES};do
rsrcs=$(kubectl --context ${CONTEXT} -n ${ns} get -o json ${resource}|jq -r '.items[].metadata.name')
for r in ${rsrcs};do
dir="${CONTEXT}/${ns}/${resource}"
mkdir -p "${dir}"
echo "Resource: $ns -> $resource -> $r"
echo "kubectl --context ${CONTEXT} -n ${ns} get -o yaml ${resource} ${r} > \"${dir}/${r}.yaml\"" >> commands_to_run.sh
done
done
done
if hash parallel 2>/dev/null; then
echo "Dumping resources in parallel"
parallel --eta commands_to_run.sh
else
echo "Dumping resources one by one"
sh commands_to_run.sh
fi
rm commands_to_run.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment