Skip to content

Commit cd3716d

Browse files
authored
Merge pull request #369 from snir911/2618
image-generator: fix image creation edge cases
2 parents 163dfd0 + 96cb6bc commit cd3716d

File tree

4 files changed

+15
-13
lines changed

4 files changed

+15
-13
lines changed

config/peerpods/podvm/aws-VM-image-create-job.yaml

+3-2
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ spec:
4444
value: aws
4545
- name: PODVM_DISTRO
4646
value: rhel
47-
- name: IMAGE_NAME
48-
value: "peer-pod-ami"
4947
# - name: INSTANCE_TYPE
5048
# value: "t3.small" # default is t3.small, uncomment and modify if not available in your region
49+
# - name: IMAGE_NAME
50+
# value: "aws-podvm-image-name" # set custom image name for custom image if you wish to avoid its deletion
5151
envFrom:
5252
- secretRef:
5353
name: peer-pods-secret
@@ -59,6 +59,7 @@ spec:
5959
- -c
6060
- |
6161
set -e
62+
[[ ! "${IMAGE_NAME}" ]] && UUID=$(uuidgen) && export IMAGE_NAME="peer-pod-ami-${UUID::6}" && echo "IMAGE_NAME:${IMAGE_NAME}"
6263
dnf install -y make git unzip
6364
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
6465
unzip awscliv2.zip

config/peerpods/podvm/aws-VM-image-delete-job.yaml

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ spec:
2626
env:
2727
- name: PODVM_DISTRO
2828
value: rhel
29-
- name: IMAGE_NAME
30-
value: "peer-pod-ami"
3129
command:
3230
- /bin/sh
3331
- -c
3432
- |
3533
set -e
34+
[[ ! "${PODVM_AMI_ID}" ]] && echo "PODVM_AMI_ID is missing, it's unknown which image to delete" && exit 1
35+
[[ "${IMAGE_NAME}" ]] && echo "IMAGE_NAME:${IMAGE_NAME} is set, it implies image was not automatically created, delete it manually" && exit 0
3636
dnf install -y unzip
3737
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
3838
unzip awscliv2.zip
@@ -41,8 +41,8 @@ spec:
4141
export MAC=$(curl -m 3 -s http://169.254.169.254/latest/meta-data/mac)
4242
[[ ! "${AWS_REGION}" ]] && export AWS_REGION=$(curl -m 30 -s --show-error http://169.254.169.254/latest/meta-data/placement/region)
4343
[[ ! "${AWS_REGION}" ]] && echo "AWS_REGION is missing" && exit 1
44-
export IMAGE_NAME=${IMAGE_NAME:-peer-pod-ami}
45-
PODVM_AMI_ID=$(aws ec2 describe-images --query "Images[*].[ImageId]" --filters "Name=name,Values=${IMAGE_NAME}" --region ${AWS_REGION} --output text) && \
4644
echo "Deleting AMI: ${PODVM_AMI_ID}"
47-
aws ec2 deregister-image --image-id ${PODVM_AMI_ID} --region ${AWS_REGION}
48-
echo "Deleting AMI: ${PODVM_AMI_ID} - DONE"
45+
RES=$(aws ec2 deregister-image --image-id "${PODVM_AMI_ID}" --region "${AWS_REGION}" 2>&1) || ERR=true
46+
echo ${RES}
47+
[[ ${ERR} ]] && [[ "$RES" =~ InvalidAMIID\.(Unavailable|NotFound) ]] # if deregister returned error and image is already deleted, continue
48+
echo "Deleted AMI: ${PODVM_AMI_ID} - DONE"

config/peerpods/podvm/azure-VM-image-create-job.yaml

+3-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ spec:
5050
value: "rhel-raw"
5151
- name: SKU
5252
value: "9_2"
53-
- name: IMAGE_NAME
54-
value: "peer-pod-vmimage"
53+
# - name: IMAGE_NAME
54+
# value: "azure-podvm-image-name" # set custom image name for custom image if you wish to avoid its deletion
5555
envFrom:
5656
- secretRef:
5757
name: peer-pods-secret
@@ -63,6 +63,7 @@ spec:
6363
- -c
6464
- |
6565
set -e
66+
[[ ! "${IMAGE_NAME}" ]] && UUID=$(uuidgen) && export IMAGE_NAME="peer-pod-vmimage-${UUID::6}" && echo "IMAGE_NAME:${IMAGE_NAME}"
6667
rpm --import https://packages.microsoft.com/keys/microsoft.asc
6768
dnf install -y https://packages.microsoft.com/config/rhel/9.0/packages-microsoft-prod.rpm
6869
dnf install -y make git azure-cli unzip

config/peerpods/podvm/azure-VM-image-delete-job.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ spec:
1717
securityContext:
1818
runAsUser: 0 # needed for container mode dnf access
1919
env:
20-
- name: IMAGE_NAME
21-
value: "peer-pod-vmimage"
2220
envFrom:
2321
- secretRef:
2422
name: peer-pods-secret
@@ -30,6 +28,8 @@ spec:
3028
- -c
3129
- |
3230
set -e
31+
[[ ! "${AZURE_IMAGE_ID}" ]] && echo "AZURE_IMAGE_ID is missing, it's unknown which image should be deleted" && exit 1
32+
[[ "${IMAGE_NAME}" ]] && echo "IMAGE_NAME:${IMAGE_NAME} is set, it implies image was not automatically created, delete it manually " && exit 0
3333
rpm --import https://packages.microsoft.com/keys/microsoft.asc
3434
dnf install -y https://packages.microsoft.com/config/rhel/9.0/packages-microsoft-prod.rpm
3535
yum install -y azure-cli
@@ -41,6 +41,6 @@ spec:
4141
[[ ! "${AZURE_CLIENT_SECRET}" ]] && echo "AZURE_CLIENT_SECRET is missing" && exit 1
4242
[[ ! "${AZURE_CLIENT_ID}" ]] && echo "AZURE_CLIENT_ID is missing" && exit 1
4343
az login --service-principal --user=${AZURE_CLIENT_ID} --password=${AZURE_CLIENT_SECRET} --tenant=${AZURE_TENANT_ID} && \
44-
az image delete --image-name "${IMAGE_NAME}" --resource-group "${AZURE_RESOURCE_GROUP}" && \
44+
az image delete --ids "${AZURE_IMAGE_ID}" && \
4545
echo "DONE"
4646
restartPolicy: Never

0 commit comments

Comments
 (0)