+# Fetch COE cluster list before fetching active stacks. K8s cluster creates
+# stack that does not match JOB_NAME, therefore ignore them while processing
+# orphaned stacks and handle them separatly.
+# The stack naming scheme is limited in the source code to take only first 20
+# chars from the JOB_NAME, and the rest is randomly generated value for
+# uniqueness:
+# https://github.com/openstack/magnum/blob/master/magnum/drivers/heat/driver.py#L202-L212
+mapfile -t OS_COE_CLUSTERS_ID < <(openstack --os-cloud "${os_cloud}" coe cluster list \
+ -f value -c "uuid" -c "name" \
+ | grep -E '(DELETE_FAILED|UNKNOWN|UNHEALTHY)' | awk '{print $1}')
+
+echo "-----> Active clusters -> stacks"
+# mapfile -t OS_COE_STACKS_ID
+OS_COE_STACKS=()
+for cluster_id in "${OS_COE_CLUSTERS_ID[@]}"; do
+ # find active stacks id associated with the COE cluster
+ stack_id=$(openstack --os-cloud "${os_cloud}" coe cluster show "${cluster_id}" \
+ -f value -c "stack_id")
+ # get the stack name associated with the COE cluster
+ stack_name=$(openstack --os-cloud "${os_cloud}" stack show "${stack_id}" \
+ -f value -c "stack_name")
+ OS_COE_STACKS+=("${stack_id}")
+ echo "clusterid:${cluster_id} -> stackid:${stack_id} stack_name: ${stack_name}"
+done
+
+if [[ ${#OS_COE_STACKS[@]} -gt "0" ]]; then
+ echo "${OS_COE_STACKS[*]}"
+ echo "-----> Active COE cluster stacks"
+ for cstack in "${OS_COE_STACKS[@]}"; do
+ echo "$cstack"
+ done
+fi
+