Add network_mode for image building

Add the ability to specify a different network_mode when running the
image builds. Under the covers the default is to use bridge networking
but if that is unavailable or a user would like to specify a different
networking  method they can now do so using the [DEFAULT]\network_mode
configuration.

Change-Id: I1d2398273b57ca4d52bbfacbc896ffecc58bc54b
This commit is contained in:
Alex Schultz 2018-04-19 16:49:06 -06:00 committed by Martin André
parent da329dd7be
commit 00bdb6d01f
4 changed files with 32 additions and 4 deletions

View File

@ -204,6 +204,8 @@ _CLI_OPTS = [
help='Show all available images (filtering supported)'),
cfg.StrOpt('namespace', short='n', default='kolla',
help='The Docker namespace name'),
cfg.StrOpt('network_mode', default=None,
help='The network mode for Docker build. Example: host'),
cfg.BoolOpt('cache', default=True,
help='Use the Docker cache when building'),
cfg.MultiOpt('profile', types.String(), short='p',

View File

@ -570,6 +570,7 @@ class BuildTask(DockerTask):
tag=image.canonical_name,
nocache=not self.conf.cache,
rm=True,
network_mode=self.conf.network_mode,
pull=pull,
forcerm=self.forcerm,
buildargs=buildargs):

View File

@ -87,11 +87,28 @@ class TasksTest(base.TestCase):
mock_client().build.assert_called_once_with(
path=self.image.path, tag=self.image.canonical_name,
nocache=False, rm=True, pull=True, forcerm=True,
network_mode=None, nocache=False, rm=True, pull=True, forcerm=True,
buildargs=None)
self.assertTrue(builder.success)
@mock.patch.dict(os.environ, clear=True)
@mock.patch('docker.APIClient')
def test_build_image_with_network_mode(self, mock_client):
self.dc = mock_client
push_queue = mock.Mock()
self.conf.set_override('network_mode', 'host')
builder = build.BuildTask(self.conf, self.image, push_queue)
builder.run()
mock_client().build.assert_called_once_with(
path=self.image.path, tag=self.image.canonical_name,
network_mode='host', nocache=False, rm=True, pull=True,
forcerm=True, buildargs=None)
self.assertTrue(builder.success)
@mock.patch.dict(os.environ, clear=True)
@mock.patch('docker.APIClient')
def test_build_image_with_build_arg(self, mock_client):
@ -107,7 +124,7 @@ class TasksTest(base.TestCase):
mock_client().build.assert_called_once_with(
path=self.image.path, tag=self.image.canonical_name,
nocache=False, rm=True, pull=True, forcerm=True,
network_mode=None, nocache=False, rm=True, pull=True, forcerm=True,
buildargs=build_args)
self.assertTrue(builder.success)
@ -126,7 +143,7 @@ class TasksTest(base.TestCase):
mock_client().build.assert_called_once_with(
path=self.image.path, tag=self.image.canonical_name,
nocache=False, rm=True, pull=True, forcerm=True,
network_mode=None, nocache=False, rm=True, pull=True, forcerm=True,
buildargs=build_args)
self.assertTrue(builder.success)
@ -147,7 +164,7 @@ class TasksTest(base.TestCase):
mock_client().build.assert_called_once_with(
path=self.image.path, tag=self.image.canonical_name,
nocache=False, rm=True, pull=True, forcerm=True,
network_mode=None, nocache=False, rm=True, pull=True, forcerm=True,
buildargs=build_args)
self.assertTrue(builder.success)

View File

@ -0,0 +1,8 @@
---
features:
- |
Adds new configuration parameter `[DEFAULT]\network_mode` that is used
when building the images. By default this is unset which will default to
the docker environment's default setting. You can specify 'host' which
would use the host's network when building the images. For other options,
see https://docs.docker.com/engine/reference/run/#network-settings