Skip to content

Commit 07b321e

Browse files
authored
Fix unsafe container cleanup that could delete images from other runs (#36547)
1 parent 1bf5629 commit 07b321e

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

sdks/python/container/run_validatescontainer.sh

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,25 @@ fi
9999
function cleanup_container {
100100
# Delete the container locally and remotely
101101
docker rmi $CONTAINER:$TAG || echo "Built container image was not removed. Possibly, it was not not saved locally."
102-
for image in $(docker images --format '{{.Repository}}:{{.Tag}}' | grep $PREBUILD_SDK_CONTAINER_REGISTRY_PATH)
102+
103+
for image in $(docker images --format '{{.Repository}}:{{.Tag}}' | grep $PREBUILD_SDK_CONTAINER_REGISTRY_PATH | grep -E "(beam_python_prebuilt_sdk|$TAG)")
103104
do
104105
echo "Deleting Docker image: $image"
105106
docker rmi $image || echo "Failed to remove prebuilt sdk container image"
106107
image_tag="${image##*:}"
108+
107109
digest=$(gcloud container images list-tags $PREBUILD_SDK_CONTAINER_REGISTRY_PATH/beam_python_prebuilt_sdk --filter="tags=$image_tag" --format="get(digest)")
108-
echo "Deleting from GCloud an image with digest: $digest"
109-
gcloud container images delete $PREBUILD_SDK_CONTAINER_REGISTRY_PATH/beam_python_prebuilt_sdk@$digest --force-delete-tags --quiet || echo "Failed to remove prebuilt sdk container image"
110+
111+
echo "Looking for digest for tag '$image_tag', found: '$digest'"
112+
113+
if [[ -n "$digest" && "$digest" =~ ^sha256:[a-f0-9]{64}$ ]]; then
114+
echo "Deleting from GCloud an image with digest: $digest"
115+
gcloud container images delete $PREBUILD_SDK_CONTAINER_REGISTRY_PATH/beam_python_prebuilt_sdk@$digest --force-delete-tags --quiet || echo "Failed to remove prebuilt sdk container image"
116+
else
117+
echo "Skipping deletion of image with invalid or empty digest: '$digest'"
118+
fi
110119
done
120+
111121
# Note: we don't delete the multi-arch containers here because this command only deletes the manifest list with the tag,
112122
# the associated container images can't be deleted because they are not tagged. However, multi-arch containers that are
113123
# older than 6 weeks old are deleted by stale_dataflow_prebuilt_image_cleaner.sh that runs daily.

0 commit comments

Comments
 (0)