forked from GoogleCloudPlatform/esp-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjanitor.sh
executable file
·114 lines (96 loc) · 3.08 KB
/
janitor.sh
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/bin/bash
# Janitor script triggered by prow to cleanup ESPv2 on serverless resources.
# This includes:
# - Cloud run services
# - Cloud functions
# - Endpoints services
# Fail on any error.
set -eo pipefail
# Set the project id
gcloud config set project cloudesf-testing
gcloud config set run/region us-central1
# Resources older than 1 day should be cleaned up
LIMIT_DATE=$(date -d "1 day ago" +%F)
echo "Cleaning up resources before ${LIMIT_DATE}"
### GKE Cluster ###
GKE_SERVICES=$(gcloud container clusters list --format="value(NAME)" \
--filter="name ~ ^e2e- AND creationTime < ${LIMIT_DATE}" )
for service in ${GKE_SERVICES};
do
echo "Deleting GKE service: ${service}"
gcloud container clusters delete ${service} \
--zone=us-central1-a \
--async \
--quiet
done
### Cloud Run ###
CLOUD_RUN_SERVICES=$(gcloud run services list \
--platform=managed \
--filter="metadata.name ~ ^e2e-test- \
AND metadata.creationTimestamp < ${LIMIT_DATE}" \
--format="value(metadata.name)")
# Note: This variable should NOT be in quotation marks,
# allowing iteration over multi-line string
for service in $CLOUD_RUN_SERVICES ; do
echo "Deleting Cloud Run service: ${service}"
gcloud run services delete ${service} \
--platform=managed \
--quiet
done
echo "Done cleaning up Cloud Run services"
### Cloud Functions ###
GOOGLE_FUNCTIONS=$(gcloud functions list \
--filter="name ~ e2e-test- \
AND updateTime < ${LIMIT_DATE}" \
--format="value(name)")
# Note: This variable should NOT be in quotation marks,
# allowing iteration over multi-line string
for gf in $GOOGLE_FUNCTIONS ; do
echo "Deleting Google Function: ${gf}"
gcloud functions delete ${gf} \
--quiet
done
echo "Done cleaning up Cloud Functions"
### App Engines ###
APP_ENGINES=$(gcloud app services list \
--filter="SERVICE ~ ^e2e-test-" \
--format="value(SERVICE)")
for ap in ${APP_ENGINES} ; do
echo "Deleting App Engine: ${ap}"
gcloud app services delete "${ap}" \
--quiet
done
echo "Done cleaning up App Engines"
### Endpoints Services ###
ENDPOINTS_SERVICES=$(gcloud endpoints services list \
--filter="serviceName ~ ^e2e-test-" \
--format="value(serviceName)")
# Note: This variable should NOT be in quotation marks,
# allowing iteration over multi-line string
for service in $ENDPOINTS_SERVICES ; do
echo "Checking if Endpoints Service is old enough to be deleted: ${service}"
# The endpoints API does not expose creation date, so infer it from the config id.
CONFIG_ID=$(gcloud endpoints configs list \
--service="${service}" \
--limit=1 \
--format="value(id)")
if [ -z "${CONFIG_ID}" ]
then
echo "Cannot determine config id, this is probably a failed rollout"
echo "Cleaning up service"
gcloud endpoints services delete ${service} \
--quiet \
--async
continue
fi
CONFIG_DATE="${CONFIG_ID::-2}"
echo "Found date: ${CONFIG_DATE}"
if [[ "${CONFIG_DATE}" < "${LIMIT_DATE}" ]] ;
then
echo "Cleaning up service"
gcloud endpoints services delete ${service} \
--quiet \
--async
fi
done
echo "Done cleaning up Endpoints Services"