forked from istio/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanup-cache
executable file
·59 lines (47 loc) · 1.67 KB
/
cleanup-cache
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
DIR="${ROOT}/scripts"
. ${DIR}/all-utilities || { echo "Cannot load Bash utilities" ; exit 1 ; }
ARGS=()
CLUSTER_NAME='jenkins-cluster'
JOB_TEMPLATE="${ROOT}/scripts/cleanup.yaml.template"
PROJECT_ID='istio-testing'
SELECTOR='role=build'
TMP_DIR="$(mktemp -d /tmp/bazel-cleanup.XXXXX)"
ZONE='us-central1-f'
while getopts :c:p:s:z: arg; do
case ${arg} in
c) CLUSTER_NAME="${OPTARG}";;
p) PROJECT_ID="${OPTARG}";;
s) SELECTOR="${OPTARG}";;
z) ZONE="${OPTARG}";;
*) error_exit "Unrecognized argument -${OPTARG}";;
esac
done
function generate_job_yaml() {
local node="${1}"
local yaml="${2}"
local uuids=(${node//-/ })
local uuid="${uuids[-2]}-${uuids[-1]}"
cp "${JOB_TEMPLATE}" "${yaml}"
sed -i "s|{UUID}|${uuid}|g" "${yaml}" \
|| error_exit "Could not generate yaml"
sed -i "s|{CLUSTER_NODE}|${node}|g" "${yaml}" \
|| error_exit "Could not generate yaml"
echo "Created ${yaml}"
}
[[ -n "${PROJECT_ID}" ]] && ARGS+=(--project "${PROJECT_ID}")
[[ -n "${ZONE}" ]] && ARGS+=(--zone "${ZONE}")
echo 'Getting credentials for Kubernetes cluster'
gcloud container clusters get-credentials ${CLUSTER_NAME} ${ARGS[@]}\
|| error_exit 'Could not get kubectl config for cluster'
CLUSTER_NODES="$(kubectl get nodes \
--selector=${SELECTOR} \
-o jsonpath='{.items[*].metadata.name}')"
[[ -z "${CLUSTER_NODES}" ]] && error_exit 'Could not find Jenkins nodes.'
for CLUSTER_NODE in ${CLUSTER_NODES[@]}; do
YAML="${TMP_DIR}/${CLUSTER_NODE}.yaml"
generate_job_yaml "${CLUSTER_NODE}" "${YAML}"
kubectl apply -f "${YAML}" \
|| error_exit "Could not create a job on node ${CLUSTER_NODE}"
done