@@ -153,7 +153,7 @@ function create_ami_using_packer() {
153153# Function to get the ami id of the newly created image
154154
155155function get_ami_id() {
156- echo " Getting the image id"
156+ echo " Getting the ami id"
157157
158158 # Get the ami id of the newly created image
159159 # If any error occurs, exit the script with an error message
@@ -162,7 +162,7 @@ function get_ami_id() {
162162 AMI_ID=$( aws ec2 describe-images --region " ${AWS_REGION} " --filters " Name=name,Values=${AMI_NAME} " --query ' Images[*].ImageId' --output text) ||
163163 error_exit " Failed to get the ami id"
164164
165- # Set the image id as an environment variable
165+ # Set the ami id as an environment variable
166166 export AMI_ID
167167
168168 echo " ID of the newly created ami: ${AMI_ID} "
@@ -181,7 +181,7 @@ function get_all_ami_ids() {
181181 AMI_ID_LIST=$( aws ec2 describe-images --region " ${AWS_REGION} " --filters " Name=name,Values=${AMI_BASE_NAME} -*" --query ' Images[*].ImageId' --output text) ||
182182 error_exit " Failed to get the ami id list"
183183
184- # Set the image id list as an environment variable
184+ # Set the ami id list as an environment variable
185185 export AMI_ID_LIST
186186
187187 # Display the list of amis
@@ -190,7 +190,7 @@ function get_all_ami_ids() {
190190}
191191
192192# Function to create or update podvm-images configmap with all the amis
193- # Input AMI_ID_LIST is a list of image ids
193+ # Input AMI_ID_LIST is a list of ami ids
194194
195195function create_or_update_image_configmap() {
196196 echo " Creating or updating podvm-images configmap"
@@ -259,14 +259,35 @@ function add_ami_id_annotation_to_peer_pods_cm() {
259259 return
260260 fi
261261
262- # Add the image id as annotation to peer-pods-cm configmap
263- kubectl annotate configmap peer-pods-cm -n openshift-sandboxed-containers-operator \
262+ # Add the ami id as annotation to peer-pods-cm configmap
263+ # Overwrite any existing values
264+ kubectl annotate --overwrite configmap peer-pods-cm -n openshift-sandboxed-containers-operator \
264265 " LATEST_AMI_ID=${AMI_ID} " ||
265266 error_exit " Failed to add the ami id as annotation to peer-pods-cm configmap"
266267
267268 echo " AMI id added as annotation to peer-pods-cm configmap successfully"
268269}
269270
271+ # Function to delete the LATEST_AMI_ID annotation from the peer-pods-cm configmap
272+
273+ function delete_ami_id_annotation_from_peer_pods_cm() {
274+ echo " Deleting ami id annotation from peer-pods-cm configmap"
275+
276+ # Check if the peer-pods-cm configmap exists
277+ if ! kubectl get configmap peer-pods-cm -n openshift-sandboxed-containers-operator > /dev/null 2>&1 ; then
278+ echo " peer-pods-cm configmap does not exist. Skipping deleting the ami id"
279+ return
280+ fi
281+
282+ # Delete the ami id annotation from peer-pods-cm configmap
283+ kubectl annotate configmap peer-pods-cm -n openshift-sandboxed-containers-operator \
284+ " LATEST_AMI_ID-" ||
285+ error_exit " Failed to delete the ami id annotation from peer-pods-cm configmap"
286+
287+ echo " Ami id annotation deleted from peer-pods-cm configmap successfully"
288+ }
289+
290+
270291# Function to create the ami in AWS
271292
272293function create_ami() {
@@ -324,6 +345,9 @@ function delete_ami_using_id() {
324345 aws ec2 deregister-image --region " ${AWS_REGION} " --image-id " ${AMI_ID} " ||
325346 error_exit " Failed to delete the ami"
326347
348+ # Remove the ami id annotation from peer-pods-cm configmap
349+ delete_ami_id_annotation_from_peer_pods_cm
350+
327351}
328352
329353# display help message
0 commit comments