Skip to content

AWS S3 images-ci-cache cleaner [HMS-11071] - #59

Merged
achilleas-k merged 14 commits into
osbuild:mainfrom
achilleas-k:s3-cache-cleanup
Sep 16, 2026
Merged

achilleas-k merged 14 commits into
osbuild:mainfrom
achilleas-k:s3-cache-cleanup

Conversation

@achilleas-k

@achilleas-k achilleas-k commented Sep 4, 2026 •

Copy link
Copy Markdown
Member

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:

  • Blobs in the root are deleted if they are over 2h old. These are images uploaded to create AMIs for testing. The uploaders should be deleting them immediately but, if that fails, this script will handle them.
  • Cached images and metadata with a DeleteAfter date on the info.json that is in the past are deleted. The info.json files are updated with a DeleteAfter timestamp whenever they, and their associated images, are created or found matching a CI image (cache hit).
  • If an info.json file does not have a DeleteAfter tag, the associated image and files are deleted if its mtime is over 2 weeks old.

See also osbuild/image-builder#2660

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.
@achilleas-k
achilleas-k requested a review from a team as a code owner September 4, 2026 11:16
@achilleas-k
achilleas-k requested review from lucasgarfield, ondrejbudai and schuellerf and removed request for a team September 4, 2026 11:16
@achilleas-k achilleas-k changed the title AWS S3 images-ci-cache cleaner AWS S3 images-ci-cache cleaner [HMS-11071] Sep 4, 2026
achilleas-k added a commit to achilleas-k/image-builder that referenced this pull request Sep 4, 2026
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
achilleas-k added a commit to achilleas-k/image-builder that referenced this pull request Sep 4, 2026
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
achilleas-k added a commit to achilleas-k/image-builder that referenced this pull request Sep 4, 2026
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
pull Bot pushed a commit to j3din00b/images that referenced this pull request Sep 5, 2026
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
avitova previously approved these changes Sep 9, 2026

@avitova avitova left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I have two nitpicks, but minor things, I know you mentioned you needed this, so let's merge.

Comment thread s3cache.py Outdated
print(f"[DeleteAfter {delete_after} < {now}]")
prefixes.append(prefix)
continue

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering if we want to print the information about keeping this key here as well?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread s3cache.py Outdated


def delete_objects(s3, objects):
response = s3.delete_objects(Bucket=BUCKET_NAME, Delete=objects)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@achilleas-k

Copy link
Copy Markdown
Member Author

Thanks for the review. Should be fixed now.

@achilleas-k
achilleas-k requested a review from avitova September 15, 2026 09:08
Comment thread s3cache.py Outdated
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)

@supakeen supakeen Sep 15, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

objects is dict but delete_objects no expect dict but list since it slices over it.

Comment thread s3cache.py Outdated
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"]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already available in obj I think.

Comment thread s3cache.py

if "DeleteAfter" in tags:
delete_after = datetime.fromisoformat(tags["DeleteAfter"])
if delete_after < now:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.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).

Comment thread s3cache.py Outdated
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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does API accept empty object? Doesn't really matter because we don't crash on it (lines below).

Comment thread s3cache.py
return parser.parse_args()


def main():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@achilleas-k

achilleas-k commented Sep 15, 2026 •

Copy link
Copy Markdown
Member Author

Thanks for the review. All great comments.

@supakeen supakeen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might've flattened the keys thing a bit more but this looks good now to me!

@achilleas-k
achilleas-k merged commit 135b45d into osbuild:main Sep 16, 2026
2 checks passed
@achilleas-k
achilleas-k deleted the s3-cache-cleanup branch September 16, 2026 09:13
@achilleas-k

Copy link
Copy Markdown
Member Author

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.

@achilleas-k

Copy link
Copy Markdown
Member Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants