forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjanitor.py
executable file
·278 lines (237 loc) · 10.1 KB
/
janitor.py
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python
# Copyright 2016 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Clean up resources from gcp projects. """
import argparse
import collections
import datetime
import json
import os
import subprocess
import sys
# A resource that need to be cleared.
Resource = collections.namedtuple('Resource', 'name group condition managed tolerate')
DEMOLISH_ORDER = [
# [WARNING FROM KRZYZACY] : TOUCH THIS WITH CARE!
# ORDER REALLY MATTERS HERE!
Resource('instances', None, 'zone', None, False),
Resource('addresses', None, 'region', None, False),
Resource('disks', None, 'zone', None, False),
Resource('firewall-rules', None, None, None, False),
Resource('routes', None, None, None, False),
Resource('forwarding-rules', None, 'region', None, False),
Resource('target-http-proxies', None, None, None, False),
Resource('target-https-proxies', None, None, None, False),
Resource('url-maps', None, None, None, False),
Resource('backend-services', None, 'region', None, False),
Resource('target-pools', None, 'region', None, False),
Resource('health-checks', None, None, None, False),
Resource('http-health-checks', None, None, None, False),
Resource('instance-groups', None, 'zone', 'Yes', False),
Resource('instance-groups', None, 'zone', 'No', False),
Resource('instance-templates', None, None, None, False),
Resource('networks', 'subnets', 'region', None, True),
Resource('networks', None, '', None, False),
]
def collect(project, age, resource, filt):
""" Collect a list of resources for each condition (zone or region).
Args:
project: The name of a gcp project.
age: Time cutoff from the creation of a resource.
resource: Definition of a type of gcloud resource.
filt: Filter clause for gcloud list command.
Returns:
A dict of condition : list of gcloud resource object.
Raises:
ValueError if json result from gcloud is invalid.
"""
col = collections.defaultdict(list)
cmd = ['gcloud', 'compute', '-q', resource.name]
if resource.group:
cmd.append(resource.group)
cmd.extend([
'list',
'--format=json(name,creationTimestamp.date(tz=UTC),zone,region,isManaged)',
'--filter=%s' % filt,
'--project=%s' % project])
print '%r' % cmd
for item in json.loads(subprocess.check_output(cmd)):
print '%r' % item
if 'name' not in item or 'creationTimestamp' not in item:
raise ValueError('%r' % item)
if resource.condition and resource.condition in item:
colname = item[resource.condition]
else:
colname = ''
if resource.managed:
if 'isManaged' not in item:
raise ValueError(resource.name, resource.managed)
else:
if resource.managed != item['isManaged']:
continue
# Unify datetime to use utc timezone.
created = datetime.datetime.strptime(item['creationTimestamp'], '%Y-%m-%dT%H:%M:%S')
print ('Found %r(%r), %r in %r, created time = %r' %
(resource.name, resource.group, item['name'], colname, item['creationTimestamp']))
if created < age:
print ('Added to janitor list: %r(%r), %r' %
(resource.name, resource.group, item['name']))
col[colname].append(item['name'])
return col
def clear_resources(project, cols, resource):
"""Clear a collection of resource, from collect func above.
Args:
project: The name of a gcp project.
cols: A dict of collection of resource.
resource: Definition of a type of gcloud resource.
Returns:
0 if no error
1 if deletion command fails
"""
err = 0
for col, items in cols.items():
if ARGS.dryrun:
print ('Resource type %r(%r) to be deleted: %r' %
(resource.name, resource.group, list(items)))
continue
manage_key = {'Yes':'managed', 'No':'unmanaged'}
# construct the customized gcloud commend
base = ['gcloud', 'compute', '-q', resource.name]
if resource.group:
base.append(resource.group)
if resource.managed:
base.append(manage_key[resource.managed])
base.append('delete')
base.append('--project=%s' % project)
if resource.condition:
if col:
base.append('--%s=%s' % (resource.condition, col))
else:
base.append('--global')
print 'Call %r' % base
try:
subprocess.check_call(base + list(items))
except subprocess.CalledProcessError as exc:
if not resource.tolerate:
err = 1
print >>sys.stderr, 'Error try to delete resources: %r' % exc
return err
def clean_gke_cluster(project, age, filt):
"""Clean up potential leaking gke cluster"""
# a cluster can be created in one of those three endpoints
endpoints = [
'https://test-container.sandbox.googleapis.com/', # test
'https://staging-container.sandbox.googleapis.com/', # staging
'https://container.googleapis.com/', # prod
]
err = 0
for endpoint in endpoints:
os.environ['CLOUDSDK_API_ENDPOINT_OVERRIDES_CONTAINER'] = endpoint
print "checking endpoint %s" % endpoint
cmd = [
'gcloud', 'container', '-q', 'clusters', 'list',
'--project=%s' % project,
'--filter=%s' % filt,
'--format=json(name,createTime,zone)'
]
print 'running %s' % cmd
output = ''
try:
output = subprocess.check_output(cmd)
except subprocess.CalledProcessError as exc:
print >>sys.stderr, 'Cannot reach endpoint %s with %r, continue' % (endpoint, exc)
continue
for item in json.loads(output):
print 'cluster info: %r' % item
if 'name' not in item or 'createTime' not in item or 'zone' not in item:
print >>sys.stderr, 'name, createTime and zone must present'
raise ValueError('%r' % item)
# The raw createTime string looks like 2017-08-30T18:33:14+00:00
# Which python 2.7 does not support timezones.
# Since age is already in UTC time we'll just strip the timezone part
item['createTime'] = item['createTime'].split('+')[0]
created = datetime.datetime.strptime(
item['createTime'], '%Y-%m-%dT%H:%M:%S')
if created < age:
print ('Found stale gke cluster %r in %r, created time = %r' %
(item['name'], endpoint, item['createTime']))
delete = [
'gcloud', 'container', '-q', 'clusters', 'delete',
item['name'],
'--project=%s' % project,
'--zone=%s' % item['zone'],
]
try:
print 'running %s' % delete
subprocess.check_call(delete)
except subprocess.CalledProcessError as exc:
err = 1
print >>sys.stderr, 'Error try to delete cluster %s: %r' % (item['name'], exc)
return err
def main(project, days, hours, filt):
""" Clean up resources from a gcp project based on it's creation time
Args:
project: The name of a gcp project.
days/hours: days/hours of maximum lifetime of a gcp resource.
filt: Resource instance filters when query.
Returns:
0 if no error
1 if list or delete command fails
"""
print '[=== Start Janitor on project %r ===]' % project
err = 0
age = datetime.datetime.utcnow() - datetime.timedelta(days=days, hours=hours)
for res in DEMOLISH_ORDER:
print 'Try to search for %r with condition %r' % (res.name, res.condition)
try:
col = collect(project, age, res, filt)
if col:
err |= clear_resources(project, col, res)
except (subprocess.CalledProcessError, ValueError):
err |= 1 # keep clean the other resource
print >>sys.stderr, 'Fail to list resource %r from project %r' % (res.name, project)
# try to clean leaking gke cluster
if 'gke' in project:
try:
err |= clean_gke_cluster(project, age, filt)
except ValueError:
err |= 1 # keep clean the other resource
print >>sys.stderr, 'Fail to clean up cluster from project %r' % project
print '[=== Finish Janitor on project %r with status %r ===]' % (project, err)
sys.exit(err)
if __name__ == '__main__':
PARSER = argparse.ArgumentParser(
description='Clean up resources from an expired project')
PARSER.add_argument('--project', help='Project to clean', required=True)
PARSER.add_argument(
'--days', type=int,
help='Clean items more than --days old (added to --hours)')
PARSER.add_argument(
'--hours', type=float,
help='Clean items more than --hours old (added to --days)')
PARSER.add_argument(
'--filter',
default='NOT tags.items:do-not-delete AND NOT name ~ ^default',
help='Filter down to these instances')
PARSER.add_argument(
'--dryrun',
default=False,
action='store_true',
help='list but not delete resources')
ARGS = PARSER.parse_args()
# We want to allow --days=0 and --hours=0, so check against None instead.
if ARGS.days is None and ARGS.hours is None:
print >>sys.stderr, 'must specify --days and/or --hours'
sys.exit(1)
main(ARGS.project, ARGS.days or 0, ARGS.hours or 0, ARGS.filter)