Merge "Support auth with docker registry"

This commit is contained in:
Zuul 2019-01-07 02:03:10 +00:00 committed by Gerrit Code Review
commit 92c941b206
3 changed files with 18 additions and 11 deletions

View File

@ -61,6 +61,10 @@ docker_opts = [
help='The default registry from which docker images are '
'pulled. Its value can be the registry domain name '
'(e.g. docker.io) or None.'),
cfg.StrOpt('default_registry_username',
help='The username of the default registry.'),
cfg.StrOpt('default_registry_password',
help='The password of the default registry.'),
]
ALL_OPTS = (docker_opts)

View File

@ -66,9 +66,17 @@ class DockerDriver(driver.ContainerImageDriver):
return None
def _pull_image(self, repo, tag):
auth_config = None
image_ref = docker_image.Reference.parse(repo)
registry, remainder = image_ref.split_hostname()
if (registry and registry == CONF.docker.default_registry and
CONF.docker.default_registry_username):
auth_config = {'username': CONF.docker.default_registry_username,
'password': CONF.docker.default_registry_password}
with docker_utils.docker_client() as docker:
try:
docker.pull(repo, tag=tag)
docker.pull(repo, tag=tag, auth_config=auth_config)
except errors.NotFound as e:
raise exception.ImageNotFound(message=six.text_type(e))
except errors.APIError as e:

View File

@ -68,8 +68,7 @@ class TestDriver(base.BaseTestCase):
ret = self.driver.pull_image(None, 'test_image', 'latest', 'always')
self.assertEqual(({'image': 'test_image', 'path': None}, True), ret)
self.mock_docker.pull.assert_called_once_with(
'test_image',
tag='latest')
'test_image', tag='latest', auth_config=None)
@mock.patch('zun.common.utils.parse_image_name')
@mock.patch.object(driver.DockerDriver,
@ -85,8 +84,7 @@ class TestDriver(base.BaseTestCase):
self.assertRaises(exception.ZunException, self.driver.pull_image,
None, 'repo', 'tag', 'always')
self.mock_docker.pull.assert_called_once_with(
'repo',
tag='tag')
'repo', tag='tag', auth_config=None)
@mock.patch('zun.common.utils.parse_image_name')
@mock.patch.object(driver.DockerDriver,
@ -104,8 +102,7 @@ class TestDriver(base.BaseTestCase):
self.assertRaises(exception.ImageNotFound, self.driver.pull_image,
None, 'repo', 'tag', 'always')
self.mock_docker.pull.assert_called_once_with(
'repo',
tag='tag')
'repo', tag='tag', auth_config=None)
self.assertEqual(1, mock_pull.call_count)
@mock.patch('zun.common.utils.parse_image_name')
@ -124,8 +121,7 @@ class TestDriver(base.BaseTestCase):
self.assertRaises(exception.DockerError, self.driver.pull_image,
None, 'repo', 'tag', 'always')
self.mock_docker.pull.assert_called_once_with(
'repo',
tag='tag')
'repo', tag='tag', auth_config=None)
self.assertEqual(1, mock_pull.call_count)
@mock.patch('zun.common.utils.parse_image_name')
@ -145,8 +141,7 @@ class TestDriver(base.BaseTestCase):
self.assertRaises(exception.ZunException, self.driver.pull_image,
None, 'repo', 'tag', 'always')
self.mock_docker.pull.assert_called_once_with(
'repo',
tag='tag')
'repo', tag='tag', auth_config=None)
self.assertEqual(1, mock_init.call_count)
def test_search_image_success(self):