Skip to content

Commit c2c9ccd

Browse files
committed
Improve placement handling in DockerClient.services.create
Signed-off-by: Joffrey F <[email protected]>
1 parent cbc7623 commit c2c9ccd

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

docker/models/services.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import copy
22
from docker.errors import create_unexpected_kwargs_error, InvalidArgument
3-
from docker.types import TaskTemplate, ContainerSpec, ServiceMode
3+
from docker.types import TaskTemplate, ContainerSpec, Placement, ServiceMode
44
from .resource import Model, Collection
55

66

@@ -153,6 +153,9 @@ def create(self, image, command=None, **kwargs):
153153
command (list of str or str): Command to run.
154154
args (list of str): Arguments to the command.
155155
constraints (list of str): Placement constraints.
156+
preferences (list of str): Placement preferences.
157+
platforms (list of tuple): A list of platforms constraints
158+
expressed as ``(arch, os)`` tuples
156159
container_labels (dict): Labels to apply to the container.
157160
endpoint_spec (EndpointSpec): Properties that can be configured to
158161
access and load balance a service. Default: ``None``.
@@ -302,6 +305,12 @@ def list(self, **kwargs):
302305
'endpoint_spec',
303306
]
304307

308+
PLACEMENT_KWARGS = [
309+
'constraints',
310+
'preferences',
311+
'platforms',
312+
]
313+
305314

306315
def _get_create_service_kwargs(func_name, kwargs):
307316
# Copy over things which can be copied directly
@@ -322,13 +331,10 @@ def _get_create_service_kwargs(func_name, kwargs):
322331
container_spec_kwargs['labels'] = kwargs.pop('container_labels')
323332

324333
placement = {}
325-
326-
if 'constraints' in kwargs:
327-
placement['Constraints'] = kwargs.pop('constraints')
328-
329-
if 'preferences' in kwargs:
330-
placement['Preferences'] = kwargs.pop('preferences')
331-
334+
for key in copy.copy(kwargs):
335+
if key in PLACEMENT_KWARGS:
336+
placement[key] = kwargs.pop(key)
337+
placement = Placement(**placement)
332338
task_template_kwargs['placement'] = placement
333339

334340
if 'log_driver' in kwargs:

tests/unit/models_services_test.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ def test_get_create_service_kwargs(self):
2626
'mounts': [{'some': 'mounts'}],
2727
'stop_grace_period': 5,
2828
'constraints': ['foo=bar'],
29+
'preferences': ['bar=baz'],
30+
'platforms': [('x86_64', 'linux')],
2931
})
3032

3133
task_template = kwargs.pop('task_template')
@@ -41,7 +43,11 @@ def test_get_create_service_kwargs(self):
4143
'ContainerSpec', 'Resources', 'RestartPolicy', 'Placement',
4244
'LogDriver', 'Networks'
4345
])
44-
assert task_template['Placement'] == {'Constraints': ['foo=bar']}
46+
assert task_template['Placement'] == {
47+
'Constraints': ['foo=bar'],
48+
'Preferences': ['bar=baz'],
49+
'Platforms': [{'Architecture': 'x86_64', 'OS': 'linux'}],
50+
}
4551
assert task_template['LogDriver'] == {
4652
'Name': 'logdriver',
4753
'Options': {'foo': 'bar'}

0 commit comments

Comments
 (0)