Last active
August 14, 2017 17:27
-
-
Save iMartyn/e9c44195abd38918991424c7bfe5bef0 to your computer and use it in GitHub Desktop.
Dump kubernetes cluster into folders containing yaml files
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
#!/bin/bash | |
CALLEDFROM=${PWD} | |
SEARCHFOR='^- apiVersion.*' | |
OPTIND=1 | |
NAMESPACED=0 | |
while getopts "n" opt; do | |
case "${opt}" in | |
n) | |
NAMESPACED=1 | |
;; | |
h\?) | |
echo "Only option is -n, to put files in namespaced directories" | |
exit 0 | |
;; | |
esac | |
done | |
if [[ "$NAMESPACED" == "1" ]]; then | |
NSARG="-n" | |
fi | |
ALLYAML=$(mktemp) | |
kubectl get all --all-namespaces --export -o yaml > ${ALLYAML} | |
${CALLEDFROM}/splityaml.bash -f ${ALLYAML} ${NSARG} -i 2 -x -r ${SEARCHFOR} | |
find -name '*.yaml' -exec sed -i s/'^- apiVersion'/'apiVersion'/ \{\} \; | |
rm ${ALLYAML} | |
rm object-0000 |
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
#!/bin/bash | |
OPTIND=1 | |
NAMESPACED=0 | |
EXTRAINDENT=0 | |
while getopts "hnxf:r:i:" opt; do | |
case "${opt}" in | |
n) | |
NAMESPACED=1 | |
;; | |
h) | |
echo "splityaml -f inputfile.yaml -r {{regexp to split on, default '^---$'}} [-n]" | |
echo " -n : put files in namespaced directories" | |
exit 0 | |
;; | |
f) | |
INPUTYAML=${OPTARG} | |
;; | |
r) | |
SEARCHFOR=${OPTARG} | |
;; | |
i) | |
EXTRAINDENT=${OPTARG} | |
;; | |
x) | |
REMOVEEXTRAINDENT=1 | |
;; | |
esac | |
done | |
if [[ ! -f $INPUTYAML ]]; then | |
echo "Input file '${INPUTYAML}' does not exist!" | |
exit 1 | |
fi | |
if [[ "${SEARCHFOR}" == "" ]]; then | |
SEARCHFOR="^---$" | |
fi | |
CSPLITSEARCH="/${SEARCHFOR}/" | |
if [[ ${EXTRAINDENT} -gt 0 ]]; then | |
EXTRASPACES=$(eval printf '.%.0s' {1..`echo ${EXTRAINDENT}`} | tr '.' ' ') | |
echo "extra indent (${EXTRAINDENT}) is (${EXTRASPACES})" | |
fi | |
if [[ ${REMOVEEXTRAINDENT} -gt 0 ]]; then | |
if [[ ${EXTRAINDENT} -lt 1 ]]; then | |
echo "Can't remove an indent of < 1!" | |
exit 1 | |
fi | |
fi | |
COUNT=$(expr $(grep -c -- "${SEARCHFOR}" ${INPUTYAML}) - 1) | |
if [[ ${COUNT} -lt 1 ]]; then | |
echo grep -c -- "${SEARCHFOR}" ${INPUTYAML} | |
echo "No matches found!" | |
exit 2 | |
fi | |
echo csplit ${INPUTYAML} "${CSPLITSEARCH}" {${COUNT}} --prefix object --suffix-format -%04d.yaml -q | |
csplit -f object- -n 4 ${INPUTYAML} "${CSPLITSEARCH}" {${COUNT}} | |
for EXPORTEDFILE in object-*; do | |
echo "Processing ${EXPORTEDFILE}..." | |
KIND=$(grep "^${EXTRASPACES}kind:" ${EXPORTEDFILE} | sed s/"^.*kind: *"//) | |
NAME=$(grep "^${EXTRASPACES} name:" ${EXPORTEDFILE} | sed s/"^.*name: *"//) | |
NAMESPACE=$(grep "^${EXTRASPACES} namespace:" ${EXPORTEDFILE} | sed s/"^.*namespace: *"//) | |
if [[ ${NAMESPACED} -eq 1 ]]; then | |
FINAL_DIR_NAME=${NAMESPACE}/${KIND} | |
FINAL_NAME=${FINAL_DIR_NAME}/${NAME}.yaml | |
else | |
FINAL_DIR_NAME=${KIND} | |
FINAL_NAME=${FINAL_DIR_NAME}/${NAME}.yaml | |
fi | |
echo "Should be $FINAL_NAME" | |
if [[ "${NAME}" != "" ]]; then | |
mkdir -p $FINAL_DIR_NAME 2>/dev/null | |
if [[ ${REMOVEEXTRAINDENT} -gt 0 ]]; then | |
sed -i "s/^${EXTRASPACES}//" ${EXPORTEDFILE} | |
fi | |
cp ${EXPORTEDFILE} ${FINAL_NAME} | |
rm ${EXPORTEDFILE} | |
fi | |
done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment