Commit ebe9381c authored by Giorgenes Gelatti's avatar Giorgenes Gelatti

Refactor tag delete code quality

parent 00ed8ef7
...@@ -26,13 +26,13 @@ module Projects ...@@ -26,13 +26,13 @@ module Projects
def delete_tags(tags_to_delete, tags_by_digest) def delete_tags(tags_to_delete, tags_by_digest)
deleted_digests = group_by_digest(tags_to_delete).select do |digest, tags| deleted_digests = group_by_digest(tags_to_delete).select do |digest, tags|
delete_tag_digest(digest, tags, tags_by_digest[digest]) delete_tag_digest(tags, tags_by_digest[digest])
end end
deleted_digests.values.flatten deleted_digests.values.flatten
end end
def delete_tag_digest(digest, tags, other_tags) def delete_tag_digest(tags, other_tags)
# Issue: https://gitlab.com/gitlab-org/gitlab-foss/issues/21405 # Issue: https://gitlab.com/gitlab-org/gitlab-foss/issues/21405
# we have to remove all tags due # we have to remove all tags due
# to Docker Distribution bug unable # to Docker Distribution bug unable
......
...@@ -24,32 +24,36 @@ module Projects ...@@ -24,32 +24,36 @@ module Projects
dummy_manifest = container_repository.client.generate_empty_manifest(container_repository.path) dummy_manifest = container_repository.client.generate_empty_manifest(container_repository.path)
return error('could not generate manifest') if dummy_manifest.nil? return error('could not generate manifest') if dummy_manifest.nil?
# update the manifests of the tags with the new dummy image deleted_tags = replace_tag_manifests(container_repository, dummy_manifest, tag_names)
deleted_tags = []
tag_digests = [] # Deletes the dummy image
# All created tag digests are the same since they all have the same dummy image.
# a single delete is sufficient to remove all tags with it
if deleted_tags.any? && container_repository.delete_tag_by_digest(deleted_tags.values.first)
success(deleted: deleted_tags.keys)
else
error('could not delete tags')
end
end
# update the manifests of the tags with the new dummy image
def replace_tag_manifests(container_repository, dummy_manifest, tag_names)
deleted_tags = {}
tag_names.each do |name| tag_names.each do |name|
digest = container_repository.client.put_tag(container_repository.path, name, dummy_manifest) digest = container_repository.client.put_tag(container_repository.path, name, dummy_manifest)
next unless digest next unless digest
deleted_tags << name deleted_tags[name] = digest
tag_digests << digest
end end
# make sure the digests are the same (it should always be) # make sure the digests are the same (it should always be)
tag_digests.uniq! digests = deleted_tags.values.uniq
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
Gitlab::Sentry.track_exception(ArgumentError.new('multiple tag digests')) if tag_digests.many? Gitlab::Sentry.track_exception(ArgumentError.new('multiple tag digests')) if digests.many?
# Deletes the dummy image deleted_tags
# All created tag digests are the same since they all have the same dummy image.
# a single delete is sufficient to remove all tags with it
if tag_digests.any? && container_repository.delete_tag_by_digest(tag_digests.first)
success(deleted: deleted_tags)
else
error('could not delete tags')
end
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment