blob: d6b2d4683f896a8a08ebb43231226b29d9de860c [file] [log] [blame]
Vladimir Khlyunevcc648af2024-04-25 19:56:40 +04001#!/bin/bash
2
3while [ "$#" -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
15done
16
17
18function wait_threads() {
19 while [[ $(jobs -r -p | wc -l) -gt 0 ]]; do
20 sleep 0.1
21 done
22}
23
24function 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
52echo "= Cleanup env ${NAMESPACE}"
53
54BMH=$(kubectl -n ${NAMESPACE} get bmh -o json | jq '.items[].metadata.name' -r)
55for 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
60done
61
62MACHINE=$(kubectl -n ${NAMESPACE} get machine -o json | jq '.items[].metadata.name' -r)
63for 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
73done
74
75for 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
85done
86
87SECRET=$(kubectl -n ${NAMESPACE} get secret -o json | jq '.items[].metadata.name' -r)
88for 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
97done
98
99CLUSTER=$(kubectl -n ${NAMESPACE} get cluster -o json | jq '.items[].metadata.name' -r)
100for 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
107done
108
109wait_threads
110
111echo "= Delete namespace: ${NAMESPACE}"
112kubectl delete ns ${NAMESPACE} || true
113kubectl -n kaas scale deployment/ironic --replicas=0
114sleep 10
115kubectl -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
117echo "Done."