Repository navigation
AWS S3 images-ci-cache cleaner [HMS-11071] - #59
Conversation
A new script that walks the image-ci-cache bucket and deletes objects based on specific rules for the bucket. It relies on a tag set by the test scripts in image-builder CI. The docstring describes all the rules for the final form of this script, which aren't implemented yet. The script also doesn't do any deletions yet.
Since we didn't always have the DeleteAfter tag, let's fall back to the mtime to decide if we want to delete an image when it's old.
These are images uploaded for converting to AMIs to boot test and should not stay around for too long.
2e366f6 to
0ac75f2
Compare
Instead of adding metadata, which requires copying a file over itself and can take time, tag an object with a DeleteAfter date that the cloud-cleaner reads to determine if an object should be deleted [1]. Unlike the previous method of modifying objects, this does not change the mtime. However, the action is a single (fast) API call. [1] osbuild/cloud-cleaner#59
Instead of adding metadata, which requires copying a file over itself and can take time, tag an object with a DeleteAfter date that the cloud-cleaner reads to determine if an object should be deleted [1]. Unlike the previous method of modifying objects, this does not change the mtime. However, the action is a single (fast) API call. [1] osbuild/cloud-cleaner#59
Instead of adding metadata, which requires copying a file over itself and can take time, tag an object with a DeleteAfter date that the cloud-cleaner reads to determine if an object should be deleted [1]. Unlike the previous method of modifying objects, this does not change the mtime. However, the action is a single (fast) API call. [1] osbuild/cloud-cleaner#59
Instead of adding metadata, which requires copying a file over itself and can take time, tag an object with a DeleteAfter date that the cloud-cleaner reads to determine if an object should be deleted [1]. Unlike the previous method of modifying objects, this does not change the mtime. However, the action is a single (fast) API call. [1] osbuild/cloud-cleaner#59
avitova
left a comment
There was a problem hiding this comment.
Nice! I have two nitpicks, but minor things, I know you mentioned you needed this, so let's merge.
| print(f"[DeleteAfter {delete_after} < {now}]") | ||
| prefixes.append(prefix) | ||
| continue | ||
|
|
There was a problem hiding this comment.
I was wondering if we want to print the information about keeping this key here as well?
There was a problem hiding this comment.
Oh yeah, good catch. I think the flow here is a bit messy looking at it now. Let me see if I can clean it up.
|
|
||
|
|
||
| def delete_objects(s3, objects): | ||
| response = s3.delete_objects(Bucket=BUCKET_NAME, Delete=objects) |
There was a problem hiding this comment.
This works only for at most 1000 keys per request, if there's more, this would have to be handled. But I assume we won't have more and there is no chance?
There was a problem hiding this comment.
It's possible, actually. A full CI run generates over 1000 images, so if they all expire at the same time, the count will be over 1000.
Either prints KEEPING for objects that will be kept or the reason they will be added to the list of things to delete.
Once everything is collected, if not doing a dry-run, delete the objects and print the keys that have been deleted.
Add it as a separate workflow that only runs once a day. This workflow runs longer than the rest and doesn't need to be running at the hourly frequency.
The KEEPING message wasn't being printed in the case where an object had a DeleteAfter tag and was also not marked for deletion.
0ac75f2 to
4212109
Compare
|
Thanks for the review. Should be fixed now. |
| for page in s3.get_paginator("list_objects_v2").paginate(Bucket=BUCKET_NAME, Prefix=prefix): | ||
| if "Contents" in page: | ||
| objects = {"Objects": [{"Key": obj["Key"]} for obj in page["Contents"]]} | ||
| delete_objects(s3, objects) |
There was a problem hiding this comment.
objects is dict but delete_objects no expect dict but list since it slices over it.
| print("KEEPING") | ||
| else: | ||
| # the object wasn't tagged, fall back to mtime and add it to the deletion list if it's over 2 weeks old | ||
| mtime = s3.head_object(Bucket=BUCKET_NAME, Key=key)["LastModified"] |
There was a problem hiding this comment.
This is already available in obj I think.
|
|
||
| if "DeleteAfter" in tags: | ||
| delete_after = datetime.fromisoformat(tags["DeleteAfter"]) | ||
| if delete_after < now: |
There was a problem hiding this comment.
.fromisoformat can return a naive datetime object depending on what was in the tag. This will then be TypeError if that happens (since now is aware).
| for batch_index in range(0, len(objects), 1000): | ||
| batch_objects = objects[batch_index:batch_index+1000] | ||
|
|
||
| response = s3.delete_objects(Bucket=BUCKET_NAME, Delete=batch_objects) |
There was a problem hiding this comment.
Does API accept empty object? Doesn't really matter because we don't crash on it (lines below).
| return parser.parse_args() | ||
|
|
||
|
|
||
| def main(): |
There was a problem hiding this comment.
This always exits 0 so it always passes in CI?
The s3.delete_objects() function can only handle up to 1000 keys at a time. Slice our deletion list into batches of 1000 when calling the function.
Make sure the two datetimes both have timezone info. 'now' is already timezone-aware. Assume and add UTC to DeleteAfter if it doesn't define one already.
In both cases, we have access to LastModified on the existing obj, so no need for the head call.
4212109 to
b7c2b1a
Compare
|
Thanks for the review. All great comments. |
supakeen
left a comment
There was a problem hiding this comment.
I might've flattened the keys thing a bit more but this looks good now to me!
|
Merging because I want to try this out and fix any issues I find. Local dry-runs work, but I'm sure I'll find some little bug in the first real run. |
This script lists and cleans up cached test images from s3://images-ci-cache.
Cache cleanup for this bucket is handled separately and with special rules:
See also osbuild/image-builder#2660