ci: Handle concurrent deletions in CI - #696
Conversation
This was used in a `cleanup-light-aws-builder-test-amis` job that has since been removed from the pipeline in c2e1e3c
This pipeline exists in the ubuntu-jammy, ubuntu-noble, and ubuntu-resolute pipelines. They all run at the same time. The cleanup of 3 year old images is looking at ALL distributions in each pipeline. This caused failures where one pipeline would delete an AMI and then the other 2 pipelines would try to delete that AMI again and fail. We cannot limit the cleanup to the current pipeline's OS, because we want to clean up old AMIs even after a stemcell line is EOL and its pipeline has been deleted. So this commit makes the cleanup script tolerant to the AMI having been deleted.
WalkthroughThe cleanup task removes the Suggested reviewers: 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Alphasite
left a comment
There was a problem hiding this comment.
i think its functionally fine, but have a few minor nits. My main concern is around snapshot_id, but i figure you did your due diligence
| delete_tolerating_missing() { | ||
| local output | ||
| if output="$("$@" 2>&1)"; then | ||
| [ -n "${output}" ] && echo "${output}" |
There was a problem hiding this comment.
nit: echo should probably be printf
| fi | ||
|
|
||
| case "${output}" in | ||
| *InvalidAMIID.Unavailable*|*InvalidAMIID.NotFound*|*InvalidSnapshot.NotFound*) |
There was a problem hiding this comment.
gemini seems to think there is 1 more condition which may be worth a looksie at:
InvalidSnapshot.InUse Race Condition
When multiple pipelines are sweeping AMIs, they usually follow two steps:
- Deregister the AMI.
- Delete the underlying EBS Snapshots.
Because AWS is eventually consistent, if Pipeline A deregisters the AMI, AWS takes a few moments to "unlock" the underlying snapshot. If Pipeline B hits the snapshot deletion step during this window, AWS will throw InvalidSnapshot.InUse (not NotFound). If this function doesn't tolerate InUse in a concurrent environment, Pipeline B will fail completely.
| # Every stemcell pipeline runs the published sweep against the same account, so two | ||
| # pipelines can select the same AMI before either has deleted it. This handles | ||
| # expected potential errors that could occur on a collision. | ||
| delete_tolerating_missing() { |
There was a problem hiding this comment.
nit: should this be named something like: run_tolerate_missing_errors or something that doesn't specifically mention delete since its also used for deregister.
There was a problem hiding this comment.
Pull request overview
This PR updates the Light AWS AMI cleanup task to tolerate expected “already deleted” errors caused by concurrent pipeline runs cleaning up the same shared AWS account, preventing CI failures when multiple pipelines attempt to delete the same AMI/snapshot.
Changes:
- Add a
delete_tolerating_missingwrapper to treat “not found/unavailable” AWS CLI delete errors as non-fatal. - Use the wrapper for
aws ec2 deregister-imageandaws ec2 delete-snapshotoperations. - Remove the unused
snapshot_idparameter and its associated cleanup/query logic.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
ci/tasks/light-aws/cleanup-ami.yml |
Removes the unused snapshot_id task param to match current script behavior. |
ci/tasks/light-aws/cleanup-ami.sh |
Adds collision-tolerant deletion wrapper and applies it to AMI/snapshot deletion; removes obsolete snapshot-id-based logic. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This pipeline exists in the ubuntu-jammy, ubuntu-noble, and ubuntu-resolute
pipelines. They all run at the same time. The cleanup of 3 year old images is
looking at ALL distributions in each pipeline. This caused failures where one
pipeline would delete an AMI and then the other 2 pipelines would try to delete
that AMI again and fail.
We cannot limit the cleanup to the current pipeline's OS, because we want to
clean up old AMIs even after a stemcell line is EOL and its pipeline has been
deleted. So this commit makes the cleanup script tolerant to the AMI having
been deleted.
This also removes an unused snapshot_id cleanup mechanism.