Refactor container driver

Introduce a BaseDriver class that is the base class of all drivers.

Change-Id: I9e4b51821f71768f52898290ced69d3849805415
This commit is contained in:
Hongbin Lu 2020-01-19 20:10:00 +00:00
parent 46a2d314ab
commit 83dbf2d5c4
4 changed files with 113 additions and 108 deletions

View File

@ -104,7 +104,7 @@ def wrap_docker_error(function):
return decorated_function
class DockerDriver(driver.ContainerDriver):
class DockerDriver(driver.BaseDriver, driver.ContainerDriver):
"""Implementation of container drivers for Docker."""
# TODO(hongbin): define a list of capabilities of this driver.

View File

@ -83,8 +83,8 @@ def load_container_driver(container_driver=None):
sys.exit(1)
class ContainerDriver(object):
"""Base class for container drivers."""
class BaseDriver(object):
"""Base class for driver."""
def __init__(self):
self.volume_drivers = {}
@ -92,99 +92,6 @@ class ContainerDriver(object):
driver = vol_driver.driver(driver_name)
self.volume_drivers[driver_name] = driver
def create(self, context, container, **kwargs):
"""Create a container."""
raise NotImplementedError()
def commit(self, context, container, repository, tag):
"""Commit a container."""
raise NotImplementedError()
def delete(self, context, container, force):
"""Delete a container."""
raise NotImplementedError()
def list(self, context):
"""List all containers."""
raise NotImplementedError()
def update_containers_states(self, context, containers, manager):
"""Update containers states."""
raise NotImplementedError()
def show(self, context, container):
"""Show the details of a container."""
raise NotImplementedError()
def reboot(self, context, container):
"""Reboot a container."""
raise NotImplementedError()
def stop(self, context, container):
"""Stop a container."""
raise NotImplementedError()
def start(self, context, container):
"""Start a container."""
raise NotImplementedError()
def pause(self, context, container):
"""Pause a container."""
raise NotImplementedError()
def unpause(self, context, container):
"""Unpause a container."""
raise NotImplementedError()
def show_logs(self, context, container, stdout=True, stderr=True,
timestamps=False, tail='all', since=None):
"""Show logs of a container."""
raise NotImplementedError()
def execute_create(self, context, container, command, **kwargs):
"""Create an execute instance for running a command."""
raise NotImplementedError()
def execute_run(self, exec_id):
"""Run the command specified by an execute instance."""
raise NotImplementedError()
def execute_resize(self, exec_id, height, width):
"""Resizes the tty session used by the exec."""
raise NotImplementedError()
def kill(self, context, container, signal):
"""Kill a container with specified signal."""
raise NotImplementedError()
def get_websocket_url(self, context, container):
"""Get websocket url of a container."""
raise NotImplementedError()
def resize(self, context, container, height, weight):
"""Resize tty of a container."""
raise NotImplementedError()
def top(self, context, container, ps_args):
"""Display the running processes inside the container."""
raise NotImplementedError()
def get_archive(self, context, container, path):
"""Copy resource from a container."""
raise NotImplementedError()
def put_archive(self, context, container, path, data):
"""Copy resource to a container."""
raise NotImplementedError()
def stats(self, context, container):
"""Display stats of the container."""
raise NotImplementedError()
def update(self, context, container):
"""Update a container."""
raise NotImplementedError()
def get_host_numa_topology(self):
numa_topo_obj = objects.NUMATopology()
os_capability_linux.LinuxHost().get_host_numa_topology(numa_topo_obj)
@ -233,16 +140,12 @@ class ContainerDriver(object):
volume_driver = self._get_volume_driver(volume_mapping)
return volume_driver.is_volume_deleted(context, volume_mapping)
def add_security_group(self, context, container, security_group, **kwargs):
raise NotImplementedError()
def remove_security_group(self, context, container, security_group,
**kwargs):
raise NotImplementedError()
def get_available_nodes(self):
return [CONF.host]
def node_support_disk_quota(self):
return False
def get_available_resources(self):
"""Retrieve resource information.
@ -412,6 +315,110 @@ class ContainerDriver(object):
return traits
class ContainerDriver(object):
"""Interface for container driver."""
def create(self, context, container, **kwargs):
"""Create a container."""
raise NotImplementedError()
def commit(self, context, container, repository, tag):
"""Commit a container."""
raise NotImplementedError()
def delete(self, context, container, force):
"""Delete a container."""
raise NotImplementedError()
def list(self, context):
"""List all containers."""
raise NotImplementedError()
def update_containers_states(self, context, containers, manager):
"""Update containers states."""
raise NotImplementedError()
def show(self, context, container):
"""Show the details of a container."""
raise NotImplementedError()
def reboot(self, context, container):
"""Reboot a container."""
raise NotImplementedError()
def stop(self, context, container):
"""Stop a container."""
raise NotImplementedError()
def start(self, context, container):
"""Start a container."""
raise NotImplementedError()
def pause(self, context, container):
"""Pause a container."""
raise NotImplementedError()
def unpause(self, context, container):
"""Unpause a container."""
raise NotImplementedError()
def show_logs(self, context, container, stdout=True, stderr=True,
timestamps=False, tail='all', since=None):
"""Show logs of a container."""
raise NotImplementedError()
def execute_create(self, context, container, command, **kwargs):
"""Create an execute instance for running a command."""
raise NotImplementedError()
def execute_run(self, exec_id):
"""Run the command specified by an execute instance."""
raise NotImplementedError()
def execute_resize(self, exec_id, height, width):
"""Resizes the tty session used by the exec."""
raise NotImplementedError()
def kill(self, context, container, signal):
"""Kill a container with specified signal."""
raise NotImplementedError()
def get_websocket_url(self, context, container):
"""Get websocket url of a container."""
raise NotImplementedError()
def resize(self, context, container, height, weight):
"""Resize tty of a container."""
raise NotImplementedError()
def top(self, context, container, ps_args):
"""Display the running processes inside the container."""
raise NotImplementedError()
def get_archive(self, context, container, path):
"""Copy resource from a container."""
raise NotImplementedError()
def put_archive(self, context, container, path, data):
"""Copy resource to a container."""
raise NotImplementedError()
def stats(self, context, container):
"""Display stats of the container."""
raise NotImplementedError()
def update(self, context, container):
"""Update a container."""
raise NotImplementedError()
def add_security_group(self, context, container, security_group, **kwargs):
raise NotImplementedError()
def remove_security_group(self, context, container, security_group,
**kwargs):
raise NotImplementedError()
def network_detach(self, context, container, network):
raise NotImplementedError()
@ -427,9 +434,6 @@ class ContainerDriver(object):
def inspect_network(self, network):
raise NotImplementedError()
def node_support_disk_quota(self):
return False
def get_host_default_base_size(self):
raise NotImplementedError()

View File

@ -892,7 +892,8 @@ class TestDockerDriver(base.DriverTestCase):
'.LinuxHost.get_cpu_numa_info')
@mock.patch('zun.container.docker.driver.DockerDriver'
'.get_total_disk_for_container')
@mock.patch('zun.container.driver.ContainerDriver.get_host_mem')
@mock.patch('zun.container.docker.driver.DockerDriver'
'.get_host_mem')
@mock.patch(
'zun.container.docker.driver.DockerDriver.get_host_info')
def test_get_available_resources(self, mock_info, mock_mem,

View File

@ -16,7 +16,7 @@ from zun.common.utils import check_container_id
from zun.container import driver
class FakeDriver(driver.ContainerDriver):
class FakeDriver(driver.BaseDriver, driver.ContainerDriver):
"""Fake driver for testing."""
def __init__(self):