Vladimir Khlyunev | cc648af | 2024-04-25 19:56:40 +0400 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | while [ "$#" -gt 0 ]; do |
| 4 | case "$1" in |
| 5 | -n|--namespace) NAMESPACE="$2"; shift 2;; |
| 6 | -n=*|--namespace=*) NAMESPACE="${1#*=}"; shift 1;; |
| 7 | |
| 8 | -c|--child-name) CHILD_NAME="$2"; shift 2;; |
| 9 | -c=*|--child-name=*) CHILD_NAME="${1#*=}"; shift 1;; |
| 10 | |
| 11 | -f|--force) FORCE="true"; shift 1;; |
| 12 | |
| 13 | --*|-*|*) echo "unknown option: $1"; help; exit 1;; |
| 14 | esac |
| 15 | done |
| 16 | |
| 17 | |
| 18 | function wait_threads() { |
| 19 | while [[ $(jobs -r -p | wc -l) -gt 0 ]]; do |
| 20 | sleep 0.1 |
| 21 | done |
| 22 | } |
| 23 | |
| 24 | function wait_threads_limit() { |
| 25 | local LIMIT=$1 |
| 26 | while [[ $(jobs -r -p | wc -l) -ge $LIMIT ]]; do |
| 27 | sleep 0.1 |
| 28 | done |
| 29 | } |
| 30 | |
| 31 | #function k_patch() { |
| 32 | # local TYPE=$1 |
| 33 | # local ITEM=$2 |
| 34 | # local PATCH=$3 |
| 35 | # |
| 36 | # echo $TYPE |
| 37 | # echo $ITEM |
| 38 | # echo $PATCH |
| 39 | # echo kubectl -n ${NAMESPACE} patch $TYPE $ITEM --type merge -p $PATCH 2>/dev/null |
| 40 | # |
| 41 | # kubectl -n ${NAMESPACE} patch $TYPE $ITEM --type merge -p $PATCH 2>/dev/null || true |
| 42 | #} |
| 43 | |
| 44 | #function k_delete() { |
| 45 | # local TYPE=$1 |
| 46 | # local ITEM=$2 |
| 47 | # |
| 48 | # kubectl -n ${NAMESPACE} delete $TYPE $ITEM || true |
| 49 | #} |
| 50 | |
| 51 | |
| 52 | echo "= Cleanup env ${NAMESPACE}" |
| 53 | |
| 54 | BMH=$(kubectl -n ${NAMESPACE} get bmh -o json | jq '.items[].metadata.name' -r) |
| 55 | for bmh in $BMH; do |
| 56 | if [[ $FORCE ]]; then |
| 57 | echo "= Path baremetalhost: $bmh" |
| 58 | kubectl -n ${NAMESPACE} patch bmh $bmh '{"spec": {"automatedCleaningMode": "disabled"}}' 2>/dev/null || true |
| 59 | fi |
| 60 | done |
| 61 | |
| 62 | MACHINE=$(kubectl -n ${NAMESPACE} get machine -o json | jq '.items[].metadata.name' -r) |
| 63 | for machine in $MACHINE; do |
| 64 | echo "= Delete machines: $machine" |
| 65 | if [[ $FORCE ]]; then |
| 66 | kubectl -n ${NAMESPACE} patch machine $machine --type merge -p '{"metadata": {"annotations": {"kaas.mirantis.com/validate": "false"}}}' 2>/dev/null || true |
| 67 | kubectl -n ${NAMESPACE} patch machine $machine --type merge -p '{"metadata": {"finalizers": null}}' 2>/dev/null || true |
| 68 | sleep 1 |
| 69 | fi |
| 70 | kubectl -n ${NAMESPACE} delete machine $machine & |
| 71 | sleep 3 && kubectl -n ${NAMESPACE} patch machine $machine --type merge -p '{"metadata": {"finalizers": null}}' 2>/dev/null || true |
| 72 | wait_threads |
| 73 | done |
| 74 | |
| 75 | for bmh in $BMH; do |
| 76 | echo "= Delete baremetalhost: $bmh" |
| 77 | if [[ $FORCE ]]; then |
| 78 | kubectl -n ${NAMESPACE} patch bmh $bmh --type merge -p '{"metadata": {"annotations": {"kaas.mirantis.com/validate": "false"}}}' 2>/dev/null || true |
| 79 | kubectl -n ${NAMESPACE} patch bmh $bmh --type merge -p '{"spec": {"consumerRef": null}}' 2>/dev/null || true |
| 80 | kubectl -n ${NAMESPACE} patch bmh $bmh --type merge -p '{"metadata": {"finalizers": null}}' 2>/dev/null || true |
| 81 | fi |
| 82 | kubectl -n ${NAMESPACE} delete bmh $bmh & |
| 83 | sleep 1 && kubectl -n ${NAMESPACE} patch bmh $bmh --type merge -p '{"metadata": {"finalizers": null}}' 2>/dev/null || true |
| 84 | wait_threads |
| 85 | done |
| 86 | |
| 87 | SECRET=$(kubectl -n ${NAMESPACE} get secret -o json | jq '.items[].metadata.name' -r) |
| 88 | for secret in $SECRET; do |
| 89 | echo "= Delete secret: $secret" |
| 90 | if [[ $FORCE ]]; then |
| 91 | kubectl -n ${NAMESPACE} patch secret $secret --type merge -p '{"metadata": {"annotations": {"kaas.mirantis.com/validate": "false"}}}' 2>/dev/null || true |
| 92 | kubectl -n ${NAMESPACE} patch secret $secret --type merge -p '{"metadata": {"finalizers": null}}' 2>/dev/null || true |
| 93 | fi |
| 94 | kubectl -n ${NAMESPACE} delete secret $secret & |
| 95 | sleep 1 && kubectl -n ${NAMESPACE} patch secret $secret --type merge -p '{"metadata": {"finalizers": null}}' 2>/dev/null || true |
| 96 | wait_threads |
| 97 | done |
| 98 | |
| 99 | CLUSTER=$(kubectl -n ${NAMESPACE} get cluster -o json | jq '.items[].metadata.name' -r) |
| 100 | for cluster in $CLUSTER; do |
| 101 | echo "= Delete cluster: $cl" |
| 102 | if [[ $FORCE ]]; then |
| 103 | kubectl -n ${NAMESPACE} patch cluster $cluster --type merge -p '{"metadata": {"annotations": {"kaas.mirantis.com/validate": "false"}}}' 2>/dev/null || true |
| 104 | kubectl -n ${NAMESPACE} patch cluster $cluster --type merge -p '{"metadata": {"finalizers": null}}' 2>/dev/null || true |
| 105 | fi |
| 106 | kubectl -n ${NAMESPACE} delete cluster $cluster || true |
| 107 | done |
| 108 | |
| 109 | wait_threads |
| 110 | |
| 111 | echo "= Delete namespace: ${NAMESPACE}" |
| 112 | kubectl delete ns ${NAMESPACE} || true |
| 113 | kubectl -n kaas scale deployment/ironic --replicas=0 |
| 114 | sleep 10 |
| 115 | kubectl -n kaas scale deployment/ironic --replicas=1 |
| 116 | (kubectl api-resources --verbs=list --namespaced -o name | xargs -n 1 kubectl get --show-kind --ignore-not-found -n "${NAMESPACE}" ) || true |
| 117 | echo "Done." |