github.com/verrazzano/verrazzano@v1.7.0/platform-operator/thirdparty/charts/rancher/scripts/post-delete-hook.sh (about)

     1  #!/bin/bash
     2  
     3  set -e
     4  
     5  namespaces="${NAMESPACES}"
     6  rancher_namespace="${RANCHER_NAMESPACE}"
     7  timeout="${TIMEOUT}"
     8  ignoreTimeoutError="${IGNORETIMEOUTERROR}"
     9  
    10  if [[ -z ${namespaces} ]]; then
    11    echo "No namespace is provided."
    12    exit 1
    13  fi
    14  
    15  if [[ -z ${rancher_namespace} ]]; then
    16    echo "No rancher namespace is provided."
    17    exit 1
    18  fi
    19  
    20  if [[ -z ${timeout} ]]; then
    21    echo "No timeout value is provided."
    22    exit 1
    23  fi
    24  
    25  if [[ -z ${ignoreTimeoutError} ]]; then
    26    echo "No ignoreTimeoutError value is provided."
    27    exit 1
    28  fi
    29  
    30  succeeded=()
    31  failed=()
    32  
    33  get_pod_count() {
    34    kubectl get pods --selector app="${1}" -n "${2}" -o json | jq '.items | length'
    35  }
    36  
    37  echo "Uninstalling Rancher resources in the following namespaces: ${namespaces}"
    38  
    39  for namespace in ${namespaces}; do
    40    for app in $(helm list -n "${namespace}" -q); do
    41      if [[ ${app} =~ .crd$ ]]; then
    42        echo "--- Skip the app [${app}] in the namespace [${namespace}]"
    43        continue
    44      fi
    45      echo "--- Deleting the app [${app}] in the namespace [${namespace}]"
    46      if [[ ! $(helm uninstall "${app}" -n "${namespace}") ]]; then
    47        failed=("${failed[@]}" "${app}")
    48        continue
    49      fi
    50  
    51      t=0
    52      while true; do
    53        if [[ $(get_pod_count "${app}" "${namespace}") -eq 0 ]]; then
    54          echo "successfully uninstalled [${app}] in the namespace [${namespace}]"
    55          succeeded=("${succeeded[@]}" "${app}")
    56          break
    57        fi
    58        if [[ ${t} -ge ${timeout} ]]; then
    59          echo "timeout uninstalling [${app}] in the namespace [${namespace}]"
    60          failed=("${failed[@]}" "${app}")
    61          break
    62        fi
    63        # by default, wait 120 seconds in total for an app to be uninstalled
    64        echo "waiting 5 seconds for pods of [${app}] to be terminated ..."
    65        sleep 5
    66        t=$((t + 5))
    67      done
    68    done
    69  
    70    # delete the helm operator pods
    71    for pod in $(kubectl get pods -n "${namespace}" -o name); do
    72      if [[ ${pod} =~ ^pod\/helm-operation-* ]]; then
    73        echo "--- Deleting the pod [${pod}] in the namespace [${namespace}]"
    74        kubectl delete "${pod}" -n "${namespace}"
    75      fi
    76    done
    77  done
    78  
    79  echo "Removing Rancher bootstrap secret in the following namespace: ${rancher_namespace}"
    80  kubectl --ignore-not-found=true delete secret bootstrap-secret -n "${rancher_namespace}"
    81  
    82  echo "------ Summary ------"
    83  if [[ ${#succeeded[@]} -ne 0 ]]; then
    84    echo "Succeeded to uninstall the following apps:" "${succeeded[@]}"
    85  fi
    86  
    87  if [[ ${#failed[@]} -ne 0 ]]; then
    88    echo "Failed to uninstall the following apps:" "${failed[@]}"
    89    if [[ "${ignoreTimeoutError}" == "false" ]]; then
    90      exit 2
    91    fi
    92  else
    93    echo "Cleanup finished successfully."
    94  fi