Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 1 | #!/bin/bash |
Borislav Glozman | 5197e2e | 2017-07-24 10:45:28 +0300 | [diff] [blame] | 2 | |
| 3 | . $(dirname "$0")/setenv.bash |
| 4 | |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 5 | delete_namespace() { |
BorislavG | 8bfc6cf | 2018-02-27 15:04:26 +0000 | [diff] [blame] | 6 | kubectl delete namespace $1 |
Borislav Glozman | 5197e2e | 2017-07-24 10:45:28 +0300 | [diff] [blame] | 7 | } |
| 8 | |
Anaël Closson | 40cdf49 | 2017-10-09 14:52:24 +0200 | [diff] [blame] | 9 | delete_service_account() { |
| 10 | kubectl delete clusterrolebinding $1-$2-admin-binding |
| 11 | printf "Service account $1-$2-admin-binding deleted.\n\n" |
| 12 | } |
| 13 | |
Borislav Glozman | 5197e2e | 2017-07-24 10:45:28 +0300 | [diff] [blame] | 14 | delete_registry_key() { |
Mandeep Khinda | 0e8f889 | 2017-08-25 03:31:17 +0000 | [diff] [blame] | 15 | kubectl --namespace $1-$2 delete secret ${1}-docker-registry-key |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 16 | } |
| 17 | |
kerenj | 74d723a | 2017-08-22 15:27:04 +0000 | [diff] [blame] | 18 | delete_app_helm() { |
kerenj | 78eff21 | 2017-08-29 09:56:36 +0000 | [diff] [blame] | 19 | helm delete $1-$2 --purge |
kerenj | 74d723a | 2017-08-22 15:27:04 +0000 | [diff] [blame] | 20 | } |
| 21 | |
yuryn | 715e36b | 2017-10-15 19:07:29 +0300 | [diff] [blame] | 22 | wait_terminate() { |
| 23 | printf "Waiting for namespaces termination...\n" |
| 24 | while true; do |
| 25 | declare -i _STATUS=0 |
| 26 | for i in ${HELM_APPS[@]}; do |
BorislavG | 8bfc6cf | 2018-02-27 15:04:26 +0000 | [diff] [blame] | 27 | kubectl get pods --namespace $1 | grep -w " $i" > /dev/null 2>&1 |
| 28 | if [ "$?" -ne "0" ]; then |
yuryn | 715e36b | 2017-10-15 19:07:29 +0300 | [diff] [blame] | 29 | _STATUS=1 |
| 30 | break |
| 31 | fi |
| 32 | done |
BorislavG | 8bfc6cf | 2018-02-27 15:04:26 +0000 | [diff] [blame] | 33 | |
| 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 |
yuryn | 715e36b | 2017-10-15 19:07:29 +0300 | [diff] [blame] | 39 | break |
| 40 | fi |
| 41 | sleep 2 |
| 42 | done |
| 43 | } |
| 44 | |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 45 | usage() { |
| 46 | cat <<EOF |
| 47 | Usage: $0 [PARAMs] |
| 48 | -u : Display usage |
| 49 | -n [NAMESPACE] : Kubernetes namespace (required) |
Munir Ahmad | 4add94b | 2017-12-20 13:09:05 -0500 | [diff] [blame] | 50 | -c : kubectl context (default: current context) |
| 51 | -y : Skip interactive confirmation (default: no) |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 52 | -a [APP] : Specify a specific ONAP component (default: all) |
| 53 | from the following choices: |
Dusan Rozman | 03aa27e | 2017-10-11 13:04:23 -0400 | [diff] [blame] | 54 | sdc, aai ,mso, message-router, robot, vid, aaf, uui |
Dusan Rozman | 7704d2a | 2017-09-26 16:04:12 -0400 | [diff] [blame] | 55 | sdnc, portal, policy, appc, multicloud, clamp, consul, vnfsdk |
yuryn | 715e36b | 2017-10-15 19:07:29 +0300 | [diff] [blame] | 56 | -N : Do not wait for deletion of namespace and its objects |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 57 | EOF |
| 58 | } |
| 59 | |
| 60 | #MAINs |
| 61 | NS= |
| 62 | INCL_SVC=false |
| 63 | APP= |
yuryn | 715e36b | 2017-10-15 19:07:29 +0300 | [diff] [blame] | 64 | WAIT_TERMINATE=true |
Munir Ahmad | 4add94b | 2017-12-20 13:09:05 -0500 | [diff] [blame] | 65 | SKIP_INTERACTIVE_CONFIRMATION=no |
| 66 | KUBECTL_CONTEXT= |
BorislavG | 8bfc6cf | 2018-02-27 15:04:26 +0000 | [diff] [blame] | 67 | SINGLE_COMPONENT=false |
Munir Ahmad | 4add94b | 2017-12-20 13:09:05 -0500 | [diff] [blame] | 68 | while getopts ":c:n:u:s:a:yN" PARAM; do |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 69 | case $PARAM in |
| 70 | u) |
| 71 | usage |
| 72 | exit 1 |
| 73 | ;; |
| 74 | n) |
| 75 | NS=${OPTARG} |
| 76 | ;; |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 77 | a) |
| 78 | APP=${OPTARG} |
| 79 | if [[ -z $APP ]]; then |
| 80 | usage |
| 81 | exit 1 |
| 82 | fi |
BorislavG | 8bfc6cf | 2018-02-27 15:04:26 +0000 | [diff] [blame] | 83 | SINGLE_COMPONENT=true |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 84 | ;; |
yuryn | 715e36b | 2017-10-15 19:07:29 +0300 | [diff] [blame] | 85 | N) |
| 86 | WAIT_TERMINATE=false |
| 87 | ;; |
Munir Ahmad | 4add94b | 2017-12-20 13:09:05 -0500 | [diff] [blame] | 88 | y) |
| 89 | SKIP_INTERACTIVE_CONFIRMATION=yes |
| 90 | ;; |
| 91 | c) |
| 92 | KUBECTL_CONTEXT=${OPTARG} |
| 93 | ;; |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 94 | ?) |
| 95 | usage |
| 96 | exit |
| 97 | ;; |
| 98 | esac |
| 99 | done |
| 100 | |
| 101 | if [[ -z $NS ]]; then |
| 102 | usage |
| 103 | exit 1 |
| 104 | fi |
| 105 | |
Munir Ahmad | 4add94b | 2017-12-20 13:09:05 -0500 | [diff] [blame] | 106 | if [[ "$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 |
| 127 | fi |
| 128 | |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 129 | if [[ ! -z "$APP" ]]; then |
kerenj | 8010284 | 2017-08-24 13:54:21 +0000 | [diff] [blame] | 130 | HELM_APPS=($APP) |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 131 | fi |
| 132 | |
| 133 | printf "\n********** Cleaning up ONAP: ${ONAP_APPS[*]}\n" |
| 134 | |
kerenj | 74d723a | 2017-08-22 15:27:04 +0000 | [diff] [blame] | 135 | for i in ${HELM_APPS[@]}; do |
kerenj | 78eff21 | 2017-08-29 09:56:36 +0000 | [diff] [blame] | 136 | delete_app_helm $NS $i |
Anaël Closson | 40cdf49 | 2017-10-09 14:52:24 +0200 | [diff] [blame] | 137 | delete_service_account $NS $i |
kerenj | 74d723a | 2017-08-22 15:27:04 +0000 | [diff] [blame] | 138 | done |
| 139 | |
BorislavG | 8bfc6cf | 2018-02-27 15:04:26 +0000 | [diff] [blame] | 140 | if [ "$SINGLE_COMPONENT" == "false" ] |
| 141 | then |
| 142 | delete_app_helm $NS "config" |
| 143 | delete_namespace $NS |
| 144 | fi |
Munir Ahmad | a37c0f4 | 2017-12-07 14:31:01 -0500 | [diff] [blame] | 145 | |
yuryn | 715e36b | 2017-10-15 19:07:29 +0300 | [diff] [blame] | 146 | if $WAIT_TERMINATE; then |
| 147 | wait_terminate $NS |
| 148 | fi |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 149 | |
| 150 | printf "\n********** Gone **********\n" |