Merge "Allow setting hostname on sandbox mode"

This commit is contained in:
Jenkins 2017-10-13 02:15:46 +00:00 committed by Gerrit Code Review
commit 94ee6dabd8
3 changed files with 11 additions and 9 deletions

View File

@ -230,11 +230,6 @@ class ContainersController(base.Controller):
hostname = container_dict.pop('hostname', None) hostname = container_dict.pop('hostname', None)
if hostname is not 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 req_version = pecan.request.version
min_version = versions.Version('', '', '', '1.9') min_version = versions.Version('', '', '', '1.9')
if req_version >= min_version: if req_version >= min_version:

View File

@ -132,8 +132,11 @@ class DockerDriver(driver.ContainerDriver):
'labels': container.labels, 'labels': container.labels,
'tty': container.interactive, 'tty': container.interactive,
'stdin_open': 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\ runtime = container.runtime if container.runtime\
else CONF.container_runtime else CONF.container_runtime
@ -669,8 +672,9 @@ class DockerDriver(driver.ContainerDriver):
network_api = zun_network.api(context=context, docker_api=docker) network_api = zun_network.api(context=context, docker_api=docker)
self._provision_network(context, network_api, requested_networks) self._provision_network(context, network_api, requested_networks)
name = self.get_sandbox_name(container) name = self.get_sandbox_name(container)
sandbox = docker.create_container(image, name=name, sandbox = docker.create_container(
hostname=name[:63]) image, name=name,
hostname=container.hostname or name[:63])
container.set_sandbox_id(sandbox['Id']) container.set_sandbox_id(sandbox['Id'])
addresses = self._setup_network_for_container( addresses = self._setup_network_for_container(
context, container, requested_networks, network_api) context, container, requested_networks, network_api)

View File

@ -365,12 +365,14 @@ class TestDockerDriver(base.DriverTestCase):
self.mock_docker.create_container = mock.Mock( self.mock_docker.create_container = mock.Mock(
return_value={'Id': 'val1', 'key1': 'val2'}) return_value={'Id': 'val1', 'key1': 'val2'})
mock_container = mock.MagicMock() mock_container = mock.MagicMock()
hostname = 'my_hostname'
mock_container.hostname = hostname
requested_networks = [] requested_networks = []
result_sandbox_id = self.driver.create_sandbox( result_sandbox_id = self.driver.create_sandbox(
self.context, mock_container, requested_networks, self.context, mock_container, requested_networks,
'kubernetes/pause') 'kubernetes/pause')
self.mock_docker.create_container.assert_called_once_with( 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') self.assertEqual(result_sandbox_id, 'val1')
@mock.patch('zun.network.kuryr_network.KuryrNetwork' @mock.patch('zun.network.kuryr_network.KuryrNetwork'
@ -385,6 +387,7 @@ class TestDockerDriver(base.DriverTestCase):
self.mock_docker.create_container = mock.Mock( self.mock_docker.create_container = mock.Mock(
return_value={'Id': 'val1', 'key1': 'val2'}) return_value={'Id': 'val1', 'key1': 'val2'})
mock_container = mock.MagicMock() mock_container = mock.MagicMock()
mock_container.hostname = None
requested_networks = [] requested_networks = []
result_sandbox_id = self.driver.create_sandbox( result_sandbox_id = self.driver.create_sandbox(
self.context, mock_container, requested_networks, self.context, mock_container, requested_networks,