Skip to content

Commit

Permalink
Use delete_crds_by_name() and force_delete_namespace() for ACM
Browse files Browse the repository at this point in the history
Signed-off-by: nmanos <nmanos@redhat.com>
  • Loading branch information
manosnoam committed Nov 3, 2021
1 parent 90a49b6 commit d5ebd0a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 45 deletions.
13 changes: 7 additions & 6 deletions acm/downstream_deploy_bundle_acm_operator.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ function clean_acm_namespace_and_resources_cluster_c() {
function clean_acm_namespace_and_resources() {
trap_to_debug_commands;

# ${OC} delete multiclusterhub --all || :
# ${OC} delete subs --all || :
# ${OC} delete clusterserviceversion --all || :
# ${OC} delete validatingwebhookconfiguration multiclusterhub-operator-validating-webhook || :
# delete_namespace_and_crds "${ACM_NAMESPACE}" "open-cluster-management" || :

local acm_uninstaller_url="https://raw.githubusercontent.com/open-cluster-management/deploy/master/multiclusterhub/uninstall.sh"
local acm_uninstaller_file="./acm_cleanup.sh"

Expand All @@ -64,6 +58,13 @@ function clean_acm_namespace_and_resources() {
export TARGET_NAMESPACE="${ACM_NAMESPACE}"
${acm_uninstaller_file} || FAILURE "Uninstalling ACM Hub did not complete successfully"

${OC} delete multiclusterhub --all || :
${OC} delete subs --all || :
${OC} delete clusterserviceversion --all || :
${OC} delete validatingwebhookconfiguration multiclusterhub-operator-validating-webhook || :
delete_crds_by_name "open-cluster-management" || :
force_delete_namespace "${ACM_NAMESPACE}" 10m || :

}

# ------------------------------------------
Expand Down
66 changes: 33 additions & 33 deletions helper_functions
Original file line number Diff line number Diff line change
Expand Up @@ -831,54 +831,55 @@ function ocp_login() {

# ------------------------------------------

# Function to delete selected namespace and CRDs on the current KUBECONFIG cluster
function delete_namespace_and_crds() {
# Function to delete all CRDs wich includes the input text on the current cluster
function delete_crds_by_name() {
trap_to_debug_commands;
# Input $1 : Namespace to delete
# Input $2 (Optional) : All CRDs to delete, by searching for *crd_name*
local ns_name=$1
local crd_name=$2

local delete_crds=FALSE
# Input $1 : All CRDs to delete, by searching for *crd_name*
local crd_name=$1

if [[ -n $crd_name ]] ; then
echo "# Searching for existing CRDs of previous ${crd_name} installation on the cluster:"
${OC} get crds |& highlight "${crd_name}" && delete_crds=TRUE || :

# If there are existing CRDs on the cluster - remove them all:
# if [[ $? = 0 ]]; then
if [[ "$delete_crds" = TRUE ]]; then
echo "# Deleting ${crd_name} CRDs in the cluster"
crd_list=$(${OC} get crds -o name | grep -Po "\/\K.*${crd_name}.*")
# Ignoring hanged CRDs on delete, due to K8s bug: https://github.com/kubernetes/kubernetes/issues/60538
${OC} delete --timeout=10s crds $crd_list --ignore-not-found || : # || : to ignore none-zero exit code
fi
fi
local delete_crds=FALSE

echo "# Searching for existing resources (and OLMs) in namespace ${ns_name} :"
# ${OC} get all,olm --all-namespaces | grep --color "${crd_name}" || delete_namespace=FALSE
${OC} get all,olm -n ${ns_name} |& highlight "No resources found" || :
echo "# Searching for existing CRDs of previous ${crd_name} installation on the cluster:"
${OC} get crds |& highlight "${crd_name}" && delete_crds=TRUE || :

# Removing whole namespace (and it's resources):
if [[ ${ns_name} = "default" ]] ; then
echo "# Skipping namespace deletion, since it's the \"default\" namespace."
return
# If there are existing CRDs on the cluster - remove them all:
# if [[ $? = 0 ]]; then
if [[ "$delete_crds" = TRUE ]]; then
echo "# Deleting ${crd_name} CRDs in the cluster"
crd_list=$(${OC} get crds -o name | grep -Po "\/\K.*${crd_name}.*")
# Ignoring hanged CRDs on delete, due to K8s bug: https://github.com/kubernetes/kubernetes/issues/60538
${OC} delete --timeout=10s crds $crd_list --ignore-not-found || : # || : to ignore none-zero exit code
else
echo "# Deleting whole namespace ${ns_name} from the cluster"
force_delete_namespace "${ns_name}"
echo "# No CRD with '${crd_name}' was found in the current cluster"
fi

}


# ------------------------------------------

# Function to force delete namespace
# Function to force delete a namespace
function force_delete_namespace() {
trap_to_debug_commands;
# Input $1 : Namespace to delete
ns_name=$1
local ns_name="$1"

# Input $2 (Optional) : Timeout to wait before forcing namespace deletion
local delete_timeout="${2:-30s}"

if [[ ${ns_name} = "default" ]] ; then
echo "# Skipping namespace deletion, since it's the \"default\" namespace."
return
fi

echo "# Show all resources (and OLMs) in namespace ${ns_name} to be deleted:"
# ${OC} get all,olm --all-namespaces | grep --color "${crd_name}" || delete_namespace=FALSE
${OC} get all,olm -n ${ns_name} |& highlight "No resources found" || :

TITLE "Deleting whole namespace ${ns_name} from the cluster"

${OC} delete --timeout=30s namespace ${ns_name} --ignore-not-found || force_ns_delete=TRUE
${OC} delete --timeout=$delete_timeout namespace ${ns_name} --ignore-not-found || force_ns_delete=TRUE

if [[ "$force_ns_delete" = TRUE && $(${OC} get namespace ${ns_name}) ]]; then
echo "# Warning: Force delete of namespace via API"
Expand Down Expand Up @@ -948,7 +949,6 @@ function install_nginx_service() {

if [[ -n "$target_namespace" ]] ; then
echo "# Create Namespace for Nginx: $target_namespace"
# delete_namespace_and_crds "${target_namespace}"
create_namespace "${target_namespace}"
fi

Expand Down
14 changes: 8 additions & 6 deletions setup_subm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1668,10 +1668,12 @@ function delete_submariner_namespace_and_crds() {
### Run cleanup of previous Submariner namespace and CRDs ###
trap_to_debug_commands;

delete_namespace_and_crds "${SUBM_NAMESPACE}" "submariner"
force_delete_namespace "${SUBM_NAMESPACE}"

delete_crds_by_name "submariner"

# Required if Broker cluster is not a Dataplane cluster as well:
delete_namespace_and_crds "${BROKER_NAMESPACE}"
force_delete_namespace "${BROKER_NAMESPACE}"

}

Expand Down Expand Up @@ -1720,7 +1722,7 @@ function delete_submariner_test_namespaces() {

for ns in "$TEST_NS" "$HEADLESS_TEST_NS" ; do
if [[ -n "$ns" ]]; then
delete_namespace_and_crds "$ns"
force_delete_namespace "$ns"
# create_namespace "$ns"
fi
done
Expand Down Expand Up @@ -1929,8 +1931,8 @@ function delete_old_submariner_images_from_current_cluster() {
$SUBM_IMG_BUNDLE \
; do
TITLE "Deleting image stream: $img_stream"
oc delete imagestream "${img_stream}" -n ${SUBM_NAMESPACE} --ignore-not-found || :
# oc tag -d submariner-operator/${img_stream}
${OC} delete imagestream "${img_stream}" -n ${SUBM_NAMESPACE} --ignore-not-found || :
# ${OC} tag -d submariner-operator/${img_stream}
done

}
Expand Down Expand Up @@ -2968,7 +2970,7 @@ function configure_cluster_custom_registry_secrets() {
)

TITLE "Prune old registry images associated with mirror registry (Brew): https://${BREW_REGISTRY}"
oc adm prune images --registry-url=https://${BREW_REGISTRY} --force-insecure --confirm || :
${OC} adm prune images --registry-url=https://${BREW_REGISTRY} --force-insecure --confirm || :

}

Expand Down

0 comments on commit d5ebd0a

Please sign in to comment.