Skip to content

Commit 934072a

Browse files
hannsemanchris-crone
authored andcommitted
Add NetworkAttachmentConfig type
Signed-off-by: Hannes Ljungberg <[email protected]>
1 parent 0be550d commit 934072a

File tree

6 files changed

+60
-9
lines changed

6 files changed

+60
-9
lines changed

docker/api/service.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,9 @@ def create_service(
135135
of the service. Default: ``None``
136136
rollback_config (RollbackConfig): Specification for the rollback
137137
strategy of the service. Default: ``None``
138-
networks (:py:class:`list`): List of network names or IDs to attach
139-
the service to. Default: ``None``.
138+
networks (:py:class:`list`): List of network names or IDs or
139+
:py:class:`~docker.types.NetworkAttachmentConfig` to attach the
140+
service to. Default: ``None``.
140141
endpoint_spec (EndpointSpec): Properties that can be configured to
141142
access and load balance a service. Default: ``None``.
142143
@@ -383,8 +384,9 @@ def update_service(self, service, version, task_template=None, name=None,
383384
of the service. Default: ``None``.
384385
rollback_config (RollbackConfig): Specification for the rollback
385386
strategy of the service. Default: ``None``
386-
networks (:py:class:`list`): List of network names or IDs to attach
387-
the service to. Default: ``None``.
387+
networks (:py:class:`list`): List of network names or IDs or
388+
:py:class:`~docker.types.NetworkAttachmentConfig` to attach the
389+
service to. Default: ``None``.
388390
endpoint_spec (EndpointSpec): Properties that can be configured to
389391
access and load balance a service. Default: ``None``.
390392
fetch_current_spec (boolean): Use the undefined settings from the

docker/models/services.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,9 @@ def create(self, image, command=None, **kwargs):
178178
``source:target:options``, where options is either
179179
``ro`` or ``rw``.
180180
name (str): Name to give to the service.
181-
networks (list of str): List of network names or IDs to attach
182-
the service to. Default: ``None``.
181+
networks (:py:class:`list`): List of network names or IDs or
182+
:py:class:`~docker.types.NetworkAttachmentConfig` to attach the
183+
service to. Default: ``None``.
183184
resources (Resources): Resource limits and reservations.
184185
restart_policy (RestartPolicy): Restart policy for containers.
185186
secrets (list of :py:class:`docker.types.SecretReference`): List

docker/types/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
ConfigReference, ContainerSpec, DNSConfig, DriverConfig, EndpointSpec,
88
Mount, Placement, PlacementPreference, Privileges, Resources,
99
RestartPolicy, RollbackConfig, SecretReference, ServiceMode, TaskTemplate,
10-
UpdateConfig
10+
UpdateConfig, NetworkAttachmentConfig
1111
)
1212
from .swarm import SwarmSpec, SwarmExternalCA

docker/types/services.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class TaskTemplate(dict):
2626
placement (Placement): Placement instructions for the scheduler.
2727
If a list is passed instead, it is assumed to be a list of
2828
constraints as part of a :py:class:`Placement` object.
29-
networks (:py:class:`list`): List of network names or IDs to attach
30-
the containers to.
29+
networks (:py:class:`list`): List of network names or IDs or
30+
:py:class:`NetworkAttachmentConfig` to attach the service to.
3131
force_update (int): A counter that triggers an update even if no
3232
relevant parameters have been changed.
3333
"""
@@ -770,3 +770,21 @@ def __init__(self, credentialspec_file=None, credentialspec_registry=None,
770770

771771
if len(selinux_context) > 0:
772772
self['SELinuxContext'] = selinux_context
773+
774+
775+
class NetworkAttachmentConfig(dict):
776+
"""
777+
Network attachment options for a service.
778+
779+
Args:
780+
target (str): The target network for attachment.
781+
Can be a network name or ID.
782+
aliases (:py:class:`list`): A list of discoverable alternate names
783+
for the service.
784+
options (:py:class:`dict`): Driver attachment options for the
785+
network target.
786+
"""
787+
def __init__(self, target, aliases=None, options=None):
788+
self['Target'] = target
789+
self['Aliases'] = aliases
790+
self['DriverOpts'] = options

docs/api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ Configuration types
142142
.. autoclass:: IPAMPool
143143
.. autoclass:: LogConfig
144144
.. autoclass:: Mount
145+
.. autoclass:: NetworkAttachmentConfig
145146
.. autoclass:: Placement
146147
.. autoclass:: PlacementPreference
147148
.. autoclass:: Privileges

tests/integration/api_service_test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,35 @@ def test_create_service_with_custom_networks(self):
371371
{'Target': net1['Id']}, {'Target': net2['Id']}
372372
]
373373

374+
def test_create_service_with_network_attachment_config(self):
375+
network = self.client.create_network(
376+
'dockerpytest_1', driver='overlay', ipam={'Driver': 'default'}
377+
)
378+
self.tmp_networks.append(network['Id'])
379+
container_spec = docker.types.ContainerSpec(BUSYBOX, ['true'])
380+
network_config = docker.types.NetworkAttachmentConfig(
381+
target='dockerpytest_1',
382+
aliases=['dockerpytest_1_alias'],
383+
options={
384+
'foo': 'bar'
385+
}
386+
)
387+
task_tmpl = docker.types.TaskTemplate(
388+
container_spec,
389+
networks=[network_config]
390+
)
391+
name = self.get_service_name()
392+
svc_id = self.client.create_service(
393+
task_tmpl, name=name
394+
)
395+
svc_info = self.client.inspect_service(svc_id)
396+
assert 'Networks' in svc_info['Spec']['TaskTemplate']
397+
service_networks_info = svc_info['Spec']['TaskTemplate']['Networks']
398+
assert len(service_networks_info) == 1
399+
assert service_networks_info[0]['Target'] == network['Id']
400+
assert service_networks_info[0]['Aliases'] == ['dockerpytest_1_alias']
401+
assert service_networks_info[0]['DriverOpts'] == {'foo': 'bar'}
402+
374403
def test_create_service_with_placement(self):
375404
node_id = self.client.nodes()[0]['ID']
376405
container_spec = docker.types.ContainerSpec(TEST_IMG, ['true'])

0 commit comments

Comments
 (0)