Skip to content

Commit e15db4c

Browse files
committed
Improve handling of placement preferences; improve docs
Signed-off-by: Joffrey F <[email protected]>
1 parent 9223655 commit e15db4c

File tree

4 files changed

+44
-13
lines changed

4 files changed

+44
-13
lines changed

docker/models/services.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,12 @@ def create(self, image, command=None, **kwargs):
153153
image (str): The image name to use for the containers.
154154
command (list of str or str): Command to run.
155155
args (list of str): Arguments to the command.
156-
constraints (list of str): Placement constraints.
157-
preferences (list of str): Placement preferences.
158-
platforms (list of tuple): A list of platforms constraints
159-
expressed as ``(arch, os)`` tuples
156+
constraints (list of str): :py:class:`~docker.types.Placement`
157+
constraints.
158+
preferences (list of tuple): :py:class:`~docker.types.Placement`
159+
preferences.
160+
platforms (list of tuple): A list of platform constraints
161+
expressed as ``(arch, os)`` tuples.
160162
container_labels (dict): Labels to apply to the container.
161163
endpoint_spec (EndpointSpec): Properties that can be configured to
162164
access and load balance a service. Default: ``None``.

docker/types/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
from .networks import EndpointConfig, IPAMConfig, IPAMPool, NetworkingConfig
66
from .services import (
77
ConfigReference, ContainerSpec, DNSConfig, DriverConfig, EndpointSpec,
8-
Mount, Placement, Privileges, Resources, RestartPolicy, RollbackConfig,
9-
SecretReference, ServiceMode, TaskTemplate, UpdateConfig
8+
Mount, Placement, PlacementPreference, Privileges, Resources,
9+
RestartPolicy, RollbackConfig, SecretReference, ServiceMode, TaskTemplate,
10+
UpdateConfig
1011
)
1112
from .swarm import SwarmSpec, SwarmExternalCA

docker/types/services.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -648,18 +648,24 @@ class Placement(dict):
648648
Placement constraints to be used as part of a :py:class:`TaskTemplate`
649649
650650
Args:
651-
constraints (:py:class:`list`): A list of constraints
652-
preferences (:py:class:`list`): Preferences provide a way to make
653-
the scheduler aware of factors such as topology. They are
654-
provided in order from highest to lowest precedence.
655-
platforms (:py:class:`list`): A list of platforms expressed as
656-
``(arch, os)`` tuples
651+
constraints (:py:class:`list` of str): A list of constraints
652+
preferences (:py:class:`list` of tuple): Preferences provide a way
653+
to make the scheduler aware of factors such as topology. They
654+
are provided in order from highest to lowest precedence and
655+
are expressed as ``(strategy, descriptor)`` tuples. See
656+
:py:class:`PlacementPreference` for details.
657+
platforms (:py:class:`list` of tuple): A list of platforms
658+
expressed as ``(arch, os)`` tuples
657659
"""
658660
def __init__(self, constraints=None, preferences=None, platforms=None):
659661
if constraints is not None:
660662
self['Constraints'] = constraints
661663
if preferences is not None:
662-
self['Preferences'] = preferences
664+
self['Preferences'] = []
665+
for pref in preferences:
666+
if isinstance(pref, tuple):
667+
pref = PlacementPreference(*pref)
668+
self['Preferences'].append(pref)
663669
if platforms:
664670
self['Platforms'] = []
665671
for plat in platforms:
@@ -668,6 +674,27 @@ def __init__(self, constraints=None, preferences=None, platforms=None):
668674
})
669675

670676

677+
class PlacementPreference(dict):
678+
"""
679+
Placement preference to be used as an element in the list of
680+
preferences for :py:class:`Placement` objects.
681+
682+
Args:
683+
strategy (string): The placement strategy to implement. Currently,
684+
the only supported strategy is ``spread``.
685+
descriptor (string): A label descriptor. For the spread strategy,
686+
the scheduler will try to spread tasks evenly over groups of
687+
nodes identified by this label.
688+
"""
689+
def __init__(self, strategy, descriptor):
690+
if strategy != 'spread':
691+
raise errors.InvalidArgument(
692+
'PlacementPreference strategy value is invalid ({}):'
693+
' must be "spread".'.format(strategy)
694+
)
695+
self['SpreadOver'] = descriptor
696+
697+
671698
class DNSConfig(dict):
672699
"""
673700
Specification for DNS related configurations in resolver configuration

docs/api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ Configuration types
143143
.. autoclass:: LogConfig
144144
.. autoclass:: Mount
145145
.. autoclass:: Placement
146+
.. autoclass:: PlacementPreference
146147
.. autoclass:: Privileges
147148
.. autoclass:: Resources
148149
.. autoclass:: RestartPolicy

0 commit comments

Comments
 (0)