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