From 339559e7a4031ef60b99f2a325e7cc46aae93724 Mon Sep 17 00:00:00 2001 From: Hongbin Lu Date: Thu, 28 Sep 2017 18:39:16 -0400 Subject: [PATCH] Allow setting hostname on sandbox mode If sandbox is used, we can simply set the hostname of the sandbox container. Then, all containers inside the sandbox will share the hostname. Change-Id: Ib7a70df58137d92df7d76e85354e761df99b2425 Related-Bug: #1716849 --- zun/api/controllers/v1/containers.py | 5 ----- zun/container/docker/driver.py | 10 +++++++--- zun/tests/unit/container/docker/test_docker_driver.py | 5 ++++- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/zun/api/controllers/v1/containers.py b/zun/api/controllers/v1/containers.py index c51761f6a..f9d9d13ef 100644 --- a/zun/api/controllers/v1/containers.py +++ b/zun/api/controllers/v1/containers.py @@ -230,11 +230,6 @@ class ContainersController(base.Controller): hostname = container_dict.pop('hostname', None) if hostname is not None: - if CONF.use_sandbox: - raise exception.ConflictOptions( - 'Cannot set container\'s hostname when use sandbox. ' - 'Because with sandbox, network_mode will be set, it ' - 'is incompatible with legacy network (hostname).') req_version = pecan.request.version min_version = versions.Version('', '', '', '1.9') if req_version >= min_version: diff --git a/zun/container/docker/driver.py b/zun/container/docker/driver.py index 03b327c2b..83908988d 100644 --- a/zun/container/docker/driver.py +++ b/zun/container/docker/driver.py @@ -132,8 +132,11 @@ class DockerDriver(driver.ContainerDriver): 'labels': container.labels, 'tty': container.interactive, 'stdin_open': container.interactive, - 'hostname': container.hostname, } + if not sandbox_id: + # Sandbox is not used so it is legitimate to customize + # the container's hostname + kwargs['hostname'] = container.hostname runtime = container.runtime if container.runtime\ else CONF.container_runtime @@ -656,8 +659,9 @@ class DockerDriver(driver.ContainerDriver): network_api = zun_network.api(context=context, docker_api=docker) self._provision_network(context, network_api, requested_networks) name = self.get_sandbox_name(container) - sandbox = docker.create_container(image, name=name, - hostname=name[:63]) + sandbox = docker.create_container( + image, name=name, + hostname=container.hostname or name[:63]) container.set_sandbox_id(sandbox['Id']) addresses = self._setup_network_for_container( context, container, requested_networks, network_api) diff --git a/zun/tests/unit/container/docker/test_docker_driver.py b/zun/tests/unit/container/docker/test_docker_driver.py index ffb6da8a9..60171e166 100644 --- a/zun/tests/unit/container/docker/test_docker_driver.py +++ b/zun/tests/unit/container/docker/test_docker_driver.py @@ -343,12 +343,14 @@ class TestDockerDriver(base.DriverTestCase): self.mock_docker.create_container = mock.Mock( return_value={'Id': 'val1', 'key1': 'val2'}) mock_container = mock.MagicMock() + hostname = 'my_hostname' + mock_container.hostname = hostname requested_networks = [] result_sandbox_id = self.driver.create_sandbox( self.context, mock_container, requested_networks, 'kubernetes/pause') self.mock_docker.create_container.assert_called_once_with( - 'kubernetes/pause', name=sandbox_name, hostname=sandbox_name) + 'kubernetes/pause', name=sandbox_name, hostname=hostname) self.assertEqual(result_sandbox_id, 'val1') @mock.patch('zun.network.kuryr_network.KuryrNetwork' @@ -363,6 +365,7 @@ class TestDockerDriver(base.DriverTestCase): self.mock_docker.create_container = mock.Mock( return_value={'Id': 'val1', 'key1': 'val2'}) mock_container = mock.MagicMock() + mock_container.hostname = None requested_networks = [] result_sandbox_id = self.driver.create_sandbox( self.context, mock_container, requested_networks,