blob: 9833af790979499760f09777b56b4ffd8ba61531 [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() {
BorislavG042b6852018-03-04 15:03:52 +000010 kubectl delete clusterrolebinding $1-admin-binding
Anaël Closson40cdf492017-10-09 14:52:24 +020011}
12
Borislav Glozman5197e2e2017-07-24 10:45:28 +030013delete_registry_key() {
BorislavG042b6852018-03-04 15:03:52 +000014 kubectl --namespace $1 delete secret ${1}-docker-registry-key
Mandeep Khindad6ea9872017-06-24 11:49:37 -040015}
16
kerenj74d723a2017-08-22 15:27:04 +000017delete_app_helm() {
kerenj78eff212017-08-29 09:56:36 +000018 helm delete $1-$2 --purge
kerenj74d723a2017-08-22 15:27:04 +000019}
20
yuryn715e36b2017-10-15 19:07:29 +030021wait_terminate() {
22 printf "Waiting for namespaces termination...\n"
23 while true; do
24 declare -i _STATUS=0
25 for i in ${HELM_APPS[@]}; do
BorislavG8bfc6cf2018-02-27 15:04:26 +000026 kubectl get pods --namespace $1 | grep -w " $i" > /dev/null 2>&1
27 if [ "$?" -ne "0" ]; then
yuryn715e36b2017-10-15 19:07:29 +030028 _STATUS=1
29 break
30 fi
31 done
BorislavG8bfc6cf2018-02-27 15:04:26 +000032
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
yuryn715e36b2017-10-15 19:07:29 +030038 break
39 fi
40 sleep 2
41 done
42}
43
Mandeep Khindad6ea9872017-06-24 11:49:37 -040044usage() {
45 cat <<EOF
46Usage: $0 [PARAMs]
47-u : Display usage
48-n [NAMESPACE] : Kubernetes namespace (required)
Munir Ahmad4add94b2017-12-20 13:09:05 -050049-c : kubectl context (default: current context)
50-y : Skip interactive confirmation (default: no)
Mandeep Khindad6ea9872017-06-24 11:49:37 -040051-a [APP] : Specify a specific ONAP component (default: all)
52 from the following choices:
Dusan Rozman03aa27e2017-10-11 13:04:23 -040053 sdc, aai ,mso, message-router, robot, vid, aaf, uui
Dusan Rozman7704d2a2017-09-26 16:04:12 -040054 sdnc, portal, policy, appc, multicloud, clamp, consul, vnfsdk
yuryn715e36b2017-10-15 19:07:29 +030055-N : Do not wait for deletion of namespace and its objects
Mandeep Khindad6ea9872017-06-24 11:49:37 -040056EOF
57}
58
59#MAINs
60NS=
61INCL_SVC=false
62APP=
yuryn715e36b2017-10-15 19:07:29 +030063WAIT_TERMINATE=true
Munir Ahmad4add94b2017-12-20 13:09:05 -050064SKIP_INTERACTIVE_CONFIRMATION=no
65KUBECTL_CONTEXT=
BorislavG8bfc6cf2018-02-27 15:04:26 +000066SINGLE_COMPONENT=false
Munir Ahmad4add94b2017-12-20 13:09:05 -050067while getopts ":c:n:u:s:a:yN" PARAM; do
Mandeep Khindad6ea9872017-06-24 11:49:37 -040068 case $PARAM in
69 u)
70 usage
71 exit 1
72 ;;
73 n)
74 NS=${OPTARG}
75 ;;
Mandeep Khindad6ea9872017-06-24 11:49:37 -040076 a)
77 APP=${OPTARG}
78 if [[ -z $APP ]]; then
79 usage
80 exit 1
81 fi
BorislavG8bfc6cf2018-02-27 15:04:26 +000082 SINGLE_COMPONENT=true
Mandeep Khindad6ea9872017-06-24 11:49:37 -040083 ;;
yuryn715e36b2017-10-15 19:07:29 +030084 N)
85 WAIT_TERMINATE=false
86 ;;
Munir Ahmad4add94b2017-12-20 13:09:05 -050087 y)
88 SKIP_INTERACTIVE_CONFIRMATION=yes
89 ;;
90 c)
91 KUBECTL_CONTEXT=${OPTARG}
92 ;;
Mandeep Khindad6ea9872017-06-24 11:49:37 -040093 ?)
94 usage
95 exit
96 ;;
97 esac
98done
99
100if [[ -z $NS ]]; then
101 usage
102 exit 1
103fi
104
Munir Ahmad4add94b2017-12-20 13:09:05 -0500105if [[ "$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
126fi
127
Mandeep Khindad6ea9872017-06-24 11:49:37 -0400128if [[ ! -z "$APP" ]]; then
kerenj80102842017-08-24 13:54:21 +0000129 HELM_APPS=($APP)
Mandeep Khindad6ea9872017-06-24 11:49:37 -0400130fi
131
132printf "\n********** Cleaning up ONAP: ${ONAP_APPS[*]}\n"
133
kerenj74d723a2017-08-22 15:27:04 +0000134for i in ${HELM_APPS[@]}; do
kerenj78eff212017-08-29 09:56:36 +0000135 delete_app_helm $NS $i
kerenj74d723a2017-08-22 15:27:04 +0000136done
137
BorislavG8bfc6cf2018-02-27 15:04:26 +0000138if [ "$SINGLE_COMPONENT" == "false" ]
139then
140 delete_app_helm $NS "config"
141 delete_namespace $NS
BorislavG042b6852018-03-04 15:03:52 +0000142 delete_registry_key $NS
143 delete_service_account $NS
BorislavG8bfc6cf2018-02-27 15:04:26 +0000144fi
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"