Skip to content

Commit ce9465b

Browse files
committed
gcp: adding image check before creating image
As following other providers logic. Signed-off-by: Beraldo Leal <[email protected]>
1 parent fb4cd56 commit ce9465b

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

config/peerpods/podvm/gcp-podvm-image-handler.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ function create_image() {
105105
function set_image_name() {
106106
# Set the image name
107107
IMAGE_NAME="${IMAGE_BASE_NAME}-${IMAGE_VERSION}"
108+
echo "Image name: ${IMAGE_NAME}"
108109
export IMAGE_NAME
109110
}
110111

@@ -114,6 +115,19 @@ function create_image_from_prebuilt_artifact() {
114115
# Set the IMAGE_NAME
115116
set_image_name
116117

118+
image_exists
119+
image_status=$?
120+
121+
if [[ "${image_status}" -eq 0 ]]; then
122+
echo "Image exists. Skipping creation"
123+
return
124+
elif [[ "${image_status}" -eq 2 ]]; then
125+
echo "Deleting image version, before recreating"
126+
delete_image_using_id
127+
fi
128+
129+
echo "Image does not exist. Proceeding to create the image"
130+
117131
echo "Pulling the podvm image from the provided path"
118132
image_src="/tmp/image"
119133
extraction_destination_path="/image"
@@ -188,6 +202,39 @@ function create_image_from_prebuilt_artifact() {
188202
echo "GCP image created successfully from prebuilt artifact"
189203
}
190204

205+
function image_exists() {
206+
echo "Checking if GCP image exists"
207+
208+
local image_path latest_image_id
209+
210+
# This command returns something like this:
211+
# https://www.googleapis.com/compute/v1/projects/my-project/global/images/my-image
212+
# Getting the cutted version to fetch from projects.*
213+
image_path=$(gcloud compute images describe "${IMAGE_NAME}-${IMAGE_VERSION}" \
214+
--project="${GCP_PROJECT_ID}" \
215+
--format='get(selfLink)' 2>/dev/null | cut -d/ -f6-)
216+
217+
latest_image_id=$(kubectl get configmap peer-pods-cm \
218+
-n openshift-sandboxed-containers-operator \
219+
-o jsonpath='{.metadata.annotations.LATEST_IMAGE_ID}')
220+
221+
if [[ -z "${image_path}" && -z "${latest_image_id}" ]]; then
222+
echo "No image in GCP and no record in configmap."
223+
return 1
224+
elif [[ -z "${image_path}" && -n "${latest_image_id}" ]]; then
225+
echo "No image in GCP, but configmap has record (${latest_image_id})."
226+
return 1
227+
# Since GCP supports both cases, lets see if image_path is a suffix of
228+
# latest_image_id.
229+
elif [[ "${image_path}" == *"${latest_image_id}" ]]; then
230+
echo "Image (${latest_image_id}) is up-to-date in configmap."
231+
return 0
232+
else
233+
echo "Image mismatch: GCP image (${image_path}) different from ConfigMap (${latest_image_id})."
234+
return 2
235+
fi
236+
}
237+
191238
# function to delete the image
192239
# IMAGE_NAME must be set as an environment variable
193240
delete_image_using_id() {

0 commit comments

Comments
 (0)