forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjanitor.py
More file actions
executable file
·206 lines (175 loc) · 7.15 KB
/
Copy pathjanitor.py
File metadata and controls
executable file
·206 lines (175 loc) · 7.15 KB
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
#!/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 subprocess
import sys
# A resource that need to be cleared.
Resource = collections.namedtuple('Resource', 'name group condition managed')
DEMOLISH_ORDER = [
# Beware of insertion order
Resource('instances', None, 'zone', None),
Resource('addresses', None, 'region', None),
Resource('disks', None, 'zone', None),
Resource('firewall-rules', None, None, None),
Resource('routes', None, None, None),
Resource('forwarding-rules', None, 'region', None),
Resource('target-http-proxies', None, None, None),
Resource('target-https-proxies', None, None, None),
Resource('url-maps', None, None, None),
Resource('backend-services', None, 'region', None),
Resource('target-pools', None, 'region', None),
Resource('health-checks', None, None, None),
Resource('http-health-checks', None, None, None),
Resource('instance-groups', None, 'zone', 'Yes'),
Resource('instance-groups', None, 'zone', 'No'),
Resource('instance-templates', None, None, None),
Resource('networks', 'subnets', 'region', None),
Resource('networks', None, '', None),
]
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:
err = 1
print >>sys.stderr, 'Error try to delete resources: %r' % 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 ValueError:
err |= 1 # keep clean the other resource
print >>sys.stderr, 'Fail to list resource %r from project %r' % (res.name, 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)