Kaydet (Commit) c28ff855 authored tarafından Joffrey F's avatar Joffrey F

Improve placement handling in DockerClient.services.create

Signed-off-by: 's avatarJoffrey F <joffrey@docker.com>
üst e7307e58
import copy import copy
from docker.errors import create_unexpected_kwargs_error, InvalidArgument from docker.errors import create_unexpected_kwargs_error, InvalidArgument
from docker.types import TaskTemplate, ContainerSpec, ServiceMode from docker.types import TaskTemplate, ContainerSpec, Placement, ServiceMode
from .resource import Model, Collection from .resource import Model, Collection
...@@ -153,6 +153,9 @@ class ServiceCollection(Collection): ...@@ -153,6 +153,9 @@ class ServiceCollection(Collection):
command (list of str or str): Command to run. command (list of str or str): Command to run.
args (list of str): Arguments to the command. args (list of str): Arguments to the command.
constraints (list of str): Placement constraints. constraints (list of str): Placement constraints.
preferences (list of str): Placement preferences.
platforms (list of tuple): A list of platforms constraints
expressed as ``(arch, os)`` tuples
container_labels (dict): Labels to apply to the container. container_labels (dict): Labels to apply to the container.
endpoint_spec (EndpointSpec): Properties that can be configured to endpoint_spec (EndpointSpec): Properties that can be configured to
access and load balance a service. Default: ``None``. access and load balance a service. Default: ``None``.
...@@ -302,6 +305,12 @@ CREATE_SERVICE_KWARGS = [ ...@@ -302,6 +305,12 @@ CREATE_SERVICE_KWARGS = [
'endpoint_spec', 'endpoint_spec',
] ]
PLACEMENT_KWARGS = [
'constraints',
'preferences',
'platforms',
]
def _get_create_service_kwargs(func_name, kwargs): def _get_create_service_kwargs(func_name, kwargs):
# Copy over things which can be copied directly # Copy over things which can be copied directly
...@@ -322,13 +331,10 @@ def _get_create_service_kwargs(func_name, kwargs): ...@@ -322,13 +331,10 @@ def _get_create_service_kwargs(func_name, kwargs):
container_spec_kwargs['labels'] = kwargs.pop('container_labels') container_spec_kwargs['labels'] = kwargs.pop('container_labels')
placement = {} placement = {}
for key in copy.copy(kwargs):
if 'constraints' in kwargs: if key in PLACEMENT_KWARGS:
placement['Constraints'] = kwargs.pop('constraints') placement[key] = kwargs.pop(key)
placement = Placement(**placement)
if 'preferences' in kwargs:
placement['Preferences'] = kwargs.pop('preferences')
task_template_kwargs['placement'] = placement task_template_kwargs['placement'] = placement
if 'log_driver' in kwargs: if 'log_driver' in kwargs:
......
...@@ -26,6 +26,8 @@ class CreateServiceKwargsTest(unittest.TestCase): ...@@ -26,6 +26,8 @@ class CreateServiceKwargsTest(unittest.TestCase):
'mounts': [{'some': 'mounts'}], 'mounts': [{'some': 'mounts'}],
'stop_grace_period': 5, 'stop_grace_period': 5,
'constraints': ['foo=bar'], 'constraints': ['foo=bar'],
'preferences': ['bar=baz'],
'platforms': [('x86_64', 'linux')],
}) })
task_template = kwargs.pop('task_template') task_template = kwargs.pop('task_template')
...@@ -41,7 +43,11 @@ class CreateServiceKwargsTest(unittest.TestCase): ...@@ -41,7 +43,11 @@ class CreateServiceKwargsTest(unittest.TestCase):
'ContainerSpec', 'Resources', 'RestartPolicy', 'Placement', 'ContainerSpec', 'Resources', 'RestartPolicy', 'Placement',
'LogDriver', 'Networks' 'LogDriver', 'Networks'
]) ])
assert task_template['Placement'] == {'Constraints': ['foo=bar']} assert task_template['Placement'] == {
'Constraints': ['foo=bar'],
'Preferences': ['bar=baz'],
'Platforms': [{'Architecture': 'x86_64', 'OS': 'linux'}],
}
assert task_template['LogDriver'] == { assert task_template['LogDriver'] == {
'Name': 'logdriver', 'Name': 'logdriver',
'Options': {'foo': 'bar'} 'Options': {'foo': 'bar'}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment