Merge "Tolerate missing labels for prepare --tag-from-label"
This commit is contained in:
commit
0ec4793c7e
@ -287,7 +287,8 @@ class DockerImageUploader(ImageUploader):
|
||||
return urllib.parse.urlparse(image)
|
||||
|
||||
@staticmethod
|
||||
def _discover_tag_from_inspect(i, image, tag_from_label=None):
|
||||
def _discover_tag_from_inspect(i, image, tag_from_label=None,
|
||||
fallback_tag=None):
|
||||
labels = i.get('Labels', {})
|
||||
|
||||
label_keys = ', '.join(labels.keys())
|
||||
@ -303,17 +304,23 @@ class DockerImageUploader(ImageUploader):
|
||||
except ValueError as e:
|
||||
raise ImageUploaderException(e)
|
||||
except KeyError as e:
|
||||
raise ImageUploaderException(
|
||||
'Image %s %s. Available labels: %s' %
|
||||
(image, e, label_keys)
|
||||
)
|
||||
if fallback_tag:
|
||||
tag_label = fallback_tag
|
||||
else:
|
||||
raise ImageUploaderException(
|
||||
'Image %s %s. Available labels: %s' %
|
||||
(image, e, label_keys)
|
||||
)
|
||||
else:
|
||||
tag_label = labels.get(tag_from_label)
|
||||
if tag_label is None:
|
||||
raise ImageUploaderException(
|
||||
'Image %s has no label %s. Available labels: %s' %
|
||||
(image, tag_from_label, label_keys)
|
||||
)
|
||||
if fallback_tag:
|
||||
tag_label = fallback_tag
|
||||
else:
|
||||
raise ImageUploaderException(
|
||||
'Image %s has no label %s. Available labels: %s' %
|
||||
(image, tag_from_label, label_keys)
|
||||
)
|
||||
|
||||
# confirm the tag exists by checking for an entry in RepoTags
|
||||
repo_tags = i.get('RepoTags', [])
|
||||
@ -343,11 +350,13 @@ class DockerImageUploader(ImageUploader):
|
||||
versioned_images[image] = versioned_image
|
||||
return versioned_images
|
||||
|
||||
def discover_image_tag(self, image, tag_from_label=None):
|
||||
def discover_image_tag(self, image, tag_from_label=None,
|
||||
fallback_tag=None):
|
||||
image_url = self._image_to_url(image)
|
||||
insecure = self.is_insecure_registry(image_url.netloc)
|
||||
i = self._inspect(image_url.geturl(), insecure)
|
||||
return self._discover_tag_from_inspect(i, image, tag_from_label)
|
||||
return self._discover_tag_from_inspect(i, image, tag_from_label,
|
||||
fallback_tag)
|
||||
|
||||
def cleanup(self, local_images):
|
||||
dockerc = Client(base_url='unix://var/run/docker.sock', version='auto')
|
||||
@ -424,5 +433,12 @@ def discover_tag_from_inspect(args):
|
||||
image_url = DockerImageUploader._image_to_url(image)
|
||||
insecure = image_url.netloc in insecure_registries
|
||||
i = DockerImageUploader._inspect(image_url.geturl(), insecure)
|
||||
if ':' in image_url.path:
|
||||
# break out the tag from the url to be the fallback tag
|
||||
path = image.rpartition(':')
|
||||
fallback_tag = path[2]
|
||||
image = path[0]
|
||||
else:
|
||||
fallback_tag = None
|
||||
return image, DockerImageUploader._discover_tag_from_inspect(
|
||||
i, image, tag_from_label)
|
||||
i, image, tag_from_label, fallback_tag)
|
||||
|
@ -317,6 +317,20 @@ class TestDockerImageUploader(base.TestCase):
|
||||
('docker.io/t/foo', '{release}-{version}', sr))
|
||||
)
|
||||
|
||||
# simple label -> tag with fallback
|
||||
self.assertEqual(
|
||||
('docker.io/t/foo', 'a'),
|
||||
image_uploader.discover_tag_from_inspect(
|
||||
('docker.io/t/foo:a', 'bar', sr))
|
||||
)
|
||||
|
||||
# templated labels -> tag with fallback
|
||||
self.assertEqual(
|
||||
('docker.io/t/foo', 'a'),
|
||||
image_uploader.discover_tag_from_inspect(
|
||||
('docker.io/t/foo:a', '{releases}-{versions}', sr))
|
||||
)
|
||||
|
||||
# Invalid template
|
||||
self.assertRaises(
|
||||
ImageUploaderException,
|
||||
|
Loading…
Reference in New Issue
Block a user