Chore: Upgrade Jenkins-job-builder to 6.3.0
[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
64 lf-activate-venv --python python3 \
65     kubernetes \
66     python-heatclient \
67     python-openstackclient \
68     python-magnumclient
69
70 #########################
71 ## FETCH ACTIVE BUILDS ##
72 #########################
73 # Fetch coe cluster list before fetching active stacks.
74 mapfile -t OS_COE_CLUSTERS < <(openstack --os-cloud "$os_cloud" coe cluster list \
75             -f value -c "uuid" -c "name" -c "status" -c "health_status" \
76             | awk '{print $2}')
77
78 ##########################
79 ## DELETE UNUSED STACKS ##
80 ##########################
81 echo "-----> Delete orphaned cluster"
82
83 # Search for COE clusters not in use by any active Jenkins systems and remove them.
84 for CLUSTER_NAME in "${OS_COE_CLUSTERS[@]}"; do
85     # jenkins_urls intentially needs globbing to be passed a separate params.
86     # shellcheck disable=SC2153,SC2086
87     if cluster_in_jenkins "$CLUSTER_NAME" $jenkins_urls; then
88         # No need to delete stacks if there exists an active build for them
89         continue
90     elif [[ "$CLUSTER_NAME" == *-managed-prod-k8s-* ]] || \
91          [[ "$CLUSTER_NAME" == *-managed-test-k8s-* ]]; then
92         # Don't destroy long lived managed k8s clusters used in Jenkins jobs
93         echo "Not deleting managed cluster: $CLUSTER_NAME"
94         continue
95     else
96         echo "Deleting orphaned k8s cluster: $CLUSTER_NAME"
97         openstack --os-cloud "$os_cloud" coe cluster delete "$CLUSTER_NAME"
98     fi
99 done