post-delete-hook.sh 2.5 KB

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