Skip to content

Commit 51105b4

Browse files
authored
Helm uninstall (#2702)
* helm uninstall Signed-off-by: Allen Li <liyuchen223@gmail.com> * add config flexibility for helm Signed-off-by: Allen Li <liyuchen223@gmail.com> * remove odlm resource after opreq Signed-off-by: Allen Li <liyuchen223@gmail.com> * uninstall edb webhook update unisntall operandrequest Signed-off-by: Allen Li <liyuchen223@gmail.com> * skip waiting if not retain ns Signed-off-by: Allen Li <liyuchen223@gmail.com> * remove entitlement key Signed-off-by: Allen Li <liyuchen223@gmail.com> * uninstall common-web-ui secret Signed-off-by: Allen Li <liyuchen223@gmail.com> --------- Signed-off-by: Allen Li <liyuchen223@gmail.com>
1 parent 8fba527 commit 51105b4

File tree

1 file changed

+134
-35
lines changed

1 file changed

+134
-35
lines changed

cp3pt0-deployment/uninstall_tenant.sh

Lines changed: 134 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ set -o nounset
1414

1515
OC=oc
1616
YQ=yq
17+
HELM=helm
1718
TENANT_NAMESPACES=""
1819
OPERATOR_NS_LIST=""
1920
CONTROL_NS=""
2021
FORCE_DELETE=0
2122
DEBUG=0
2223
RETAIN="false"
24+
NO_OLM="false"
2325

2426
# ---------- Command variables ----------
2527

@@ -39,19 +41,31 @@ function main() {
3941
trap cleanup_log EXIT
4042
pre_req
4143
set_tenant_namespaces
42-
if [ $FORCE_DELETE -eq 0 ]; then
44+
# only waiting for OperandRequests to be deleted when not retaining namespaces
45+
if [[ $RETAIN == "true" ]]; then
46+
uninstall_odlm_resource
47+
uninstall_nss_resource
48+
fi
49+
50+
delete_rbac_resource
51+
52+
if [[ "$NO_OLM" == "true" ]]; then
53+
uninstall_helm_resources
54+
else
4355
uninstall_odlm
4456
uninstall_cs_operator
4557
uninstall_nss
4658
fi
47-
delete_rbac_resource
59+
4860
delete_webhook
4961
delete_unavailable_apiservice
5062
if [[ $RETAIN == "false" ]]; then
5163
delete_tenant_ns
5264
else
5365
cleanup_extra_resources
5466
fi
67+
68+
success "Tenant uninstall process completed."
5569
}
5670

5771
function parse_arguments() {
@@ -70,13 +84,20 @@ function parse_arguments() {
7084
shift
7185
YQ=$1
7286
;;
87+
--helm)
88+
shift
89+
HELM=$1
90+
;;
7391
--operator-namespace)
7492
shift
7593
OPERATOR_NS=$1
7694
;;
7795
--retain-ns)
7896
RETAIN="true"
7997
;;
98+
--no-olm)
99+
NO_OLM="true"
100+
;;
80101
-f)
81102
FORCE_DELETE=1
82103
;;
@@ -107,7 +128,9 @@ function print_usage() {
107128
echo "Options:"
108129
echo " --oc string Optional. File path to oc CLI. Default uses oc in your PATH"
109130
echo " --yq string Optional. File path to yq CLI. Default uses yq in your PATH"
131+
echo " --helm string Optional. File path to helm CLI. Default uses helm in your PATH"
110132
echo " --operator-namespace string Required. Namespace to uninstall Foundational services operators and the whole tenant."
133+
echo " --no-olm Optional. Uninstall Foundational services operators and resources installed via Helm."
111134
echo " -f Optional. Enable force delete. It will take much more time if you add this label, we suggest run this script without -f label first"
112135
echo " --retain-ns Optional. Prevents script from deleting tenant namespaces during uninstall."
113136
echo " -v, --debug integer Optional. Verbosity of logs. Default is 0. Set to 1 for debug logs"
@@ -123,6 +146,9 @@ function pre_req() {
123146

124147
check_command "${OC}"
125148
check_command "${YQ}"
149+
if [[ "$NO_OLM" == "true" ]]; then
150+
check_command "${HELM}"
151+
fi
126152
check_yq_version
127153

128154
# Checking oc command logged in
@@ -142,70 +168,77 @@ function pre_req() {
142168
fi
143169
}
144170

171+
145172
function set_tenant_namespaces() {
146-
# check if user want to cleanup operatorNamespace
147173
for ns in ${OPERATOR_NS//,/ }; do
148-
# if this namespace is operatorNamespace
149-
temp_namespace=$(${OC} get -n "$ns" configmap namespace-scope -o jsonpath='{.data.namespaces}' --ignore-not-found)
150-
if [ "$temp_namespace" != "" ]; then
151-
if [ "$TENANT_NAMESPACES" == "" ]; then
152-
TENANT_NAMESPACES=$temp_namespace
153-
OPERATOR_NS_LIST=$ns
154-
else
155-
TENANT_NAMESPACES="${TENANT_NAMESPACES},${temp_namespace}"
156-
OPERATOR_NS_LIST="${OPERATOR_NS_LIST},${ns}"
157-
fi
158-
continue
159-
fi
160-
161-
# if this namespace is servicesNamespace
174+
# Get operatorNamespace and servicesNamespace from CommonService CR
162175
operator_ns=$(${OC} get -n "$ns" commonservice common-service -o jsonpath='{.spec.operatorNamespace}' --ignore-not-found)
163176
services_ns=$(${OC} get -n "$ns" commonservice common-service -o jsonpath='{.spec.servicesNamespace}' --ignore-not-found)
164-
if [ "$services_ns" == "$ns" ]; then
165-
temp_namespace=$(${OC} get -n "$operator_ns" configmap namespace-scope -o jsonpath='{.data.namespaces}' --ignore-not-found)
166-
if [ "$TENANT_NAMESPACES" == "" ]; then
177+
178+
# Get tenant namespaces from namespace-scope ConfigMap
179+
temp_namespace=$(${OC} get -n "$operator_ns" configmap namespace-scope -o jsonpath='{.data.namespaces}' --ignore-not-found)
180+
# Append temp_namespace if not empty
181+
if [[ -n "$temp_namespace" ]]; then
182+
if [[ -z "$TENANT_NAMESPACES" ]]; then
167183
TENANT_NAMESPACES=$temp_namespace
168184
OPERATOR_NS_LIST=$operator_ns
169185
else
170186
TENANT_NAMESPACES="${TENANT_NAMESPACES},${temp_namespace}"
171187
OPERATOR_NS_LIST="${OPERATOR_NS_LIST},${operator_ns}"
172188
fi
173-
continue
174189
fi
175190

176-
# if this namespace neither operatorNamespace nor serviceNamsespace
177-
if [ "$TENANT_NAMESPACES" == "" ]; then
191+
# In NO_OLM mode, and no namespace-scope configmap, get WATCH_NAMESPACE from cs-operator deployment
192+
if [[ -z "$temp_namespace" && "$NO_OLM" == "true" ]]; then
193+
watch_ns=$(${OC} get deployment ibm-common-service-operator -n "$operator_ns" \
194+
-o jsonpath='{.spec.template.spec.containers[?(@.name=="ibm-common-service-operator")].env[?(@.name=="WATCH_NAMESPACE")].value}' --ignore-not-found)
195+
if [[ -n "$watch_ns" ]]; then
196+
if [[ -z "$TENANT_NAMESPACES" ]]; then
197+
TENANT_NAMESPACES=$watch_ns
198+
OPERATOR_NS_LIST=$operator_ns
199+
else
200+
TENANT_NAMESPACES="${TENANT_NAMESPACES},${watch_ns}"
201+
OPERATOR_NS_LIST="${OPERATOR_NS_LIST},${operator_ns}"
202+
fi
203+
fi
204+
fi
205+
206+
# If still empty, fallback to ns
207+
if [[ -z "$TENANT_NAMESPACES" ]]; then
178208
TENANT_NAMESPACES=$ns
179209
else
180210
TENANT_NAMESPACES="${TENANT_NAMESPACES},${ns}"
181211
fi
182212
done
183213

184-
# delete duplicate namespace in TENANT_NAMESPACES and OPERATOR_NS_LIST
185-
TENANT_NAMESPACES=$(echo "$TENANT_NAMESPACES" | sed -e 's/,/\n/g' | sort -u | tr "\r\n" "," | sed '$ s/,$//')
186-
OPERATOR_NS_LIST=$(echo "$OPERATOR_NS_LIST" | sed -e 's/,/\n/g' | sort -u | tr "\r\n" "," | sed '$ s/,$//')
214+
# Remove empty entries and duplicates
215+
TENANT_NAMESPACES=$(echo "$TENANT_NAMESPACES" | sed 's/^,*//;s/,*$//' | sed 's/,,*/,/g' | sed -e 's/,/\n/g' | sort -u | tr "\r\n" "," | sed '$ s/,$//')
216+
OPERATOR_NS_LIST=$(echo "$OPERATOR_NS_LIST" | sed 's/^,*//;s/,*$//' | sed 's/,,*/,/g' | sed -e 's/,/\n/g' | sort -u | tr "\r\n" "," | sed '$ s/,$//')
217+
187218
info "Tenant namespaces are: $TENANT_NAMESPACES"
188219
}
189220

190-
function uninstall_odlm() {
191-
title "Uninstalling OperandRequests and ODLM"
221+
222+
function uninstall_odlm_resource() {
223+
title "Uninstalling odlm resoource"
192224

193225
local grep_args=""
226+
info "Cleaning up OperandRequests in tenant namespaces"
194227
for ns in ${TENANT_NAMESPACES//,/ }; do
195228
local opreq=$(${OC} get -n "$ns" operandrequests --no-headers | cut -d ' ' -f1)
196229
if [ "$opreq" != "" ]; then
230+
echo "Deleting OperandRequests ${opreq//$'\n'/ } in namespace: $ns"
197231
${OC} delete -n "$ns" operandrequests ${opreq//$'\n'/ } --timeout=60s
198232
fi
199-
grep_args="${grep_args}-e $ns "
200233
done
201234

202235
if [ "$grep_args" == "" ]; then
203236
grep_args='no-operand-requests'
204237
fi
205238

206239
for ns in ${TENANT_NAMESPACES//,/ }; do
207-
local condition="${OC} get operandrequests -n ${ns} --no-headers | cut -d ' ' -f1 | grep -w ${grep_args} || echo Success"
208-
local retries=20
240+
local condition="${OC} get operandrequests -n ${ns} --no-headers 2>/dev/null | wc -l | grep '0'"
241+
local retries=30
209242
local sleep_time=10
210243
local total_time_mins=$(( sleep_time * retries / 60))
211244
local wait_message="Waiting for all OperandRequests in tenant namespaces:${ns} to be deleted"
@@ -216,7 +249,42 @@ function uninstall_odlm() {
216249
wait_for_condition "${condition}" ${retries} ${sleep_time} "${wait_message}" "${success_message}" "${error_message}"
217250
done
218251

219-
for ns in ${TENANT_NAMESPACES//,/ }; do
252+
info "Cleaning up remaining ODLM resources in tenant namespaces"
253+
254+
for ns in ${TENANT_NAMESPACES//,/ }; do
255+
local opreq=$(${OC} get -n "$ns" operandregistry --no-headers | cut -d ' ' -f1)
256+
if [ "$opreq" != "" ]; then
257+
${OC} delete -n "$ns" operandregistry ${opreq//$'\n'/ } --timeout=60s
258+
fi
259+
done
260+
261+
for ns in ${TENANT_NAMESPACES//,/ }; do
262+
local opreq=$(${OC} get -n "$ns" operandconfig --no-headers | cut -d ' ' -f1)
263+
if [ "$opreq" != "" ]; then
264+
${OC} delete -n "$ns" operandconfig ${opreq//$'\n'/ } --timeout=60s
265+
fi
266+
done
267+
268+
for ns in ${TENANT_NAMESPACES//,/ }; do
269+
local opreq=$(${OC} get -n "$ns" operandbindinfo --no-headers | cut -d ' ' -f1)
270+
if [ "$opreq" != "" ]; then
271+
${OC} delete -n "$ns" operandbindinfo ${opreq//$'\n'/ } --timeout=60s
272+
fi
273+
done
274+
275+
for ns in ${TENANT_NAMESPACES//,/ }; do
276+
local opreq=$(${OC} get -n "$ns" operatorconfig --no-headers | cut -d ' ' -f1)
277+
if [ "$opreq" != "" ]; then
278+
${OC} delete -n "$ns" operatorconfig ${opreq//$'\n'/ } --timeout=60s
279+
fi
280+
done
281+
}
282+
283+
function uninstall_odlm() {
284+
title "Uninstalling ODLM"
285+
286+
local grep_args=""
287+
for ns in ${TENANT_NAMESPACES//,/ }; do
220288
local sub=$(fetch_sub_from_package ibm-odlm $ns)
221289
if [ "$sub" != "" ]; then
222290
${OC} delete --ignore-not-found -n "$ns" sub "$sub"
@@ -245,18 +313,26 @@ function uninstall_cs_operator() {
245313
done
246314
}
247315

248-
function uninstall_nss() {
316+
function uninstall_nss_resource() {
249317
title "Uninstall ibm-namespace-scope-operator"
250318

251319
for ns in ${TENANT_NAMESPACES//,/ }; do
252-
${OC} delete --ignore-not-found nss -n "$ns" common-service --timeout=30s
320+
${OC} delete --ignore-not-found namespacescope -n "$ns" common-service --timeout=30s
321+
${OC} delete --ignore-not-found configmap -n "$ns" namespace-scope --timeout=30s
253322
for op_ns in ${OPERATOR_NS_LIST//,/ }; do
254323
${OC} delete --ignore-not-found rolebinding -n "$ns" "nss-managed-role-from-$op_ns"
255324
${OC} delete --ignore-not-found role -n "$ns" "nss-managed-role-from-$op_ns"
256325
${OC} delete --ignore-not-found rolebinding -n "$ns" "nss-runtime-managed-role-from-$op_ns"
257326
${OC} delete --ignore-not-found role -n "$ns" "nss-runtime-managed-role-from-$op_ns"
258327
done
328+
done
329+
}
259330

331+
332+
function uninstall_nss() {
333+
title "Uninstall ibm-namespace-scope-operator"
334+
335+
for ns in ${TENANT_NAMESPACES//,/ }; do
260336
sub=$(fetch_sub_from_package ibm-namespace-scope-operator "$ns")
261337
if [ "$sub" != "" ]; then
262338
${OC} delete --ignore-not-found -n "$ns" sub "$sub"
@@ -273,6 +349,11 @@ function delete_webhook() {
273349
for ns in ${TENANT_NAMESPACES//,/ }; do
274350
${OC} delete ValidatingWebhookConfiguration ibm-common-service-validating-webhook-${ns} --ignore-not-found
275351
${OC} delete MutatingWebhookConfiguration ibm-common-service-webhook-configuration ibm-operandrequest-webhook-configuration namespace-admission-config ibm-operandrequest-webhook-configuration-${ns} --ignore-not-found
352+
if [[ "$NO_OLM" == "true" ]]; then
353+
${OC} delete mutatingwebhookconfiguration postgresql-operator-mutating-webhook-configuration-${ns} --ignore-not-found
354+
${OC} delete validatingwebhookconfiguration postgresql-operator-validating-webhook-configuration-${ns} --ignore-not-found
355+
${OC} delete service postgresql-operator-webhook-service -n $ns --ignore-not-found
356+
fi
276357
done
277358
}
278359

@@ -410,16 +491,34 @@ function cleanup_extra_resources() {
410491
${OC} delete issuer cs-ss-issuer cs-ca-issuer -n $ns --ignore-not-found
411492
${OC} delete certificate cs-ca-certificate -n $ns --ignore-not-found
412493
${OC} delete configmap cloud-native-postgresql-image-list ibm-cpp-config -n $ns --ignore-not-found
413-
${OC} delete secret common-service-db-im-tls-secret postgresql-operator-controller-manager-config cs-ca-certificate-secret common-service-db-tls-secret common-service-db-replica-tls-secret common-service-db-zen-tls-secret -n $ns --ignore-not-found
494+
${OC} delete secret common-service-db-im-tls-secret postgresql-operator-controller-manager-config cs-ca-certificate-secret common-service-db-tls-secret common-service-db-replica-tls-secret common-service-db-zen-tls-secret common-web-ui-cert -n $ns --ignore-not-found
414495
${OC} delete commonservice common-service im-common-service -n $ns --ignore-not-found
415496
${OC} delete operandconfig common-service -n $ns --ignore-not-found
416497
${OC} delete operandregistry common-service -n $ns --ignore-not-found
417498
${OC} delete catalogsource opencloud-operators ibm-cs-install-catalog ibm-cs-iam-catalog -n $ns --ignore-not-found
499+
${OC} delete secret ibm-entitlement-key -n $ns --ignore-not-found
418500
info "Remaining resources (minus package manifests and events) in namespace $ns:"
419501
${OC} get "$(${OC} api-resources --namespaced=true --verbs=list -o name | awk '{printf "%s%s",sep,$0;sep=","}')" --ignore-not-found -n $ns -o=custom-columns=KIND:.kind,NAME:.metadata.name --sort-by='kind' | grep -v PackageManifest | grep -v Event
420502
done
421503
success "Excess resources cleaned up in retained tenant namespaces."
422504
}
423505

424506

507+
function uninstall_helm_resources() {
508+
title "Uninstalling Helm releases in tenant namespaces"
509+
for ns in ${TENANT_NAMESPACES//,/ }; do
510+
local releases=$(${HELM} list -n "$ns" --short)
511+
if [[ "$releases" != "" ]]; then
512+
for release in $releases; do
513+
msg "Uninstalling Helm release: $release from namespace: $ns"
514+
${HELM} uninstall "$release" -n "$ns"
515+
done
516+
else
517+
info "No Helm releases found in namespace: $ns"
518+
fi
519+
done
520+
}
521+
522+
523+
425524
main $*

0 commit comments

Comments
 (0)