@@ -105,6 +105,7 @@ function create_image() {
105
105
function set_image_name() {
106
106
# Set the image name
107
107
IMAGE_NAME=" ${IMAGE_BASE_NAME} -${IMAGE_VERSION} "
108
+ echo " Image name: ${IMAGE_NAME} "
108
109
export IMAGE_NAME
109
110
}
110
111
@@ -114,6 +115,19 @@ function create_image_from_prebuilt_artifact() {
114
115
# Set the IMAGE_NAME
115
116
set_image_name
116
117
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
+
117
131
echo " Pulling the podvm image from the provided path"
118
132
image_src=" /tmp/image"
119
133
extraction_destination_path=" /image"
@@ -188,6 +202,39 @@ function create_image_from_prebuilt_artifact() {
188
202
echo " GCP image created successfully from prebuilt artifact"
189
203
}
190
204
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
+
191
238
# function to delete the image
192
239
# IMAGE_NAME must be set as an environment variable
193
240
delete_image_using_id () {
0 commit comments