-
Notifications
You must be signed in to change notification settings - Fork 73
Make docker registry independent of deployed environment #775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d2bb332
make docker registry independent of deployed environment
lukasewecker bfea281
Merge branch 'main' into lukas/docker-registry
lukasewecker 3f384bf
address greptile comments
lukasewecker 3a18cfa
add infer_registry_type to module's __all__
lukasewecker 6600f8d
address greptile
lukasewecker 012cae6
run black
lukasewecker ed9d942
Merge branch 'main' into lukas/docker-registry
lukasewecker a56da1e
ran linters as in ci
lukasewecker 1ec5ec6
ran linters as in ci
lukasewecker b24cbb5
fix test
lukasewecker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
model-engine/model_engine_server/infra/repositories/generic_docker_repository.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import re | ||
| from typing import Optional | ||
| from urllib.parse import urlencode | ||
|
|
||
| import requests | ||
| from model_engine_server.common.dtos.docker_repository import BuildImageRequest, BuildImageResponse | ||
| from model_engine_server.core.config import infra_config | ||
| from model_engine_server.core.loggers import logger_name, make_logger | ||
| from model_engine_server.domain.repositories import DockerRepository | ||
|
|
||
| logger = make_logger(logger_name()) | ||
|
|
||
| _REQUEST_TIMEOUT = 10 | ||
|
|
||
|
|
||
| def _parse_www_authenticate(header: str) -> Optional[dict]: | ||
| """Parse a Www-Authenticate Bearer header into realm, service, and scope.""" | ||
| match = re.match(r"Bearer\s+(.*)", header, re.IGNORECASE) | ||
| if not match: | ||
| return None | ||
| params = {} | ||
| for m in re.finditer(r'(\w+)="([^"]*)"', match.group(1)): | ||
| params[m.group(1)] = m.group(2) | ||
| return params if "realm" in params else None | ||
|
|
||
|
|
||
| def _get_token(realm: str, service: Optional[str], scope: Optional[str]) -> Optional[str]: | ||
| """Fetch a bearer token from the registry's token endpoint.""" | ||
| query = {} | ||
| if service: | ||
| query["service"] = service | ||
| if scope: | ||
| query["scope"] = scope | ||
| separator = "&" if "?" in realm else "?" | ||
| url = f"{realm}{separator}{urlencode(query)}" if query else realm | ||
| try: | ||
| resp = requests.get(url, timeout=_REQUEST_TIMEOUT) | ||
| if resp.status_code == 200: | ||
| data = resp.json() | ||
| return data.get("token") or data.get("access_token") | ||
| except (requests.RequestException, ValueError): | ||
| pass | ||
| return None | ||
|
|
||
|
|
||
| class GenericDockerRepository(DockerRepository): | ||
| """Registry-agnostic Docker repository using the OCI Distribution / Docker Registry V2 HTTP API.""" | ||
|
|
||
| def image_exists( | ||
| self, image_tag: str, repository_name: str, aws_profile: Optional[str] = None | ||
| ) -> bool: | ||
| prefix = infra_config().docker_repo_prefix.rstrip("/") | ||
| parts = prefix.split("/", 1) | ||
| registry_host = parts[0] | ||
| path_prefix = parts[1] if len(parts) > 1 else "" | ||
| full_repo = f"{path_prefix}/{repository_name}" if path_prefix else repository_name | ||
| manifest_url = f"https://{registry_host}/v2/{full_repo}/manifests/{image_tag}" | ||
| headers = { | ||
| "Accept": ", ".join( | ||
| [ | ||
| "application/vnd.docker.distribution.manifest.v2+json", | ||
| "application/vnd.oci.image.manifest.v1+json", | ||
| "application/vnd.docker.distribution.manifest.list.v2+json", | ||
| "application/vnd.oci.image.index.v1+json", | ||
| ] | ||
| ) | ||
| } | ||
|
|
||
| try: | ||
| resp = requests.head(manifest_url, headers=headers, timeout=_REQUEST_TIMEOUT) | ||
|
|
||
| if resp.status_code == 200: | ||
| return True | ||
|
|
||
| if resp.status_code == 401: | ||
| www_auth = resp.headers.get("Www-Authenticate", "") | ||
| auth_params = _parse_www_authenticate(www_auth) | ||
| if auth_params: | ||
| token = _get_token( | ||
| realm=auth_params["realm"], | ||
| service=auth_params.get("service"), | ||
| scope=auth_params.get("scope"), | ||
| ) | ||
| if token: | ||
| headers["Authorization"] = f"Bearer {token}" | ||
| resp = requests.head( | ||
| manifest_url, headers=headers, timeout=_REQUEST_TIMEOUT | ||
| ) | ||
| return resp.status_code == 200 | ||
|
|
||
| return False | ||
| except requests.RequestException as e: | ||
| logger.warning(f"Failed to check image existence at {manifest_url}: {e}") | ||
| return False | ||
|
|
||
| def get_image_url(self, image_tag: str, repository_name: str) -> str: | ||
| if self.is_repo_name(repository_name): | ||
| return f"{infra_config().docker_repo_prefix}/{repository_name}:{image_tag}" | ||
| return f"{repository_name}:{image_tag}" | ||
|
|
||
| def build_image(self, image_params: BuildImageRequest) -> BuildImageResponse: | ||
| raise NotImplementedError("GenericDockerRepository does not support building images") | ||
|
|
||
| def get_latest_image_tag(self, repository_name: str) -> str: | ||
| raise NotImplementedError("GenericDockerRepository does not support querying latest tags") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.