Skip to content

Instantly share code, notes, and snippets.

@rms1000watt
Created November 15, 2019 23:11
Show Gist options
  • Save rms1000watt/640384b0d671add118954b6bc29431bf to your computer and use it in GitHub Desktop.
Save rms1000watt/640384b0d671add118954b6bc29431bf to your computer and use it in GitHub Desktop.
Kubernetes save all deployed resources to file
#!/usr/bin/env bash
# Usage:
# ./svc-to-file.sh hello-world dev
if [[ -z $1 ]]; then
echo "ERROR: No service name provided"
exit 1
fi
if [[ -z $2 ]]; then
echo "ERROR: No namespace provided"
exit 1
fi
name=$1
ns=$2
fname="${name}.${ns}.yaml"
if [[ -f ${fname} ]]; then
echo "ERROR: File already exists: fname=${fname}"
exit 1
fi
echo "Finding and saving everything related to: name=${name} ns=${ns}"
types=(ing svc deployment cm hpa)
for t in ${types[@]}; do
for v in $(kubectl get "${t}" -n "${ns}" -o jsonpath="{.items[*].metadata.name}"); do
if ! echo ${v} | grep -q "${name}"; then continue; fi
echo "kubectl get ${t} ${v} -n ${ns} -o yaml"
echo "---" >> "${fname}"
kubectl get "${t}" "${v}" -n "${ns}" -o yaml >> "${fname}"
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment