Skip to content

Commit

Permalink
Delete crossplane CR by patching finalizer (#1893)
Browse files Browse the repository at this point in the history
* delete crossplane CR by patch finalizer

Signed-off-by: YuChen <[email protected]>

* create general resource deletion function

Signed-off-by: YuChen <[email protected]>

* set namespace arg with -n ns or -A

Signed-off-by: YuChen <[email protected]>

* remove -A for patch cluster scoped resources

Signed-off-by: YuChen <[email protected]>

---------

Signed-off-by: YuChen <[email protected]>
  • Loading branch information
YCShen1010 authored Apr 2, 2024
1 parent 1ffb1ce commit e491f94
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
56 changes: 38 additions & 18 deletions cp3pt0-deployment/common/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1053,18 +1053,15 @@ function cleanup_secretshare() {

# TODO: clean up crossplane sub and CR in operator_ns and service_ns
function cleanup_crossplane() {
#check if crossplane operator is installed or not
# check if crossplane operator is installed or not
local is_exist=$($OC get subscription.operators.coreos.com -A --no-headers | (grep ibm-crossplane || echo "fail") | awk '{print $1}')
if [[ $is_exist != "fail" ]]; then
# delete CR
info "cleanup crossplane CR"
${OC} get configuration.pkg.ibm.crossplane.io -A --no-headers | awk '{print $1}' | xargs ${OC} delete --ignore-not-found configuration.pkg.ibm.crossplane.io
${OC} get lock.pkg.ibm.crossplane.io -A --no-headers | awk '{print $1}' | xargs ${OC} delete --ignore-not-found lock.pkg.ibm.crossplane.io
${OC} get ProviderConfig -A --no-headers | awk '{print $1}' | xargs ${OC} delete --ignore-not-found ProviderConfig
local resource_types=("configuration.pkg.ibm.crossplane.io" "lock.pkg.ibm.crossplane.io" "ProviderConfig")
if [[ $is_exist == "fail" ]]; then
# Delete CR
msg "Cleanup crossplane CR"
delete_resources resource_types[@]

sleep 30

# delete Sub
# Delete Sub
info "cleanup crossplane Subscription and ClusterServiceVersion"
local namespace=$($OC get subscription.operators.coreos.com -A --no-headers | (grep ibm-crossplane-operator-app || echo "fail") | awk '{print $1}')
if [[ $namespace != "fail" ]]; then
Expand All @@ -1084,14 +1081,9 @@ function cleanup_OperandBindInfo() {

function cleanup_NamespaceScope() {
local namespace=$1
local resources=("odlm-scope-managedby-odlm" "nss-odlm-scope" "nss-managedby-odlm" "common-service")
for resource in "${resources[@]}"; do
msg "Delete $resource in namespace $namespace..."
if ! ${OC} delete namespacescope $resource -n ${namespace} --ignore-not-found --timeout=10s > /dev/null 2>&1; then
warning "Deletion of $resource failed. Patching finalizer..."
${OC} patch -n ${namespace} namespacescope $resource --type="json" -p '[{"op": "remove", "path":"/metadata/finalizers"}]'
fi
done
resource_types=("namespacescope")

delete_resources resource_types[@] $namespace
}

function cleanup_OperandRequest() {
Expand All @@ -1107,6 +1099,34 @@ function cleanup_deployment() {

wait_for_no_pod ${namespace} ${name}
}

function delete_resources() {
local resource_types=("${!1}")
local namespace=${2:--A}
local namespace_arg="-A"

if [ $namespace != "-A" ]; then
namespace_arg="-n $namespace"
fi

for resource_type in "${resource_types[@]}"; do
msg "Deleting $resource_type resources..."
# Retrieve resources of the specified type
resources=$(${OC} get $resource_type ${namespace_arg} --no-headers --ignore-not-found | awk '{print $1}')
for resource in $resources; do
msg "Deleting $resource..."
if ! ${OC} delete $resource_type $resource ${namespace_arg} --ignore-not-found --timeout=10s > /dev/null 2>&1; then
warning "Deletion of $resource failed. Patching finalizer..."
if [ $namesapce_arg == "-A" ]; then
${OC} patch $resource_type $resource --type="json" -p '[{"op": "remove", "path":"/metadata/finalizers"}]'
else
${OC} patch $resource_type $resource ${namespace_arg} --type="json" -p '[{"op": "remove", "path":"/metadata/finalizers"}]'
fi
fi
done
done
}

# ---------- cleanup functions end ----------#

function get_control_namespace() {
Expand Down
2 changes: 1 addition & 1 deletion cp3pt0-deployment/migrate_tenant.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function main() {
pre_req
prepare_preview_mode

# # TODO check Cloud Pak compatibility
# TODO check Cloud Pak compatibility

# Scale down CS, ODLM and delete OperandReigsrty
# It helps to prevent re-installing licensing and cert-manager services
Expand Down

0 comments on commit e491f94

Please sign in to comment.