Skip to content

Commit

Permalink
FIX: Delete managed nodes when force is specified
Browse files Browse the repository at this point in the history
  • Loading branch information
Fred78290 committed Oct 10, 2024
1 parent 4cc43d8 commit b419d13
Showing 1 changed file with 38 additions and 16 deletions.
54 changes: 38 additions & 16 deletions bin/delete-masterkube.sh
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,40 @@ fi
prepare_environment

echo_blue_bold "Delete masterkube ${MASTERKUBE} previous instance"

#===========================================================================================================================================
#
#===========================================================================================================================================
function delete_all_vms() {
function all_vm_names() {
local NAMES=()

for NODEINDEX in $(seq ${FIRSTNODE} ${LASTNODE_INDEX})
do
delete_vm_by_name $(get_vm_name ${NODEINDEX}) || true
NAMES+=($(get_vm_name ${NODEINDEX}))
done

echo ${NAMES[@]}
}

#===========================================================================================================================================
#
#===========================================================================================================================================
function delete_all_vms() {
for NAME in $@
do
delete_vm_by_name ${NAME} || true
done
}

#===========================================================================================================================================
#
#===========================================================================================================================================
function list_nodes() {
local ALLNODES=$(kubectl get node -o json --kubeconfig ${TARGET_CLUSTER_LOCATION}/config)

echo ${ALLNODES} | jq -r '.items | .[] | .metadata.annotations["cluster.autoscaler.nodegroup/instance-name"]' | sort -r
}

#===========================================================================================================================================
#
#===========================================================================================================================================
Expand All @@ -160,33 +184,31 @@ function delete_nodes() {

INSTANCENAMES=$(echo ${ALLNODES} | jq -r '.items | .[] | .metadata.annotations["cluster.autoscaler.nodegroup/instance-name"]' | sort -r)

for VM in ${INSTANCENAMES}
do
delete_vm_by_name ${VM} || true
done

delete_vm_by_name ${MASTERKUBE} || true
delete_all_vms ${MASTERKUBE} ${INSTANCENAMES}

wait_jobs_finish
delete_all_vms

rm ${TARGET_CLUSTER_LOCATION}/config
}

#===========================================================================================================================================
#
#===========================================================================================================================================
if [ -f ${TARGET_CONFIG_LOCATION}/buildenv ]; then
source ${TARGET_CONFIG_LOCATION}/buildenv
else
FORCE=YES
fi

if [ ! -f ${TARGET_CLUSTER_LOCATION}/config ]; then
VMNAMES=$(all_vm_names)
FORCE=YES
else
VMNAMES=$(list_nodes)

if [ -f ${TARGET_CONFIG_LOCATION}/buildenv ]; then
source ${TARGET_CONFIG_LOCATION}/buildenv
else
FORCE=YES
fi
fi

if [ "${FORCE}" = "YES" ]; then
delete_all_vms
delete_all_vms ${VMNAMES}
elif [ -f ${TARGET_CLUSTER_LOCATION}/config ]; then
delete_nodes
fi
Expand Down

0 comments on commit b419d13

Please sign in to comment.