Skip to content

Commit

Permalink
Snapshot deletion validation during KataConfig removal
Browse files Browse the repository at this point in the history
Included a validation for ensuring the Snapshot associated with the AMI was deleted properly

Signed-off-by: Gilad Sid <[email protected]>
  • Loading branch information
gsid100 committed Jan 28, 2025
1 parent d302502 commit 47eac9e
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions config/peerpods/podvm/aws-podvm-image-handler.sh
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,39 @@ function create_ami() {

}

# function to delete the ami
# Function to delete the ami's snapshots
# SNAPSHOT_IDS must be set as an environment variable

function delete_ami_snapshots() {
echo "Deleting AWS AMIs snapshots"

# SNAPSHOT_IDS shouldn't be empty
[[ -z "${SNAPSHOT_IDS}" ]] && error_exit "SNAPSHOT_IDS is empty"

SNAPSHOT_IDS_LIST=($SNAPSHOT_IDS)
for SNAPSHOT_ID in "${SNAPSHOT_IDS_LIST[@]}"; do

# Delete the associated snapshot
aws ec2 delete-snapshot --region "${AWS_REGION}" --snapshot-id "${SNAPSHOT_ID}" ||
error_exit "Failed to delete snapshot '${SNAPSHOT_ID}'"

# Validate the snapshot was deleted
snapshot_description=$(
aws ec2 describe-snapshots \
--region "${AWS_REGION}" \
--filters "Name=snapshot-id,Values='${SNAPSHOT_ID}'" \
--query "Snapshots" \
--output text
) || error_exit "Failed to get the ami's snapshot description"
if [[ -n "$snapshot_description" ]]; then
error_exit "Snapshot '${SNAPSHOT_ID}' still exists after deletion attempt"
else
echo "Snapshot '${SNAPSHOT_ID}' successfully deleted"
fi
done
}

# Function to delete the ami
# AMI_ID must be set as an environment variable

function delete_ami_using_id() {
Expand All @@ -343,7 +375,7 @@ function delete_ami_using_id() {
# AMI_ID shouldn't be empty
[[ -z "${AMI_ID}" ]] && error_exit "AMI_ID is empty"

# Retrieve the associated snapshots
# Retrieve the associated snapshots before deleting the ami
SNAPSHOT_IDS_JSON_PATH='Images[].BlockDeviceMappings[].Ebs.SnapshotId'
SNAPSHOT_IDS=$(
aws ec2 describe-images \
Expand All @@ -352,17 +384,14 @@ function delete_ami_using_id() {
--query "${SNAPSHOT_IDS_JSON_PATH}" \
--output text
) || error_exit "Failed to get the ami's snapshot ids"
export SNAPSHOT_IDS

# Delete the ami
aws ec2 deregister-image --region "${AWS_REGION}" --image-id "${AMI_ID}" ||
error_exit "Failed to delete the ami"

# Delete the associated snapshots
SNAPSHOT_IDS_LIST=($SNAPSHOT_IDS)
for SNAPSHOT_ID in "${SNAPSHOT_IDS_LIST[@]}"; do
aws ec2 delete-snapshot --region "${AWS_REGION}" --snapshot-id "${SNAPSHOT_ID}" ||
error_exit "Failed to delete snapshot '${SNAPSHOT_ID}'"
done
# Delete the amis snapshots
delete_ami_snapshots

# Remove the ami id annotation from peer-pods-cm configmap
delete_ami_id_annotation_from_peer_pods_cm
Expand Down

0 comments on commit 47eac9e

Please sign in to comment.