Thursday, January 30, 2020

The following script rounds up the retrieval of definitions
#!/bin/bash
# retrieves definitions and resources for a user namespace
COMMAND=$1
if [ -z "$COMMAND" ]
then
  echo "Command does not exist"
  exit 1
fi

NAMESPACE=$2
if [ -z  "$NAMESPACE" ]
then
  echo "Namespace does not exist"
  exit 1
fi


# Pack
if [[ $1 == "GET" ]]; then
   kubectl api-resources --verbs=list --namespaced -o name | xargs -n 1 kubectl get -n $NAMESPACE -o yaml > definitions.yaml
   kubectl get all -n $NAMESPACE -o yaml > resources.yaml
fi
#Unpack
if [[ $1 == "PUT" ]]; then
   kubectl create ns $NAMESPACE
   if [ ! -e "definitions.yaml" ]; then
       echo "definitions does not exist"
       exit $E_NOFILE
   fi
   sed -i.bak '/creationTimestamp/d'  definitions.yaml
   sed -i.bak '/deletionTimestamp/d' definitions.yaml
   sed -i.bak '/uid:/d' definitions.yaml
   echo kubectl create -f definitions.yaml --validate=false

   if [ ! -e "resources.yaml" ]; then
       echo "resources does not exist"
       exit $E_NOFILE
   fi
   sed -i.bak '/creationTimestamp/d' resources.yaml
   sed -i.bak '/deletionTimestamp/d' resources.yaml
   sed -i.bak '/uid:/d' resources.yaml
   sed -i.bak '/uid:/d' resources.yaml
   echo kubectl create -f resources.yaml --validate=false
fi



By changing the namespace, the same files can be imported again.
The export order should be resources first followed by definitions while the import order should be definitions followed by resources.

No comments:

Post a Comment