fix(ci): treat only a NAME_UNKNOWN 404 as an image with no tags
next_image_version.py read any 404 on the first tags/list page as a repository not pushed yet, so a wrong registry path or a proxy's 404 page would restart the image at <base>.0 instead of failing. The OCI distribution spec answers a missing repository with 404 and the error code NAME_UNKNOWN, so only that answer now means no tags. RegistryError carried only the readable text of the body, so it now also carries the body's OCI error codes. The tests cover a plain 404 page and a 404 with another code, both of which now raise. Assisted-by: Claude Code
This commit is contained in:
1 parent
6fee0ea8b2
commit
a48cd063ea
2 files changed
+31
-4
No files matched your search
@@ -70,9 +70,18 @@ USER_AGENT = "kamino-next-image-version"
|
|||||||
|
|
||||||
|
|
||||||
class RegistryError(Exception):
|
class RegistryError(Exception):
|
||||||
def __init__(self, url, status, text, challenge=""):
|
def __init__(self, url, status, text, challenge="", codes=()):
|
||||||
super().__init__(f"GET {url}: HTTP {status}: {text}")
|
super().__init__(f"GET {url}: HTTP {status}: {text}")
|
||||||
self.status, self.challenge = status, challenge
|
self.status, self.challenge, self.codes = status, challenge, codes
|
||||||
|
|
||||||
|
|
||||||
|
def _error_codes(raw):
|
||||||
|
"""The OCI error codes of an error body (`{"errors": [{"code": ...}]}`), or () for any other body."""
|
||||||
|
try:
|
||||||
|
errors = json.loads(raw).get("errors") or []
|
||||||
|
return tuple(e.get("code") for e in errors if isinstance(e, dict))
|
||||||
|
except (ValueError, AttributeError, TypeError):
|
||||||
|
return ()
|
||||||
|
|
||||||
|
|
||||||
def _readable(raw):
|
def _readable(raw):
|
||||||
@@ -92,7 +101,9 @@ def _fetch(url, headers):
|
|||||||
except urllib.error.HTTPError as exc:
|
except urllib.error.HTTPError as exc:
|
||||||
raw = exc.read(20000).decode("utf-8", errors="replace")
|
raw = exc.read(20000).decode("utf-8", errors="replace")
|
||||||
challenge = exc.headers.get("WWW-Authenticate", "")
|
challenge = exc.headers.get("WWW-Authenticate", "")
|
||||||
raise RegistryError(url, exc.code, _readable(raw), challenge) from exc
|
raise RegistryError(
|
||||||
|
url, exc.code, _readable(raw), challenge, _error_codes(raw)
|
||||||
|
) from exc
|
||||||
|
|
||||||
|
|
||||||
class Registry:
|
class Registry:
|
||||||
@@ -139,7 +150,8 @@ class Registry:
|
|||||||
body, headers = self._get(path)
|
body, headers = self._get(path)
|
||||||
except RegistryError as exc:
|
except RegistryError as exc:
|
||||||
# A repository not pushed yet is 404 NAME_UNKNOWN (OCI distribution spec): no tags.
|
# A repository not pushed yet is 404 NAME_UNKNOWN (OCI distribution spec): no tags.
|
||||||
if exc.status == 404 and not tags:
|
# Any other 404, such as a wrong registry path, must fail rather than restart at .0.
|
||||||
|
if exc.status == 404 and "NAME_UNKNOWN" in exc.codes and not tags:
|
||||||
return []
|
return []
|
||||||
raise
|
raise
|
||||||
tags += body.get("tags") or []
|
tags += body.get("tags") or []
|
||||||
|
|||||||
@@ -105,6 +105,7 @@ class FakeRegistry(http.server.BaseHTTPRequestHandler):
|
|||||||
no_realm = False
|
no_realm = False
|
||||||
empty_token = False
|
empty_token = False
|
||||||
unknown = False # the repository does not exist yet
|
unknown = False # the repository does not exist yet
|
||||||
|
not_found = None # a 404 body without NAME_UNKNOWN
|
||||||
agents = None # a fresh list per test, set in setUp
|
agents = None # a fresh list per test, set in setUp
|
||||||
|
|
||||||
def do_GET(self):
|
def do_GET(self):
|
||||||
@@ -128,6 +129,8 @@ class FakeRegistry(http.server.BaseHTTPRequestHandler):
|
|||||||
b"{}",
|
b"{}",
|
||||||
extra={"WWW-Authenticate": f'Bearer realm="{realm}",service="reg"'},
|
extra={"WWW-Authenticate": f'Bearer realm="{realm}",service="reg"'},
|
||||||
)
|
)
|
||||||
|
elif self.not_found is not None:
|
||||||
|
self.reply(404, self.not_found, "text/plain")
|
||||||
elif self.unknown:
|
elif self.unknown:
|
||||||
body = {"errors": [{"code": "NAME_UNKNOWN", "message": "unknown"}]}
|
body = {"errors": [{"code": "NAME_UNKNOWN", "message": "unknown"}]}
|
||||||
self.reply(404, json.dumps(body).encode())
|
self.reply(404, json.dumps(body).encode())
|
||||||
@@ -155,6 +158,7 @@ class RegistryClient(unittest.TestCase):
|
|||||||
def setUp(self):
|
def setUp(self):
|
||||||
FakeRegistry.refuse = FakeRegistry.refuse_token = FakeRegistry.no_realm = False
|
FakeRegistry.refuse = FakeRegistry.refuse_token = FakeRegistry.no_realm = False
|
||||||
FakeRegistry.empty_token = FakeRegistry.unknown = False
|
FakeRegistry.empty_token = FakeRegistry.unknown = False
|
||||||
|
FakeRegistry.not_found = None
|
||||||
FakeRegistry.agents = []
|
FakeRegistry.agents = []
|
||||||
server = QuietServer(("127.0.0.1", 0), FakeRegistry)
|
server = QuietServer(("127.0.0.1", 0), FakeRegistry)
|
||||||
threading.Thread(target=server.serve_forever, daemon=True).start()
|
threading.Thread(target=server.serve_forever, daemon=True).start()
|
||||||
@@ -176,6 +180,17 @@ class RegistryClient(unittest.TestCase):
|
|||||||
niv.next_version("0.1", self.registry.tags(), "c", no_label), "0.1.0"
|
niv.next_version("0.1", self.registry.tags(), "c", no_label), "0.1.0"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_a_404_without_name_unknown_is_a_registry_error(self):
|
||||||
|
for body in (
|
||||||
|
b"404 page not found",
|
||||||
|
b'{"errors":[{"code":"NOT_FOUND","message":"no such route"}]}',
|
||||||
|
):
|
||||||
|
with self.subTest(body=body):
|
||||||
|
FakeRegistry.not_found = body
|
||||||
|
with self.assertRaises(niv.RegistryError) as caught:
|
||||||
|
self.registry.tags()
|
||||||
|
self.assertEqual(caught.exception.status, 404)
|
||||||
|
|
||||||
def test_a_missing_manifest_is_still_a_registry_error(self):
|
def test_a_missing_manifest_is_still_a_registry_error(self):
|
||||||
FakeRegistry.unknown = True
|
FakeRegistry.unknown = True
|
||||||
with self.assertRaises(niv.RegistryError) as caught:
|
with self.assertRaises(niv.RegistryError) as caught:
|
||||||
|
|||||||
Reference in new issue
Block a user