53f1d1a9cbccc6bc9202d24bff43a751e28e4db3
[oom.git] / kubernetes / oneclick / deleteAll.bash
1 #!/bin/bash
2
3 . $(dirname "$0")/setenv.bash
4
5 delete_namespace() {
6   _NS=$1-$2
7   kubectl delete namespace $_NS
8 }
9
10 delete_service_account() {
11     kubectl delete clusterrolebinding $1-$2-admin-binding
12     printf "Service account $1-$2-admin-binding deleted.\n\n"
13 }
14
15 delete_registry_key() {
16   kubectl --namespace $1-$2 delete secret ${1}-docker-registry-key
17 }
18
19 delete_app_helm() {
20   helm delete $1-$2 --purge
21 }
22
23 wait_terminate() {
24   printf "Waiting for namespaces termination...\n"
25   while true; do
26     declare -i _STATUS=0
27     for i in ${HELM_APPS[@]}; do
28       kubectl get namespaces $1-$i > /dev/null 2>&1
29       if [ "$?" -eq "0" ]; then
30         _STATUS=1
31         break
32       fi
33     done
34     if [ "$_STATUS" -eq "0" ]; then
35       break
36     fi
37     sleep 2
38   done
39 }
40
41 usage() {
42   cat <<EOF
43 Usage: $0 [PARAMs]
44 -u                  : Display usage
45 -n [NAMESPACE]      : Kubernetes namespace (required)
46 -c                  : kubectl context (default: current context)
47 -y                  : Skip interactive confirmation (default: no)
48 -a [APP]            : Specify a specific ONAP component (default: all)
49                       from the following choices:
50                       sdc, aai ,mso, message-router, robot, vid, aaf, uui
51                       sdnc, portal, policy, appc, multicloud, clamp, consul, vnfsdk
52 -N                  : Do not wait for deletion of namespace and its objects
53 EOF
54 }
55
56 #MAINs
57 NS=
58 INCL_SVC=false
59 APP=
60 WAIT_TERMINATE=true
61 SKIP_INTERACTIVE_CONFIRMATION=no
62 KUBECTL_CONTEXT=
63
64 while getopts ":c:n:u:s:a:yN" PARAM; do
65   case $PARAM in
66     u)
67       usage
68       exit 1
69       ;;
70     n)
71       NS=${OPTARG}
72       ;;
73     a)
74       APP=${OPTARG}
75       if [[ -z $APP ]]; then
76         usage
77         exit 1
78       fi
79       ;;
80     N)
81       WAIT_TERMINATE=false
82       ;;
83     y)
84       SKIP_INTERACTIVE_CONFIRMATION=yes
85       ;;
86     c)
87       KUBECTL_CONTEXT=${OPTARG}
88       ;;
89     ?)
90       usage
91       exit
92       ;;
93   esac
94 done
95
96 if [[ -z $NS ]]; then
97   usage
98   exit 1
99 fi
100
101 if [[ "$SKIP_INTERACTIVE_CONFIRMATION" != yes ]]; then
102   current_kubectl_context=$(kubectl config get-contexts |grep "*" |awk '{print $2}')
103   if test "$KUBECTL_CONTEXT" != "$current_kubectl_context"; then
104     printf "Current kubectl context does not match context specified:\x1b[31m $current_kubectl_context\x1b[0m\n"
105     if [ ! -z "$KUBECTL_CONTEXT" -a "$KUBECTL_CONTEXT" != " " ]; then
106       read -p "Do you wish to switch context to $KUBECTL_CONTEXT and continue?" yn
107       case $yn in
108         [Yy]* ) kubectl config use-context $KUBECTL_CONTEXT;;
109         * ) printf "Skipping delete...\n"; exit;;
110       esac
111     else
112       printf "You are about to delete deployment from:\x1b[31m $current_kubectl_context\x1b[0m\n"
113       read -p "To continue enter context name: " response
114
115       if test "$response" != "$current_kubectl_context"
116       then
117         printf "Your response does not match current context! Skipping delete ...\n"
118         exit 1
119       fi
120     fi
121   fi
122 fi
123
124 if [[ ! -z "$APP" ]]; then
125   HELM_APPS=($APP)
126 fi
127
128 printf "\n********** Cleaning up ONAP: ${ONAP_APPS[*]}\n"
129
130 for i in ${HELM_APPS[@]}; do
131
132   delete_app_helm $NS $i
133   delete_namespace $NS $i
134   delete_service_account $NS $i
135
136 done
137
138 delete_app_helm $NS "config"
139 kubectl delete namespace $NS
140
141 if $WAIT_TERMINATE; then
142   wait_terminate $NS
143 fi
144
145 printf "\n********** Gone **********\n"