Last active
December 10, 2022 05:31
-
-
Save akhan4u/a050b3e89d43d5dbfef1b92ee9481430 to your computer and use it in GitHub Desktop.
Export Kubernetes namespace to Disk 💾
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
# Prerequisites: | |
# [GNU Parallel](https://www.gnu.org/software/parallel/) | |
# [kubectl-neat](https://github.com/itaysk/kubectl-neat) | |
# [mikefarah/yq](https://github.com/mikefarah/yq) | |
# | |
# Usage: | |
# - Copy and paste the below into your active shell session. Alternatively, add to your shell initialization scripts. | |
# - kubectl_export <namespace> | |
# The folder structure will be created: <namespace>/<kind>/<resource_name>.yaml | |
# | |
# Inspired from https://www.studytonight.com/post/how-to-list-all-resources-in-a-kubernetes-namespace | |
function kubectl_export { | |
NAMESPACE=${1:?"Supply a namespace with this command"} | |
mkdir -p $NAMESPACE | |
kubectl api-resources --verbs=list --namespaced -o name | grep -v "events.events.k8s.io" | grep -v "events" | sort | uniq | parallel --tag --env NAMESPACE -- '\ | |
kubectl get {} -n '$NAMESPACE' 2>&1 | grep -vq "No resources found in '$NAMESPACE' namespace" && \ | |
mkdir -p '$NAMESPACE'/{} && \ | |
kubectl neat get -- {} -n '$NAMESPACE' --ignore-not-found -o yaml | yq --split-exp "\"'$NAMESPACE'/{}/\" + .metadata.name" ".items[]" | |
' | |
} | |
export -f kubectl_export |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment