Move create_image and upload_image_data

This refactoring is for making the code structure more consistent.
In particular, the image driver will be decoupled from compute
manager and align with volume/network driver.
In the new structure, compute manager is mainly for interfacing
with docker driver and docker driver is for interfacing with
image/volume/network drivers.

Change-Id: Ib270062e3a274d2c5d98c05375520ac46b644984
This commit is contained in:
Hongbin Lu 2018-03-19 01:17:13 +00:00
parent 499863e18a
commit 6de85a9a49
6 changed files with 49 additions and 38 deletions

View File

@ -884,8 +884,8 @@ class Manager(periodic_task.PeriodicTasks):
# NOTE(miaohb): Glance is the only driver that support image
# uploading in the current version, so we have hard-coded here.
# https://bugs.launchpad.net/zun/+bug/1697342
snapshot_image = image_driver.create_image(context, repository,
glance.GlanceDriver())
snapshot_image = self.driver.create_image(context, repository,
glance.GlanceDriver())
except exception.DockerError as e:
LOG.error("Error occurred while calling glance "
"create_image API: %s",
@ -902,8 +902,8 @@ class Manager(periodic_task.PeriodicTasks):
def _do_container_image_upload(self, context, snapshot_image,
container_image_id, data, tag):
try:
image_driver.upload_image_data(context, snapshot_image,
tag, data, glance.GlanceDriver())
self.driver.upload_image_data(context, snapshot_image,
tag, data, glance.GlanceDriver())
except Exception as e:
LOG.exception("Unexpected exception while uploading image: %s",
six.text_type(e))

View File

@ -165,6 +165,35 @@ class DockerDriver(driver.ContainerDriver):
'for image: %s', six.text_type(e))
raise exception.ZunException(six.text_type(e))
def create_image(self, context, image_name, image_driver):
img = None
try:
img = image_driver.create_image(context, image_name)
except Exception as e:
LOG.exception('Unknown exception occurred while creating '
'image: %s', six.text_type(e))
raise exception.ZunException(six.text_type(e))
return img
def upload_image_data(self, context, image, image_tag, image_data,
image_driver):
img = None
try:
img = image_driver.update_image(context,
image.id,
tag=image_tag)
# Image data has to match the image format.
# contain format defaults to 'docker';
# disk format defaults to 'qcow2'.
img = image_driver.upload_image_data(context,
image.id,
image_data)
except Exception as e:
LOG.exception('Unknown exception occurred while uploading '
'image: %s', six.text_type(e))
raise exception.ZunException(six.text_type(e))
return img
def read_tar_image(self, image):
with docker_utils.docker_client() as docker:
LOG.debug('Reading local tar image %s ', image['path'])

View File

@ -270,3 +270,10 @@ class ContainerDriver(object):
def search_image(self, context, repo, tag, driver_name, exact_match):
raise NotImplementedError()
def create_image(self, context, image_name, image_driver):
raise NotImplementedError()
def upload_image_data(self, context, image, image_tag, image_data,
image_driver):
raise NotImplementedError()

View File

@ -12,7 +12,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import six
import sys
from oslo_log import log as logging
@ -59,37 +58,6 @@ def load_image_driver(image_driver=None):
sys.exit(1)
def create_image(context, image_name, image_driver):
img = None
try:
img = image_driver.create_image(context, image_name)
except Exception as e:
LOG.exception('Unknown exception occurred while creating image: %s',
six.text_type(e))
raise exception.ZunException(six.text_type(e))
return img
def upload_image_data(context, image, image_tag, image_data,
image_driver):
img = None
try:
img = image_driver.update_image(context,
image.id,
tag=image_tag)
# Image data has to match the image format.
# contain format defaults to 'docker';
# disk format defaults to 'qcow2'.
img = image_driver.upload_image_data(context,
image.id,
image_data)
except Exception as e:
LOG.exception('Unknown exception occurred while uploading image: %s',
six.text_type(e))
raise exception.ZunException(six.text_type(e))
return img
def delete_image(context, img_id, image_driver=None):
if image_driver:
image_driver_list = [image_driver.lower()]

View File

@ -1086,7 +1086,7 @@ class TestManager(base.TestCase):
@mock.patch.object(ContainerActionEvent, 'event_start')
@mock.patch.object(ContainerActionEvent, 'event_finish')
@mock.patch('zun.image.driver.upload_image_data')
@mock.patch.object(fake_driver, 'upload_image_data')
@mock.patch.object(fake_driver, 'get_image')
@mock.patch.object(fake_driver, 'commit')
@mock.patch.object(fake_driver, 'pause')
@ -1118,7 +1118,7 @@ class TestManager(base.TestCase):
@mock.patch.object(ContainerActionEvent, 'event_start')
@mock.patch.object(ContainerActionEvent, 'event_finish')
@mock.patch('zun.image.driver.upload_image_data')
@mock.patch.object(fake_driver, 'upload_image_data')
@mock.patch.object(fake_driver, 'get_image')
@mock.patch.object(fake_driver, 'commit')
@mock.patch.object(fake_driver, 'pause')

View File

@ -44,6 +44,13 @@ class FakeDriver(driver.ContainerDriver):
def pull_image(self, context, repo, tag, **kwargs):
pass
def create_image(self, context, image_name, image_driver):
pass
def upload_image_data(self, context, image, image_tag, image_data,
image_driver):
pass
def create(self, container):
pass