From 44901a6e5e183165322a183f48e4b9fe68323f82 Mon Sep 17 00:00:00 2001 From: Dusty Mabe Date: Fri, 24 Jan 2025 16:11:24 -0500 Subject: [PATCH] cosalib/container_manifest.py: account for images that are repo:tag enhance the garbage collection for the repo:tag we are trying to create a container image manifest for. In this case we were getting an error because someone had pulled the quay.io/coreos-assembler/coreos-assembler:main image down from quay. It wasn't a manifest listed repo:tag so `podman manifest exists` wasn't returning true, but the create would fail. The updated code here should handle this case. This enhances the previous effort in 01e4e55. --- src/cosalib/container_manifest.py | 43 +++++++++++++++++-------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/src/cosalib/container_manifest.py b/src/cosalib/container_manifest.py index f7307beaa3..5e2f5a8093 100644 --- a/src/cosalib/container_manifest.py +++ b/src/cosalib/container_manifest.py @@ -20,31 +20,36 @@ def create_local_container_manifest(repo, tag, images) -> dict: return json.loads(manifest_info) -def local_container_manifest_exists(repo, tag): +def local_container_manifest_or_image_exists(repo, tag): ''' - Delete local manifest list + Delete local manifest list or image associated with repo:tag @param repo str registry repository - @param tag str manifest tag + @param tag str tag ''' - cmd = ["podman", "manifest", "exists", f"{repo}:{tag}"] - cp = runcmd(cmd, check=False) - # The commands returns 0 (exists), 1 (doesn't exist), 125 (other error) - if cp.returncode == 125: - if cp.stdout: - print(f" STDOUT: {cp.stdout.decode()}") - if cp.stderr: - print(f" STDERR: {cp.stderr.decode()}") - raise Exception("Error encountered when checking if manifest exists") - return cp.returncode == 0 + cmds = [["podman", "manifest", "exists", f"{repo}:{tag}"], + ["podman", "image", "exists", f"{repo}:{tag}"]] + for cmd in cmds: + cp = runcmd(cmd, check=False) + # The commands returns 0 (exists), 1 (doesn't exist), 125 (other error) + if cp.returncode == 125: + if cp.stdout: + print(f" STDOUT: {cp.stdout.decode()}") + if cp.stderr: + print(f" STDERR: {cp.stderr.decode()}") + raise Exception("Error encountered when checking if manifest exists") + if cp.returncode == 0: + return True + return False -def delete_local_container_manifest(repo, tag): +def delete_local_container_imgref(repo, tag): ''' - Delete local manifest list + Delete local manifest list or image associated with repo:tag @param repo str registry repository @param tag str manifest tag ''' - cmd = ["podman", "manifest", "rm", f"{repo}:{tag}"] + # Note `podman image rm` will delete a manifest or plain image + cmd = ["podman", "image", "rm", f"{repo}:{tag}"] runcmd(cmd) @@ -74,10 +79,10 @@ def create_and_push_container_manifest(repo, tags, images, v2s2) -> dict: @param images list of image specifications (including transport) @param v2s2 boolean use to force v2s2 format ''' - if local_container_manifest_exists(repo, tags[0]): + if local_container_manifest_or_image_exists(repo, tags[0]): # perhaps left over from a previous failed run -> delete - delete_local_container_manifest(repo, tags[0]) + delete_local_container_imgref(repo, tags[0]) manifest_info = create_local_container_manifest(repo, tags[0], images) push_container_manifest(repo, tags, v2s2) - delete_local_container_manifest(repo, tags[0]) + delete_local_container_imgref(repo, tags[0]) return manifest_info