-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
264 lines (217 loc) · 10.4 KB
/
main.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
'''
Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
the License. A copy of the License is located at
http://aws.amazon.com/apache2.0/
or in the "license" file accompanying this file. This file 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.
'''
import argparse
import os
import re
from datetime import timedelta, datetime
import boto3
from kubernetes import config, client
REGION = None
DRYRUN = None
IMAGES_TO_KEEP = 300
IMAGES_KEEP_DURATION = "3m"
IGNORE_TAGS_REGEX = None
IGNORE_REPO_REGEX = None
CLUSTERS = []
def initialize():
global REGION
global DRYRUN
global IMAGES_TO_KEEP
global IGNORE_TAGS_REGEX
global IGNORE_REPO_REGEX
global CLUSTERS
global IMAGES_KEEP_DURATION
REGION = os.environ.get('REGION', "None")
DRYRUN = os.environ.get('DRYRUN', "false").lower()
IMAGES_KEEP_DURATION = os.environ.get('IMAGES_KEEP_DURATION', "3m")
CLUSTERS = os.environ.get('CLUSTERS', "None").split(",")
if DRYRUN == "false":
DRYRUN = False
else:
DRYRUN = True
IMAGES_TO_KEEP = int(os.environ.get('IMAGES_TO_KEEP', 100))
IGNORE_TAGS_REGEX = os.environ.get('IGNORE_TAGS_REGEX', "^$")
IGNORE_REPO_REGEX = os.environ.get('IGNORE_REPO_REGEX', "^$")
def handler(event, context):
initialize()
if REGION == "None":
ec2_client = boto3.client('ec2')
available_regions = ec2_client.describe_regions()['Regions']
for region in available_regions:
discover_delete_images(region['RegionName'], CLUSTERS)
else:
discover_delete_images(REGION, CLUSTERS)
def convert_duration_to_timedelta(duration_str):
number = int(re.search(r'\d+', duration_str).group())
if 'd' in duration_str:
return timedelta(days=number)
elif 'w' in duration_str:
return timedelta(weeks=number)
elif 'm' in duration_str:
return timedelta(days=number * 30)
else:
raise ValueError(f'Invalid duration string: {duration_str}')
def get_eks_pods_images(cluster):
config.load_kube_config(context=cluster)
v1 = client.CoreV1Api()
pods = v1.list_pod_for_all_namespaces().items
running_images = []
for pod in pods:
for container in pod.spec.containers:
if container.image not in running_images:
running_images.append(container.image)
return running_images
def discover_delete_images(region_name, clusters):
global time_limit
print("Discovering images in " + region_name)
ecr_client = boto3.client('ecr', region_name=region_name)
repositories = []
describe_repo_paginator = ecr_client.get_paginator('describe_repositories')
for response_listrepopaginator in describe_repo_paginator.paginate():
for repo in response_listrepopaginator['repositories']:
repositories.append(repo)
running_containers = []
for cluster in clusters:
print(f'Discovering running pods in {cluster}')
running_containers.extend(get_eks_pods_images(cluster))
print("Images that are running:")
for image in running_containers:
print(image)
ignore_repo_regex = re.compile(IGNORE_REPO_REGEX)
for repository in repositories:
if re.search(ignore_repo_regex, repository['repositoryUri']):
print("------------------------")
print("Skipping repository: " + repository['repositoryUri'])
continue
print("------------------------")
print("Starting with repository: " + repository['repositoryUri'])
deletesha = []
deletetag = []
tagged_images = []
describe_image_paginator = ecr_client.get_paginator('describe_images')
for response_describe_image_paginator in describe_image_paginator.paginate(
registryId=repository['registryId'],
repositoryName=repository['repositoryName']):
for image in response_describe_image_paginator['imageDetails']:
if 'imageTags' in image:
tagged_images.append(image)
else:
append_to_list(deletesha, image['imageDigest'])
print("Total number of images found: {}".format(len(tagged_images) + len(deletesha)))
print("Number of untagged images found {}".format(len(deletesha)))
tagged_images.sort(key=lambda k: k['imagePushedAt'], reverse=True)
# Get ImageDigest from ImageURL for running images. Do this for every repository
# Converting running_containers to set for efficient lookup
running_containers_set = set(running_containers)
running_sha = []
# Calculate abs point to check how old images are
keep_duration = convert_duration_to_timedelta(IMAGES_KEEP_DURATION)
for image in tagged_images:
time_limit = datetime.now(tz=image['imagePushedAt'].tzinfo) - keep_duration
for tag in image['imageTags']:
imageurl = repository['repositoryUri'] + ":" + tag
if imageurl in running_containers_set: # This is a constant time operation
running_sha.append(image['imageDigest'])
# Remove duplicates in running_sha
running_sha_set = set(running_sha)
print("Number of running images found {}".format(len(running_sha)))
ignore_tags_regex = re.compile(IGNORE_TAGS_REGEX)
for index, image in enumerate(tagged_images):
# lastRecordedPullTime is present only for active images.
last_activity_time = image.get('lastRecordedPullTime')
eligible_for_deletion = False
# stale image, we will decide based on number or push time
if last_activity_time is None:
if image['imagePushedAt'] < time_limit or index >= IMAGES_TO_KEEP:
eligible_for_deletion = True
# active image, only by last pull
elif last_activity_time < time_limit:
eligible_for_deletion = True
if eligible_for_deletion:
for tag in image['imageTags']:
if "latest" not in tag and ignore_tags_regex.search(tag) is None:
if not running_sha_set or image['imageDigest'] not in running_sha_set:
append_to_list(deletesha, image['imageDigest'])
append_to_tag_list(deletetag, {"imageUrl": repository['repositoryUri'] + ":" + tag,
"pushedAt": image["imagePushedAt"],
"pulledAt": image.get('lastRecordedPullTime')})
if deletesha:
print("Number of images to be deleted: {}".format(len(deletesha)))
delete_images(
ecr_client,
deletesha,
deletetag,
repository['registryId'],
repository['repositoryName']
)
else:
print("Nothing to delete in repository : " + repository['repositoryName'])
def append_to_list(image_digest_list, repo_id):
if not {'imageDigest': repo_id} in image_digest_list:
image_digest_list.append({'imageDigest': repo_id})
def append_to_tag_list(tag_list, tag_id):
if not tag_id in tag_list:
tag_list.append(tag_id)
def chunks(repo_list, chunk_size):
"""Yield successive n-sized chunks from l."""
for i in range(0, len(repo_list), chunk_size):
yield repo_list[i:i + chunk_size]
def delete_images(ecr_client, deletesha, deletetag, repo_id, name):
if len(deletesha) >= 1:
## spliting list of images to delete on chunks with 100 images each
## http://docs.aws.amazon.com/AmazonECR/latest/APIReference/API_BatchDeleteImage.html#API_BatchDeleteImage_RequestSyntax
i = 0
for deletesha_chunk in chunks(deletesha, 100):
i += 1
if not DRYRUN:
delete_response = ecr_client.batch_delete_image(
registryId=repo_id,
repositoryName=name,
imageIds=deletesha_chunk
)
print(delete_response)
else:
print("registryId:" + repo_id)
print("repositoryName:" + name)
print("Deleting {} chank of images".format(i))
print("imageIds:", end='')
print(deletesha_chunk)
if deletetag:
print("Image URLs that are marked for deletion:")
for ids in deletetag:
print("- {} - {} - {}".format(ids["imageUrl"], ids["pushedAt"], ids["pulledAt"]))
# Below is the test harness
if __name__ == '__main__':
REQUEST = {"None": "None"}
PARSER = argparse.ArgumentParser(description='Deletes stale ECR images')
PARSER.add_argument('--dryrun', help='Prints the repository to be deleted without deleting them', default='true',
action='store', dest='dryrun')
PARSER.add_argument('-i', '--imagestokeep', help='Number of image tags to keep', default='100', action='store',
dest='imagestokeep')
PARSER.add_argument('-d', '--imagestokeepduration', help='Duration from last push', default='3m', action='store',
dest='imagestokeepduration')
PARSER.add_argument('-r', '--region', help='ECR/ECS region', action='store', dest='region', required=True)
PARSER.add_argument('-re', '--ignoretagsregex', help='Regex of tag names to ignore', default="^$", action='store',
dest='ignoretagsregex')
PARSER.add_argument('-c', '--cluster', nargs='+', help='<Required> Add context to parse', required=True)
PARSER.add_argument('-ir', '--ignorereporegex', help='Regex of repo names to ignore', default="^$", action='store',
dest='ignorereporegex')
ARGS = PARSER.parse_args()
if ARGS.region:
os.environ["REGION"] = ARGS.region
else:
os.environ["REGION"] = "None"
os.environ["DRYRUN"] = ARGS.dryrun.lower()
os.environ["IMAGES_TO_KEEP"] = ARGS.imagestokeep
os.environ["IGNORE_TAGS_REGEX"] = ARGS.ignoretagsregex
os.environ["IGNORE_REPO_REGEX"] = ARGS.ignorereporegex
os.environ["IMAGES_KEEP_DURATION"] = ARGS.imagestokeepduration
os.environ["CLUSTERS"] = ','.join(ARGS.cluster)
handler(REQUEST, None)