2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2017, 2022 The Linux Foundation and others.
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11 # Scans OpenStack for orphaned COE clusters
12 echo "---> Orphaned k8s clusters"
14 os_cloud="${OS_CLOUD:-vex}"
15 jenkins_urls="${JENKINS_URLS:-}"
17 cluster_in_jenkins() {
18 # Usage: cluster_in_jenkins CLUSTER_NAME JENKINS_URL [JENKINS_URL...]
19 # Returns: 0 If CLUSTER_NAME is in Jenkins and 1 if CLUSTER_NAME is not
25 for jenkins in "${@:2}"; do
26 PARAMS="tree=computer[executors[currentExecutable[url]],"
27 PARAMS=$PARAMS"oneOffExecutors[currentExecutable[url]]]"
28 PARAMS=$PARAMS"&xpath=//url&wrapper=builds"
29 JENKINS_URL="$jenkins/computer/api/json?$PARAMS"
30 resp=$(curl -s -w "\\n\\n%{http_code}" --globoff -H "Content-Type:application/json" "$JENKINS_URL")
31 json_data=$(echo "$resp" | head -n1)
32 status=$(echo "$resp" | awk 'END {print $NF}')
34 if [ "$status" != 200 ]; then
35 >&2 echo "ERROR: Failed to fetch data from $JENKINS_URL with status code $status"
40 if [[ "${jenkins}" == *"jenkins."*".org" ]] || [[ "${jenkins}" == *"jenkins."*".io" ]]; then
43 silo=$(echo "$jenkins" | sed 's/\/*$//' | awk -F'/' '{print $NF}')
46 # We purposely want to wordsplit here to combine the arrays
47 # shellcheck disable=SC2206,SC2207
48 builds=(${builds[@]} $(echo "$json_data" | \
49 jq -r '.computer[].executors[].currentExecutable.url' \
50 | grep -v null | awk -F'/' '{print ENVIRON["silo"] "-" $6 "-" $7}')
54 if [[ "${builds[*]}" =~ $CLUSTER_NAME ]]; then
61 # shellcheck disable=SC1090
63 # Check if openstack venv was previously created
64 if [ -f "/tmp/.os_lf_venv" ]; then
65 os_lf_venv=$(cat "/tmp/.os_lf_venv")
68 if [ -d "${os_lf_venv}" ] && [ -f "${os_lf_venv}/bin/openstack" ]; then
69 echo "Re-use existing venv: ${os_lf_venv}"
70 PATH=$os_lf_venv/bin:$PATH
72 lf-activate-venv --python python3 \
75 python-openstackclient \
79 #########################
80 ## FETCH ACTIVE BUILDS ##
81 #########################
82 # Fetch coe cluster list before fetching active stacks.
83 mapfile -t OS_COE_CLUSTERS < <(openstack --os-cloud "$os_cloud" coe cluster list \
84 -f value -c "uuid" -c "name" -c "status" -c "health_status" \
87 ##########################
88 ## DELETE UNUSED STACKS ##
89 ##########################
90 echo "-----> Delete orphaned cluster"
92 # Search for COE clusters not in use by any active Jenkins systems and remove them.
93 for CLUSTER_NAME in "${OS_COE_CLUSTERS[@]}"; do
94 # jenkins_urls intentially needs globbing to be passed a separate params.
95 # shellcheck disable=SC2153,SC2086
96 if cluster_in_jenkins "$CLUSTER_NAME" $jenkins_urls; then
97 # No need to delete stacks if there exists an active build for them
100 echo "Deleting orphaned k8s cluster: $CLUSTER_NAME"
101 openstack --os-cloud "$os_cloud" coe cluster delete "$CLUSTER_NAME"