forked from longtao.wu/droboticflow
Builtin operator images were rebuilt four times after 2026-06-29 but the catalog kept the first digests: recording a release was a manual step that nothing ran. Once the old tags were pruned from CCR, 66 builtin operators pointed at manifests that no longer exist. - build-poc-images-kaniko.sh now runs record_operator_releases.py after operator builds (POC_KANIKO_RECORD_RELEASES, default true): it resolves each pushed tag to its digest, writes release.json with the stable :builtin tag, regenerates the catalog and repository manifests, then runs verify_operator_image_digests.py on the built operators. - verify_operator_image_digests.py fails when any pinned catalog digest is missing from the registry (usable in CI / before deploys). - sync_live_builtin_operator_images.py promotes catalog digests to a running platform (the API keeps live digests over the catalog), only after confirming each digest exists; dry run unless --apply. - scripts/lib/registry_client.py parses repo:tag@sha256 references and handles CCR's Bearer token challenge. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
111 lines
4.6 KiB
Python
111 lines
4.6 KiB
Python
"""Minimal Docker Registry v2 client for checking and resolving operator image references.
|
|
|
|
Credentials come from REGISTRY_USERNAME / REGISTRY_PASSWORD (the same values the deploy
|
|
scripts load from local deploy secrets). Handles Basic auth and the Bearer token challenge
|
|
used by CCR.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import base64
|
|
import json
|
|
import os
|
|
import urllib.error
|
|
import urllib.parse
|
|
import urllib.request
|
|
from dataclasses import dataclass
|
|
from typing import Callable
|
|
|
|
MANIFEST_ACCEPT = ", ".join(
|
|
[
|
|
"application/vnd.oci.image.index.v1+json",
|
|
"application/vnd.oci.image.manifest.v1+json",
|
|
"application/vnd.docker.distribution.manifest.v2+json",
|
|
"application/vnd.docker.distribution.manifest.list.v2+json",
|
|
]
|
|
)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ImageReference:
|
|
host: str
|
|
repository: str
|
|
tag: str = ""
|
|
digest: str = ""
|
|
|
|
@property
|
|
def reference(self) -> str:
|
|
return self.digest or self.tag or "latest"
|
|
|
|
|
|
def parse_image_reference(value: str) -> ImageReference:
|
|
"""Parse host/repo[:tag][@sha256:...]; the tag is stripped from the repository name."""
|
|
text = value.strip()
|
|
name, _, digest = text.partition("@")
|
|
host, _, path = name.partition("/")
|
|
if not path or "." not in host and ":" not in host:
|
|
raise ValueError(f"image reference must include a registry host: {value!r}")
|
|
tag = ""
|
|
last = path.rsplit("/", 1)[-1]
|
|
if ":" in last:
|
|
path, _, tag = path.rpartition(":")
|
|
if digest and not digest.startswith("sha256:"):
|
|
raise ValueError(f"unsupported digest in {value!r}")
|
|
return ImageReference(host=host, repository=path, tag=tag, digest=digest)
|
|
|
|
|
|
Opener = Callable[[urllib.request.Request, float], object]
|
|
|
|
|
|
def _default_opener(request: urllib.request.Request, timeout: float):
|
|
return urllib.request.urlopen(request, timeout=timeout)
|
|
|
|
|
|
class RegistryClient:
|
|
def __init__(self, username: str | None = None, password: str | None = None, opener: Opener | None = None):
|
|
username = os.environ.get("REGISTRY_USERNAME", "") if username is None else username
|
|
password = os.environ.get("REGISTRY_PASSWORD", "") if password is None else password
|
|
self._basic = "Basic " + base64.b64encode(f"{username}:{password}".encode()).decode() if username else ""
|
|
self._open = opener or _default_opener
|
|
self._tokens: dict[tuple[str, str], str] = {}
|
|
|
|
def _bearer(self, challenge: str, repository: str) -> str:
|
|
params = dict(part.split("=", 1) for part in challenge.split(" ", 1)[1].replace('"', "").split(","))
|
|
query = urllib.parse.urlencode({"service": params.get("service", ""), "scope": f"repository:{repository}:pull"})
|
|
request = urllib.request.Request(f"{params['realm']}?{query}", headers={"Authorization": self._basic} if self._basic else {})
|
|
with self._open(request, 30) as response:
|
|
payload = json.loads(response.read())
|
|
return "Bearer " + (payload.get("token") or payload.get("access_token") or "")
|
|
|
|
def head_manifest(self, image: ImageReference) -> tuple[int, str]:
|
|
"""Return (HTTP status, Docker-Content-Digest) for the image's tag or digest."""
|
|
url = f"https://{image.host}/v2/{image.repository}/manifests/{image.reference}"
|
|
key = (image.host, image.repository)
|
|
authorization = self._tokens.get(key, self._basic)
|
|
for _ in range(2):
|
|
headers = {"Accept": MANIFEST_ACCEPT}
|
|
if authorization:
|
|
headers["Authorization"] = authorization
|
|
try:
|
|
with self._open(urllib.request.Request(url, method="HEAD", headers=headers), 30) as response:
|
|
return response.status, response.headers.get("Docker-Content-Digest", "")
|
|
except urllib.error.HTTPError as error:
|
|
challenge = error.headers.get("WWW-Authenticate", "") if error.headers else ""
|
|
if error.code == 401 and challenge.startswith("Bearer") and key not in self._tokens:
|
|
authorization = self._tokens[key] = self._bearer(challenge, image.repository)
|
|
continue
|
|
return error.code, ""
|
|
return 401, ""
|
|
|
|
def exists(self, value: str) -> bool:
|
|
status, _ = self.head_manifest(parse_image_reference(value))
|
|
return status == 200
|
|
|
|
def resolve_digest(self, value: str) -> str:
|
|
"""Resolve host/repo:tag to host/repo@sha256:..., raising when the tag is missing."""
|
|
image = parse_image_reference(value)
|
|
status, digest = self.head_manifest(image)
|
|
if status != 200 or not digest:
|
|
raise LookupError(f"{value}: registry returned {status}")
|
|
return f"{image.host}/{image.repository}@{digest}"
|