33fcaa65e97973cf26378faa3fa468265695078b
[releng/global-jjb.git] / shell / openstack-cleanup-orphaned-k8s-clusters.sh
1 #!/bin/bash -l
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2017, 2022 The Linux Foundation and others.
5 #
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"
13
14 os_cloud="${OS_CLOUD:-vex}"
15 jenkins_urls="${JENKINS_URLS:-}"
16
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
20     # in Jenkins.
21
22     CLUSTER_NAME="${1}"
23
24     builds=()
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}')
33
34         if [ "$status" != 200 ]; then
35             >&2 echo "ERROR: Failed to fetch data from $JENKINS_URL with status code $status"
36             >&2 echo "$resp"
37             exit 1
38         fi
39
40         if [[ "${jenkins}" == *"jenkins."*".org" ]] || [[ "${jenkins}" == *"jenkins."*".io" ]]; then
41             silo="production"
42         else
43             silo=$(echo "$jenkins" | sed 's/\/*$//' | awk -F'/' '{print $NF}')
44         fi
45         export silo
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}')
51         )
52     done
53
54     if [[ "${builds[*]}" =~ $CLUSTER_NAME ]]; then
55         return 0
56     fi
57
58     return 1
59 }
60
61 # shellcheck disable=SC1090
62 source ~/lf-env.sh
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")
66 fi
67
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
71 else
72     lf-activate-venv --python python3 \
73         kubernetes \
74         python-heatclient \
75         python-openstackclient \
76         python-magnumclient
77 fi
78
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" \
85             | awk '{print $2}')
86
87 ##########################
88 ## DELETE UNUSED STACKS ##
89 ##########################
90 echo "-----> Delete orphaned cluster"
91
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
98         continue
99     else
100         echo "Deleting orphaned k8s cluster: $CLUSTER_NAME"
101         openstack --os-cloud "$os_cloud" coe cluster delete "$CLUSTER_NAME"
102     fi
103 done