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