blob: 53f1d1a9cbccc6bc9202d24bff43a751e28e4db3 [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() {
Borislav Glozman5197e2e2017-07-24 10:45:28 +03006 _NS=$1-$2
7 kubectl delete namespace $_NS
Borislav Glozman5197e2e2017-07-24 10:45:28 +03008}
9
Anaël Closson40cdf492017-10-09 14:52:24 +020010delete_service_account() {
11 kubectl delete clusterrolebinding $1-$2-admin-binding
12 printf "Service account $1-$2-admin-binding deleted.\n\n"
13}
14
Borislav Glozman5197e2e2017-07-24 10:45:28 +030015delete_registry_key() {
Mandeep Khinda0e8f8892017-08-25 03:31:17 +000016 kubectl --namespace $1-$2 delete secret ${1}-docker-registry-key
Mandeep Khindad6ea9872017-06-24 11:49:37 -040017}
18
kerenj74d723a2017-08-22 15:27:04 +000019delete_app_helm() {
kerenj78eff212017-08-29 09:56:36 +000020 helm delete $1-$2 --purge
kerenj74d723a2017-08-22 15:27:04 +000021}
22
yuryn715e36b2017-10-15 19:07:29 +030023wait_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
Mandeep Khindad6ea9872017-06-24 11:49:37 -040041usage() {
42 cat <<EOF
43Usage: $0 [PARAMs]
44-u : Display usage
45-n [NAMESPACE] : Kubernetes namespace (required)
Munir Ahmad4add94b2017-12-20 13:09:05 -050046-c : kubectl context (default: current context)
47-y : Skip interactive confirmation (default: no)
Mandeep Khindad6ea9872017-06-24 11:49:37 -040048-a [APP] : Specify a specific ONAP component (default: all)
49 from the following choices:
Dusan Rozman03aa27e2017-10-11 13:04:23 -040050 sdc, aai ,mso, message-router, robot, vid, aaf, uui
Dusan Rozman7704d2a2017-09-26 16:04:12 -040051 sdnc, portal, policy, appc, multicloud, clamp, consul, vnfsdk
yuryn715e36b2017-10-15 19:07:29 +030052-N : Do not wait for deletion of namespace and its objects
Mandeep Khindad6ea9872017-06-24 11:49:37 -040053EOF
54}
55
56#MAINs
57NS=
58INCL_SVC=false
59APP=
yuryn715e36b2017-10-15 19:07:29 +030060WAIT_TERMINATE=true
Munir Ahmad4add94b2017-12-20 13:09:05 -050061SKIP_INTERACTIVE_CONFIRMATION=no
62KUBECTL_CONTEXT=
Mandeep Khindad6ea9872017-06-24 11:49:37 -040063
Munir Ahmad4add94b2017-12-20 13:09:05 -050064while getopts ":c:n:u:s:a:yN" PARAM; do
Mandeep Khindad6ea9872017-06-24 11:49:37 -040065 case $PARAM in
66 u)
67 usage
68 exit 1
69 ;;
70 n)
71 NS=${OPTARG}
72 ;;
Mandeep Khindad6ea9872017-06-24 11:49:37 -040073 a)
74 APP=${OPTARG}
75 if [[ -z $APP ]]; then
76 usage
77 exit 1
78 fi
79 ;;
yuryn715e36b2017-10-15 19:07:29 +030080 N)
81 WAIT_TERMINATE=false
82 ;;
Munir Ahmad4add94b2017-12-20 13:09:05 -050083 y)
84 SKIP_INTERACTIVE_CONFIRMATION=yes
85 ;;
86 c)
87 KUBECTL_CONTEXT=${OPTARG}
88 ;;
Mandeep Khindad6ea9872017-06-24 11:49:37 -040089 ?)
90 usage
91 exit
92 ;;
93 esac
94done
95
96if [[ -z $NS ]]; then
97 usage
98 exit 1
99fi
100
Munir Ahmad4add94b2017-12-20 13:09:05 -0500101if [[ "$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
122fi
123
Mandeep Khindad6ea9872017-06-24 11:49:37 -0400124if [[ ! -z "$APP" ]]; then
kerenj80102842017-08-24 13:54:21 +0000125 HELM_APPS=($APP)
Mandeep Khindad6ea9872017-06-24 11:49:37 -0400126fi
127
128printf "\n********** Cleaning up ONAP: ${ONAP_APPS[*]}\n"
129
kerenj74d723a2017-08-22 15:27:04 +0000130for i in ${HELM_APPS[@]}; do
131
kerenj78eff212017-08-29 09:56:36 +0000132 delete_app_helm $NS $i
kerenj74d723a2017-08-22 15:27:04 +0000133 delete_namespace $NS $i
Anaël Closson40cdf492017-10-09 14:52:24 +0200134 delete_service_account $NS $i
kerenj74d723a2017-08-22 15:27:04 +0000135
136done
137
Munir Ahmada37c0f42017-12-07 14:31:01 -0500138delete_app_helm $NS "config"
139kubectl delete namespace $NS
140
yuryn715e36b2017-10-15 19:07:29 +0300141if $WAIT_TERMINATE; then
142 wait_terminate $NS
143fi
Mandeep Khindad6ea9872017-06-24 11:49:37 -0400144
145printf "\n********** Gone **********\n"