blob: 52748bc933ead6d45f9f6524133142bbcb865efb [file] [log] [blame]
Mandeep Khindad6ea9872017-06-24 11:49:37 -04001#!/bin/bash
Borislav Glozman5197e2e2017-07-24 10:45:28 +03002
3. $(dirname "$0")/setenv.bash
4
Mandeep Khindad6ea9872017-06-24 11:49:37 -04005delete_namespace() {
BorislavG8bfc6cf2018-02-27 15:04:26 +00006 kubectl delete namespace $1
Borislav Glozman5197e2e2017-07-24 10:45:28 +03007}
8
Anaël Closson40cdf492017-10-09 14:52:24 +02009delete_service_account() {
10 kubectl delete clusterrolebinding $1-$2-admin-binding
11 printf "Service account $1-$2-admin-binding deleted.\n\n"
12}
13
Borislav Glozman5197e2e2017-07-24 10:45:28 +030014delete_registry_key() {
Mandeep Khinda0e8f8892017-08-25 03:31:17 +000015 kubectl --namespace $1-$2 delete secret ${1}-docker-registry-key
Mandeep Khindad6ea9872017-06-24 11:49:37 -040016}
17
kerenj74d723a2017-08-22 15:27:04 +000018delete_app_helm() {
kerenj78eff212017-08-29 09:56:36 +000019 helm delete $1-$2 --purge
kerenj74d723a2017-08-22 15:27:04 +000020}
21
yuryn715e36b2017-10-15 19:07:29 +030022wait_terminate() {
23 printf "Waiting for namespaces termination...\n"
24 while true; do
25 declare -i _STATUS=0
26 for i in ${HELM_APPS[@]}; do
BorislavG8bfc6cf2018-02-27 15:04:26 +000027 kubectl get pods --namespace $1 | grep -w " $i" > /dev/null 2>&1
28 if [ "$?" -ne "0" ]; then
yuryn715e36b2017-10-15 19:07:29 +030029 _STATUS=1
30 break
31 fi
32 done
BorislavG8bfc6cf2018-02-27 15:04:26 +000033
34 if [ "$SINGLE_COMPONENT" == "false" ]; then
35 kubectl get namespaces $1 > /dev/null 2>&1
36 _STATUS=$?
37 fi
38 if [ "$_STATUS" -ne "0" ]; then
yuryn715e36b2017-10-15 19:07:29 +030039 break
40 fi
41 sleep 2
42 done
43}
44
Mandeep Khindad6ea9872017-06-24 11:49:37 -040045usage() {
46 cat <<EOF
47Usage: $0 [PARAMs]
48-u : Display usage
49-n [NAMESPACE] : Kubernetes namespace (required)
Munir Ahmad4add94b2017-12-20 13:09:05 -050050-c : kubectl context (default: current context)
51-y : Skip interactive confirmation (default: no)
Mandeep Khindad6ea9872017-06-24 11:49:37 -040052-a [APP] : Specify a specific ONAP component (default: all)
53 from the following choices:
Dusan Rozman03aa27e2017-10-11 13:04:23 -040054 sdc, aai ,mso, message-router, robot, vid, aaf, uui
Dusan Rozman7704d2a2017-09-26 16:04:12 -040055 sdnc, portal, policy, appc, multicloud, clamp, consul, vnfsdk
yuryn715e36b2017-10-15 19:07:29 +030056-N : Do not wait for deletion of namespace and its objects
Mandeep Khindad6ea9872017-06-24 11:49:37 -040057EOF
58}
59
60#MAINs
61NS=
62INCL_SVC=false
63APP=
yuryn715e36b2017-10-15 19:07:29 +030064WAIT_TERMINATE=true
Munir Ahmad4add94b2017-12-20 13:09:05 -050065SKIP_INTERACTIVE_CONFIRMATION=no
66KUBECTL_CONTEXT=
BorislavG8bfc6cf2018-02-27 15:04:26 +000067SINGLE_COMPONENT=false
Munir Ahmad4add94b2017-12-20 13:09:05 -050068while getopts ":c:n:u:s:a:yN" PARAM; do
Mandeep Khindad6ea9872017-06-24 11:49:37 -040069 case $PARAM in
70 u)
71 usage
72 exit 1
73 ;;
74 n)
75 NS=${OPTARG}
76 ;;
Mandeep Khindad6ea9872017-06-24 11:49:37 -040077 a)
78 APP=${OPTARG}
79 if [[ -z $APP ]]; then
80 usage
81 exit 1
82 fi
BorislavG8bfc6cf2018-02-27 15:04:26 +000083 SINGLE_COMPONENT=true
Mandeep Khindad6ea9872017-06-24 11:49:37 -040084 ;;
yuryn715e36b2017-10-15 19:07:29 +030085 N)
86 WAIT_TERMINATE=false
87 ;;
Munir Ahmad4add94b2017-12-20 13:09:05 -050088 y)
89 SKIP_INTERACTIVE_CONFIRMATION=yes
90 ;;
91 c)
92 KUBECTL_CONTEXT=${OPTARG}
93 ;;
Mandeep Khindad6ea9872017-06-24 11:49:37 -040094 ?)
95 usage
96 exit
97 ;;
98 esac
99done
100
101if [[ -z $NS ]]; then
102 usage
103 exit 1
104fi
105
Munir Ahmad4add94b2017-12-20 13:09:05 -0500106if [[ "$SKIP_INTERACTIVE_CONFIRMATION" != yes ]]; then
107 current_kubectl_context=$(kubectl config get-contexts |grep "*" |awk '{print $2}')
108 if test "$KUBECTL_CONTEXT" != "$current_kubectl_context"; then
109 printf "Current kubectl context does not match context specified:\x1b[31m $current_kubectl_context\x1b[0m\n"
110 if [ ! -z "$KUBECTL_CONTEXT" -a "$KUBECTL_CONTEXT" != " " ]; then
111 read -p "Do you wish to switch context to $KUBECTL_CONTEXT and continue?" yn
112 case $yn in
113 [Yy]* ) kubectl config use-context $KUBECTL_CONTEXT;;
114 * ) printf "Skipping delete...\n"; exit;;
115 esac
116 else
117 printf "You are about to delete deployment from:\x1b[31m $current_kubectl_context\x1b[0m\n"
118 read -p "To continue enter context name: " response
119
120 if test "$response" != "$current_kubectl_context"
121 then
122 printf "Your response does not match current context! Skipping delete ...\n"
123 exit 1
124 fi
125 fi
126 fi
127fi
128
Mandeep Khindad6ea9872017-06-24 11:49:37 -0400129if [[ ! -z "$APP" ]]; then
kerenj80102842017-08-24 13:54:21 +0000130 HELM_APPS=($APP)
Mandeep Khindad6ea9872017-06-24 11:49:37 -0400131fi
132
133printf "\n********** Cleaning up ONAP: ${ONAP_APPS[*]}\n"
134
kerenj74d723a2017-08-22 15:27:04 +0000135for i in ${HELM_APPS[@]}; do
kerenj78eff212017-08-29 09:56:36 +0000136 delete_app_helm $NS $i
Anaël Closson40cdf492017-10-09 14:52:24 +0200137 delete_service_account $NS $i
kerenj74d723a2017-08-22 15:27:04 +0000138done
139
BorislavG8bfc6cf2018-02-27 15:04:26 +0000140if [ "$SINGLE_COMPONENT" == "false" ]
141then
142 delete_app_helm $NS "config"
143 delete_namespace $NS
144fi
Munir Ahmada37c0f42017-12-07 14:31:01 -0500145
yuryn715e36b2017-10-15 19:07:29 +0300146if $WAIT_TERMINATE; then
147 wait_terminate $NS
148fi
Mandeep Khindad6ea9872017-06-24 11:49:37 -0400149
150printf "\n********** Gone **********\n"