This is an automated email from the ASF dual-hosted git repository.

rzo1 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/storm.git


The following commit(s) were added to refs/heads/master by this push:
     new 65bd6d0cf0 docker-to-squash.py: keep registry and namespace when 
re-fetching a multi-arch image (#9294)
65bd6d0cf0 is described below

commit 65bd6d0cf05ba27c36534155ac712a01f9eeb483
Author: linzch3 <[email protected]>
AuthorDate: Mon Nov 23 13:55:35 2026 -0500

    docker-to-squash.py: keep registry and namespace when re-fetching a 
multi-arch image (#9294)
    
    Fixes #9290
---
 bin/docker-to-squash.py      | 17 +++++++++++++++--
 bin/test_docker_to_squash.py | 38 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 52 insertions(+), 3 deletions(-)

diff --git a/bin/docker-to-squash.py b/bin/docker-to-squash.py
index ea07530229..4154e0d1d0 100755
--- a/bin/docker-to-squash.py
+++ b/bin/docker-to-squash.py
@@ -354,6 +354,20 @@ def get_pull_fmt_string(pull_format):
     return pull_fmt_string
 
 
+def get_image_without_tag_or_digest(image):
+    """Strip the tag and/or digest from an image reference, keeping the 
registry and namespace.
+
+    A ':' only starts a tag when it comes after the last '/', so a registry 
port
+    (e.g. "localhost:5000/app") is left alone.
+    """
+    name = image.split("@", 1)[0]
+    last_slash = name.rfind("/")
+    last_colon = name.rfind(":")
+    if last_colon > last_slash:
+        name = name[:last_colon]
+    return name
+
+
 def get_manifest_from_docker_image(pull_format, image):
     pull_fmt_string = get_pull_fmt_string(pull_format)
     out, err, rc = shell_command(["skopeo", "inspect", "--raw", 
pull_fmt_string + image], False, True, True)
@@ -369,8 +383,7 @@ def get_manifest_from_docker_image(pull_format, image):
         if not sha:
             raise Exception("Could not find amd64 manifest for image " + image)
 
-        image_without_tag = image.split("/", 1)[-1].split(":", 1)[0]
-        image_and_sha = image_without_tag + "@" + sha
+        image_and_sha = get_image_without_tag_or_digest(image) + "@" + sha
 
         logging.debug("amd64 manifest sha is: %s", sha)
 
diff --git a/bin/test_docker_to_squash.py b/bin/test_docker_to_squash.py
index 65f2b6d442..20cad96a78 100644
--- a/bin/test_docker_to_squash.py
+++ b/bin/test_docker_to_squash.py
@@ -16,7 +16,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from unittest import TestCase
+import json
+from unittest import TestCase, mock
 # import docker-to-squash as dtsq
 dtsq = __import__('docker-to-squash')  # TODO: rename docker-to-squash.py as 
docker_to_squash.py
 
@@ -43,6 +44,41 @@ class Test(TestCase):
             with self.assertRaises(Exception, msg=f"Expected to raise 
cmd={cmd}"):
                 dtsq.shell_command(cmd, True, True, True, timeout_sec=10)
 
+    def test_get_image_without_tag_or_digest(self):
+        cases = {
+            "ubuntu": "ubuntu",
+            "ubuntu:22.04": "ubuntu",
+            "myorg/app:1": "myorg/app",
+            "registry.example.com/team/app:1.0": 
"registry.example.com/team/app",
+            "localhost:5000/app": "localhost:5000/app",
+            "localhost:5000/team/app:1.0": "localhost:5000/team/app",
+            "myorg/app@sha256:abc": "myorg/app",
+            "myorg/app:1@sha256:abc": "myorg/app",
+        }
+        for image, expected in cases.items():
+            self.assertEqual(expected, 
dtsq.get_image_without_tag_or_digest(image), image)
+
+    def 
test_get_manifest_from_docker_image_keeps_repository_for_manifest_list(self):
+        manifest_list = json.dumps({"manifests": [
+            {"digest": "sha256:arm", "platform": {"architecture": "arm64"}},
+            {"digest": "sha256:amd", "platform": {"architecture": "amd64"}},
+        ]}).encode()
+        image_manifest = json.dumps({"layers": []}).encode()
+        inspected = []
+
+        def fake_shell_command(cmd, *args, **kwargs):
+            inspected.append(cmd[-1])
+            return (manifest_list if len(inspected) == 1 else image_manifest), 
b"", 0
+
+        # calculate_string_hash is stubbed: it is handed a str today and can't 
hash it on Python 3,
+        # which is a separate problem from the one tested here.
+        with mock.patch.object(dtsq, "shell_command", 
side_effect=fake_shell_command), \
+                mock.patch.object(dtsq, "calculate_string_hash", 
return_value="hash"):
+            dtsq.get_manifest_from_docker_image("docker", 
"registry.example.com/team/app:1.0")
+
+        self.assertEqual(["docker://registry.example.com/team/app:1.0",
+                          
"docker://registry.example.com/team/app@sha256:amd"], inspected)
+
     # TODO:
     #  def test_read_image_tag_to_hash(self):
     #     """

Reply via email to