Skip to content

Instantly share code, notes, and snippets.

@cyxou
Last active December 14, 2022 12:40
Show Gist options
  • Save cyxou/9b6f465d61c0ed40d1a686bf693ba8a0 to your computer and use it in GitHub Desktop.
Save cyxou/9b6f465d61c0ed40d1a686bf693ba8a0 to your computer and use it in GitHub Desktop.
Dump all the Kubernetes objects from cluster
#!/usr/bin/env bash
set -e
NAMESPACE=dev
OBJECTS_TO_DUMP=pv,pvc,configmap,serviceaccount,secret,service,deployment,statefulset,job,cronjob
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)
DUMP_DIR=$SCRIPT_DIR/dump
neat () {
yq --yaml-output 'del(.metadata.annotations,
.metadata.creationTimestamp,
.metadata.generateName,
.metadata.generation,
.metadata.managedFields,
.metadata.namespace,
.metadata.ownerReferences,
.metadata.resourceVersion,
.metadata.selfLink,
.metadata.uid,
.spec.clusterIP,
.spec.clusterIPs,
.spec.externalTrafficPolicy,
.spec.sessionAffinity,
.spec.progressDeadlineSeconds,
.spec.revisionHistoryLimit,
.spec.strategy.rollingUpdate,
.spec.template.metadata.annotations,
.spec.template.metadata.creationTimestamp,
.spec.template.spec.affinity,
.spec.template.spec.dnsPolicy,
.spec.template.spec.restartPolicy,
.spec.template.spec.schedulerName,
.spec.template.spec.securityContext,
.spec.template.spec.terminationGracePeriodSeconds,
.spec.tolerations,
.status)' -
}
neat_deployment () {
yq --yaml-output 'del(.spec.template.spec.containers[].terminationMessagePath,
.spec.template.spec.containers[].terminationMessagePolicy)' -
}
neat_service () {
yq --yaml-output '.spec.type = "ClusterIP"' | yq --yaml-output 'del(.spec.ports[].nodePort)' -
}
for n in $(kubectl get -n $NAMESPACE -o=name $OBJECTS_TO_DUMP)
do
resource=$(dirname $n)
mkdir -p $DUMP_DIR/$resource
if [ "$resource" == "deployment.apps" ]; then
kubectl get -o=yaml -n $NAMESPACE $n | neat | neat_deployment > $DUMP_DIR/$n.yaml
elif [ "$resource" == "service" ]; then
kubectl get -o=yaml -n $NAMESPACE $n | neat | neat_service > $DUMP_DIR/$n.yaml
else
kubectl get -o=yaml -n $NAMESPACE $n | neat > $DUMP_DIR/$n.yaml
fi
done
echo "🎉 All done!"
#!/usr/bin/env bash
set -e
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)
reldir=$0
# yamls=$(find $SCRIPT_DIR/base -type f -name "*.yaml" | grep -v kustomization.yaml)
# Собираем все yaml-файлы в папке base игнорируя папку postgres и файл kustomization.yaml
yamls=$(find $SCRIPT_DIR/base -name 'postgres' -prune -o -name '*.yaml' -print | grep -v kustomization.yaml)
for y in ${yamls[@]}; do
echo $y
cd $SCRIPT_DIR/base
kustomize edit add resource ${y#$SCRIPT_DIR/base/}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment