Merge "Allow glance image name/id support"

This commit is contained in:
Jenkins
2017-08-11 06:17:40 +00:00
committed by Gerrit Code Review
6 changed files with 37 additions and 9 deletions

View File

@@ -116,6 +116,7 @@ class Manager(periodic_task.PeriodicTasks):
try:
image, image_loaded = image_driver.pull_image(
context, repo, tag, image_pull_policy, image_driver_name)
image['repo'], image['tag'] = repo, tag
if not image_loaded:
self.driver.load_image(image['path'])
except exception.ImageNotFound as e:
@@ -145,6 +146,9 @@ class Manager(periodic_task.PeriodicTasks):
try:
limits = limits
rt = self._get_resource_tracker()
if image['driver'] == 'glance':
image['repo'], image['tag'] = self.driver.read_tar_image(
image)
with rt.container_claim(context, container, container.host,
limits):
container = self.driver.create(context, container, image,

View File

@@ -105,6 +105,12 @@ class DockerDriver(driver.ContainerDriver):
with docker_utils.docker_client() as docker:
return docker.images(repo, quiet)
def read_tar_image(self, image):
with docker_utils.docker_client() as docker:
LOG.debug('Reading local tar image %s ', image['path'])
tar_repo, tar_tag = docker.read_tar_image(image['path'])
return tar_repo, tar_tag
def create(self, context, container, image, requested_networks):
sandbox_id = container.get_sandbox_id()
network_standalone = False if sandbox_id else True
@@ -112,9 +118,8 @@ class DockerDriver(driver.ContainerDriver):
with docker_utils.docker_client() as docker:
network_api = zun_network.api(context=context, docker_api=docker)
name = container.name
image = container.image
LOG.debug('Creating container with image %(image)s name %(name)s',
{'image': image, 'name': name})
{'image': image['image'], 'name': name})
if network_standalone:
self._provision_network(context, network_api,
requested_networks)
@@ -150,8 +155,8 @@ class DockerDriver(driver.ContainerDriver):
host_config['restart_policy'] = {'Name': name,
'MaximumRetryCount': count}
kwargs['host_config'] = docker.create_host_config(**host_config)
response = docker.create_container(image, **kwargs)
image_repo = image['repo'] + ":" + image['tag']
response = docker.create_container(image_repo, **kwargs)
container.container_id = response['Id']
if network_standalone:

View File

@@ -10,10 +10,13 @@
# License for the specific language governing permissions and limitations
# under the License.
import contextlib
import re
import six
import tarfile
import docker
from docker import errors
from oslo_utils import encodeutils
from zun.common import exception
import zun.conf
@@ -79,3 +82,13 @@ class DockerHTTPClient(docker.APIClient):
def list_containers(self):
return self.containers(all=True, filters={'name': 'zun-'})
def read_tar_image(self, image_path=None):
with tarfile.open(image_path, 'r') as fil:
fest = fil.extractfile('manifest.json')
data = fest.read()
find_repotag = re.search('\"RepoTags\":\[\"(.+?)\"\]',
encodeutils.safe_decode(data))
if find_repotag:
repo, tag = find_repotag.group(1).split(":")
return repo, tag

View File

@@ -118,7 +118,8 @@ class TestManager(base.TestCase):
mock_create, mock_pull,
mock_save):
container = Container(self.context, **utils.get_test_container())
image = {'image': 'repo', 'path': 'out_path', 'driver': 'glance'}
image = {'image': 'repo', 'path': 'out_path', 'driver': 'glance',
'repo': 'test', 'tag': 'testtag'}
mock_pull.return_value = image, False
mock_create.side_effect = exception.DockerError("Creation Failed")
self.compute_manager._resource_tracker = FakeResourceTracker()
@@ -221,7 +222,9 @@ class TestManager(base.TestCase):
mock_fail,
mock_pull, mock_save):
container = Container(self.context, **utils.get_test_container())
mock_pull.return_value = {'name': 'nginx', 'path': None}, True
image = {'image': 'repo', 'path': 'out_path', 'driver': 'glance',
'repo': 'test', 'tag': 'testtag'}
mock_pull.return_value = image, True
mock_create.side_effect = exception.DockerError(
message="Docker Error occurred")
self.compute_manager._resource_tracker = FakeResourceTracker()
@@ -235,7 +238,7 @@ class TestManager(base.TestCase):
mock_pull.assert_any_call(self.context, container.image, 'latest',
'always', 'glance')
mock_create.assert_called_once_with(
self.context, container, {'name': 'nginx', 'path': None}, networks)
self.context, container, image, networks)
@mock.patch.object(compute_node_tracker.ComputeNodeTracker,
'remove_usage_from_container')

View File

@@ -90,7 +90,7 @@ class TestDockerDriver(base.DriverTestCase):
return_value={'Id1': 'val1', 'key2': 'val2'})
self.mock_docker.create_container = mock.Mock(
return_value={'Id': 'val1', 'key1': 'val2'})
image = {'path': ''}
image = {'path': '', 'image': '', 'repo': '', 'tag': ''}
mock_container = self.mock_default_container
result_container = self.driver.create(self.context, mock_container,
image, [])
@@ -113,7 +113,7 @@ class TestDockerDriver(base.DriverTestCase):
'tty': True,
}
self.mock_docker.create_container.assert_called_once_with(
mock_container.image, **kwargs)
image['repo'] + ":" + image['tag'], **kwargs)
self.assertEqual('val1', result_container.container_id)
self.assertEqual(result_container.status,
consts.CREATED)

View File

@@ -106,3 +106,6 @@ class FakeDriver(driver.ContainerDriver):
@check_container_id
def commit(self, context, container, repository, tag):
pass
def read_tar_image(self, image):
return image.get('repo'), image.get('tag')