Skip to content

Commit

Permalink
Check and return errors while validating science image.
Browse files Browse the repository at this point in the history
  • Loading branch information
draghuram committed Mar 4, 2023
1 parent 246ed4e commit 66d4d13
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
18 changes: 12 additions & 6 deletions servicex_app/servicex/docker_repo_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,29 @@ class DockerRepoAdapter:
def __init__(self, registry_endpoint="https://hub.docker.com"):
self.registry_endpoint = registry_endpoint

def check_image_exists(self, tagged_image: str) -> bool:
def check_image_exists(self, tagged_image: str) -> (bool, str):
"""
Checks that the given Docker image
:param tagged_image: Full Docker image name, e.g. "sslhep/servicex_app:latest".
:return: Whether or not the image exists in the registry.
"""
search_result = re.search("(.+)/(.+):(.+)", tagged_image)
if not search_result or len(search_result.groups()) != 3:
return False
return False, f"Requested transformer docker image is not in the right format: {tagged_image}"

(repo, image, tag) = search_result.groups()

query = f'{self.registry_endpoint}/v2/repositories/{repo}/{image}/tags/{tag}'
r = requests.get(query)

if r.status_code >= 200 and r.status_code < 300:
current_app.logger.info(f"Requested Image: {tagged_image} exists, "
f"last updated {r.json()['last_updated']}")
return True, ""

if r.status_code == 404:
return False
return False, f"Requested transformer docker image doesn't exist: {tagged_image}"

msg = f"Unexpected error in validating transformer docker image ({tagged_image}), status_code: {r.status_code}, msg ({r.content})"
return False, msg

current_app.logger.info(f"Requested Image: {tagged_image} exists, "
f"last updated {r.json()['last_updated']}")
return True
4 changes: 2 additions & 2 deletions servicex_app/servicex/resources/transformation/submit.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ def post(self):
request_rec.image = codegen_transformer_image

if config['TRANSFORMER_VALIDATE_DOCKER_IMAGE']:
if not self.docker_repo_adapter.check_image_exists(request_rec.image):
msg = f"Requested transformer docker image doesn't exist: {request_rec.image}"
exists, msg = self.docker_repo_adapter.check_image_exists(request_rec.image)
if not exists:
current_app.logger.error(msg, extra={'requestId': request_id})
return {'message': msg}, 400

Expand Down

0 comments on commit 66d4d13

Please sign in to comment.