Dump Kubernetes cluster namespace resources as YAML

Home   »   Dump Kubernetes cluster namespace resources as YAML

#!/usr/bin/env bash
set -e

NAMESPACE="$1"

if [[ -z ${NAMESPACE} ]]; then
  echo "Usage: $0 NAMESPACE"
  exit 1
fi

# Dump file
DUMP_FILE="dump-${NAMESPACE}.yaml"
# Delete if existing
if [[ -f "$DUMP_FILE" ]]; then
    rm $DUMP_FILE
fi


# HARD-CODED TYPES
TYPES="configmap secret deployment service ingress"

# DYNAMICALLY-READ TYPES
# types=$(kubectl api-resources --verbs=list --namespaced -o name)

for type in ${TYPES};do
  resources=$(kubectl -n ${NAMESPACE} get -o json ${type} | jq ".items[].metadata.name" | sed "s/\"//g")
  for resource in ${resources};do
    echo "# ${type}: ${resource}" >> ${DUMP_FILE}
    kubectl -n ${NAMESPACE} get -o yaml ${type} ${resource} >> ${DUMP_FILE}
    echo "---" >> ${DUMP_FILE}
  done
done

Leave a Reply

Your email address will not be published. Required fields are marked *