Change tags API endpoint to use new registry model interface
This commit is contained in:
parent
8225c61a1f
commit
bc99dd7963
7 changed files with 148 additions and 841 deletions
|
@ -1,20 +1,40 @@
|
|||
""" Manage the tags of a repository. """
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
from datetime import datetime
|
||||
from flask import request, abort
|
||||
|
||||
from auth.auth_context import get_authenticated_user
|
||||
from data.model import DataModelException
|
||||
from data.registry_model import registry_model
|
||||
from endpoints.api import (resource, nickname, require_repo_read, require_repo_write,
|
||||
RepositoryParamResource, log_action, validate_json_request, path_param,
|
||||
parse_args, query_param, truthy_bool, disallow_for_app_repositories)
|
||||
from endpoints.api.tag_models_interface import Repository
|
||||
from endpoints.api.tag_models_pre_oci import pre_oci_model as model
|
||||
from endpoints.api.image import image_dict
|
||||
from endpoints.exception import NotFound, InvalidRequest
|
||||
from endpoints.v2.manifest import _generate_and_store_manifest
|
||||
from util.names import TAG_ERROR, TAG_REGEX
|
||||
|
||||
|
||||
def _tag_dict(tag):
|
||||
tag_info = {
|
||||
'name': tag.name,
|
||||
'reversion': tag.reversion,
|
||||
}
|
||||
|
||||
if tag.lifetime_start_ts > 0:
|
||||
tag_info['start_ts'] = tag.lifetime_start_ts
|
||||
|
||||
if tag.lifetime_end_ts > 0:
|
||||
tag_info['end_ts'] = tag.lifetime_end_ts
|
||||
|
||||
if tag.manifest_digest:
|
||||
tag_info['manifest_digest'] = tag.manifest_digest
|
||||
|
||||
if tag.legacy_image:
|
||||
tag_info['docker_image_id'] = tag.legacy_image.docker_image_id
|
||||
|
||||
return tag_info
|
||||
|
||||
|
||||
@resource('/v1/repository/<apirepopath:repository>/tag/')
|
||||
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
||||
class ListRepositoryTags(RepositoryParamResource):
|
||||
|
@ -33,17 +53,17 @@ class ListRepositoryTags(RepositoryParamResource):
|
|||
page = max(1, parsed_args.get('page', 1))
|
||||
limit = min(100, max(1, parsed_args.get('limit', 50)))
|
||||
|
||||
tag_history = model.list_repository_tag_history(namespace_name=namespace,
|
||||
repository_name=repository, page=page,
|
||||
size=limit, specific_tag=specific_tag)
|
||||
|
||||
if not tag_history:
|
||||
repo_ref = registry_model.lookup_repository(namespace, repository)
|
||||
if repo_ref is None:
|
||||
raise NotFound()
|
||||
|
||||
history, has_more = registry_model.list_repository_tag_history(repo_ref, page=page,
|
||||
size=limit,
|
||||
specific_tag_name=specific_tag)
|
||||
return {
|
||||
'tags': [tag.to_dict() for tag in tag_history.tags],
|
||||
'tags': [_tag_dict(tag) for tag in history],
|
||||
'page': page,
|
||||
'has_additional': tag_history.more,
|
||||
'has_additional': has_more,
|
||||
}
|
||||
|
||||
|
||||
|
@ -75,59 +95,67 @@ class RepositoryTag(RepositoryParamResource):
|
|||
@validate_json_request('ChangeTag')
|
||||
def put(self, namespace, repository, tag):
|
||||
""" Change which image a tag points to or create a new tag."""
|
||||
|
||||
if not TAG_REGEX.match(tag):
|
||||
abort(400, TAG_ERROR)
|
||||
|
||||
repo = model.get_repo(namespace, repository)
|
||||
if not repo:
|
||||
repo_ref = registry_model.lookup_repository(namespace, repository)
|
||||
if repo_ref is None:
|
||||
raise NotFound()
|
||||
|
||||
if 'expiration' in request.get_json():
|
||||
tag_ref = registry_model.get_repo_tag(repo_ref, tag)
|
||||
if tag_ref is None:
|
||||
raise NotFound()
|
||||
|
||||
expiration = request.get_json().get('expiration')
|
||||
expiration_date = None
|
||||
if expiration is not None:
|
||||
try:
|
||||
expiration_date = datetime.utcfromtimestamp(float(expiration))
|
||||
except ValueError:
|
||||
abort(400)
|
||||
try:
|
||||
expiration_date = datetime.utcfromtimestamp(float(expiration))
|
||||
except ValueError:
|
||||
abort(400)
|
||||
|
||||
if expiration_date <= datetime.now():
|
||||
abort(400)
|
||||
if expiration_date <= datetime.now():
|
||||
abort(400)
|
||||
|
||||
existing_end_ts, ok = model.change_repository_tag_expiration(namespace, repository, tag,
|
||||
expiration_date)
|
||||
existing_end_ts, ok = registry_model.change_repository_tag_expiration(tag_ref,
|
||||
expiration_date)
|
||||
if ok:
|
||||
if not (existing_end_ts is None and expiration_date is None):
|
||||
log_action('change_tag_expiration', namespace, {
|
||||
'username': get_authenticated_user().username,
|
||||
'repo': repository,
|
||||
'tag': tag,
|
||||
'namespace': namespace,
|
||||
'expiration_date': expiration_date,
|
||||
'old_expiration_date': existing_end_ts
|
||||
}, repo_name=repository)
|
||||
if not (existing_end_ts is None and expiration_date is None):
|
||||
log_action('change_tag_expiration', namespace, {
|
||||
'username': get_authenticated_user().username,
|
||||
'repo': repository,
|
||||
'tag': tag,
|
||||
'namespace': namespace,
|
||||
'expiration_date': expiration_date,
|
||||
'old_expiration_date': existing_end_ts
|
||||
}, repo_name=repository)
|
||||
else:
|
||||
raise InvalidRequest('Could not update tag expiration; Tag has probably changed')
|
||||
|
||||
if 'image' in request.get_json():
|
||||
existing_tag = registry_model.get_repo_tag(repo_ref, tag, include_legacy_image=True)
|
||||
|
||||
image_id = request.get_json()['image']
|
||||
image = model.get_repository_image(namespace, repository, image_id)
|
||||
image = registry_model.get_legacy_image(repo_ref, image_id)
|
||||
if image is None:
|
||||
raise NotFound()
|
||||
|
||||
original_image_id = model.get_repo_tag_image(repo, tag)
|
||||
model.create_or_update_tag(namespace, repository, tag, image_id)
|
||||
if not registry_model.retarget_tag(repo_ref, tag, image):
|
||||
raise InvalidRequest('Could not move tag')
|
||||
|
||||
username = get_authenticated_user().username
|
||||
log_action('move_tag' if original_image_id else 'create_tag', namespace, {
|
||||
|
||||
log_action('move_tag' if existing_tag else 'create_tag', namespace, {
|
||||
'username': username,
|
||||
'repo': repository,
|
||||
'tag': tag,
|
||||
'namespace': namespace,
|
||||
'image': image_id,
|
||||
'original_image': original_image_id
|
||||
'original_image': existing_tag.legacy_image.docker_image_id if existing_tag else None,
|
||||
}, repo_name=repository)
|
||||
|
||||
# TODO(jschorr): Move this into the retarget_tag call
|
||||
_generate_and_store_manifest(namespace, repository, tag)
|
||||
|
||||
return 'Updated', 201
|
||||
|
@ -137,7 +165,11 @@ class RepositoryTag(RepositoryParamResource):
|
|||
@nickname('deleteFullTag')
|
||||
def delete(self, namespace, repository, tag):
|
||||
""" Delete the specified repository tag. """
|
||||
model.delete_tag(namespace, repository, tag)
|
||||
repo_ref = registry_model.lookup_repository(namespace, repository)
|
||||
if repo_ref is None:
|
||||
raise NotFound()
|
||||
|
||||
registry_model.delete_tag(repo_ref, tag)
|
||||
|
||||
username = get_authenticated_user().username
|
||||
log_action('delete_tag', namespace,
|
||||
|
@ -163,37 +195,28 @@ class RepositoryTagImages(RepositoryParamResource):
|
|||
type=truthy_bool, default=False)
|
||||
def get(self, namespace, repository, tag, parsed_args):
|
||||
""" List the images for the specified repository tag. """
|
||||
try:
|
||||
tag_image = model.get_repo_tag_image(
|
||||
Repository(namespace_name=namespace, repository_name=repository), tag)
|
||||
except DataModelException:
|
||||
repo_ref = registry_model.lookup_repository(namespace, repository)
|
||||
if repo_ref is None:
|
||||
raise NotFound()
|
||||
|
||||
if tag_image is None:
|
||||
tag_ref = registry_model.get_repo_tag(repo_ref, tag, include_legacy_image=True)
|
||||
if tag_ref is None:
|
||||
raise NotFound()
|
||||
|
||||
# Find all the parent images for the tag.
|
||||
parent_images = model.get_parent_images(namespace, repository, tag_image.docker_image_id)
|
||||
all_images = [tag_image] + list(parent_images)
|
||||
image_map = {image.docker_image_id: image for image in all_images}
|
||||
skip_set = set()
|
||||
image_id = tag_ref.legacy_image.docker_image_id
|
||||
|
||||
# Filter the images returned to those not found in the ancestry of any of the other tags in
|
||||
# the repository.
|
||||
all_images = None
|
||||
if parsed_args['owned']:
|
||||
all_tags = model.list_repository_tags(namespace, repository)
|
||||
for current_tag in all_tags:
|
||||
if current_tag.name == tag:
|
||||
continue
|
||||
all_images = registry_model.get_legacy_images_owned_by_tag(tag_ref)
|
||||
else:
|
||||
image_with_parents = registry_model.get_legacy_image(repo_ref, image_id, include_parents=True)
|
||||
if image_with_parents is None:
|
||||
raise NotFound()
|
||||
|
||||
skip_set.add(current_tag.image.ancestor_id)
|
||||
skip_set = skip_set | set(current_tag.image.ancestor_id_list)
|
||||
all_images = [image_with_parents] + image_with_parents.parents
|
||||
|
||||
return {
|
||||
'images': [
|
||||
image.to_dict(image_map) for image in all_images
|
||||
if not parsed_args['owned'] or (image.ancestor_id not in skip_set)
|
||||
]
|
||||
'images': [image_dict(image) for image in all_images],
|
||||
}
|
||||
|
||||
|
||||
|
@ -226,6 +249,9 @@ class RestoreTag(RepositoryParamResource):
|
|||
@validate_json_request('RestoreTag')
|
||||
def post(self, namespace, repository, tag):
|
||||
""" Restores a repository tag back to a previous image in the repository. """
|
||||
repo_ref = registry_model.lookup_repository(namespace, repository)
|
||||
if repo_ref is None:
|
||||
raise NotFound()
|
||||
|
||||
# Restore the tag back to the previous image.
|
||||
image_id = request.get_json()['image']
|
||||
|
@ -239,19 +265,26 @@ class RestoreTag(RepositoryParamResource):
|
|||
'tag': tag,
|
||||
'image': image_id,
|
||||
}
|
||||
repo = Repository(namespace, repository)
|
||||
if manifest_digest is not None:
|
||||
existing_image = model.restore_tag_to_manifest(repo, tag, manifest_digest)
|
||||
else:
|
||||
existing_image = model.restore_tag_to_image(repo, tag, image_id)
|
||||
_generate_and_store_manifest(namespace, repository, tag)
|
||||
|
||||
if existing_image is not None:
|
||||
log_data['original_image'] = existing_image.docker_image_id
|
||||
manifest_or_legacy_image = None
|
||||
if manifest_digest is not None:
|
||||
manifest_or_legacy_image = registry_model.lookup_manifest_by_digest(repo_ref, manifest_digest,
|
||||
allow_dead=True)
|
||||
else:
|
||||
manifest_or_legacy_image = registry_model.get_legacy_image(repo_ref, image_id)
|
||||
|
||||
if manifest_or_legacy_image is None:
|
||||
raise NotFound()
|
||||
|
||||
if not registry_model.retarget_tag(repo_ref, tag, manifest_or_legacy_image, is_reversion=True):
|
||||
raise InvalidRequest('Could not restore tag')
|
||||
|
||||
if manifest_digest is None:
|
||||
# TODO(jschorr): Move this into the retarget_tag call
|
||||
_generate_and_store_manifest(namespace, repository, tag)
|
||||
|
||||
log_action('revert_tag', namespace, log_data, repo_name=repository)
|
||||
|
||||
return {
|
||||
'image_id': image_id,
|
||||
'original_image_id': existing_image.docker_image_id if existing_image else None,
|
||||
}
|
||||
|
|
Reference in a new issue