#!/bin/bash

namespace=命名空间

install_yq() {
  wget https://oss.demo.uavcmlc.com/cmlc-installation/downloadfile/amd/yq_linux_amd64 -O /usr/local/bin/yq
#  wget https://oss.demo.uavcmlc.com/cmlc-installation/downloadfile/aarch/yq_linux_arm64 -O /usr/local/bin/yq
  chmod +x /usr/local/bin/yq

  echo ""
}

backup_all_deployment() {

  echo "start to back up all deployments "

  kubectl get deployments -n ${namespace} -o yaml | yq eval '.items[] | del( .metadata.managedFields, .metadata.annotations, .metadata.generation, .metadata.creationTimestamp, .metadata.uid, .metadata.resourceVersion, .status)' >all-deployment-$namespace.yaml
  sed -i '/^apiVersion:/i ---' all-deployment-$namespace.yaml
  echo " done !"
  echo ""
}

backup_all_service() {
  echo "start to back up all services "
  kubectl get services -n ${namespace} -o yaml | yq eval '.items[] | del(.metadata.annotations,.metadata.managedFields, .metadata.selfLink, .metadata.creationTimestamp, .metadata.uid, .metadata.resourceVersion, .status)' - >all-service-$namespace.yaml
  sed -i '/^apiVersion:/i ---' all-service-$namespace.yaml
  echo " done !"
  echo ""
}

backup_all_configmap() {
  echo "start to back up all configmaps "
  kubectl get configmaps -n ${namespace} -o yaml | yq eval '.items[] |del(.metadata.annotations, .metadata.managedFields, .metadata.selfLink, .metadata.creationTimestamp, .metadata.uid, .metadata.resourceVersion)' - >all-configmaps-$namespace.yaml
  sed -i '/^apiVersion:/i ---' all-configmaps-$namespace.yaml
  echo " done !"
  echo ""
}

backup_all_stateful_sets() {
  echo "start to back up all stateful sets ! "
  kubectl get statefulsets -n ${namespace} -o yaml | yq eval '.items[] |del(.metadata.annotations, .metadata.managedFields, .metadata.selfLink, .metadata.creationTimestamp,.metadata.generation, .metadata.uid, .metadata.resourceVersion, .status)' - >all-statefull_sets-$namespace.yaml
  sed -i '/^apiVersion:/i ---' all-statefull_sets-$namespace.yaml
  echo " done !"
  echo ""
}

install_yq
backup_all_deployment
backup_all_service
backup_all_stateful_sets
backup_all_configmap

# https://github.com/mikefarah/yq/releases/download/v4.44.1/yq_linux_amd64
