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