Skip to content

Commit caef663

Browse files
authored
Merge pull request #2741 from WojciechowskiPiotr/maxreplicas
Support for docker.types.Placement.MaxReplicas
2 parents ccab788 + 6d1dffe commit caef663

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

docker/models/services.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ def create(self, image, command=None, **kwargs):
157157
constraints.
158158
preferences (list of tuple): :py:class:`~docker.types.Placement`
159159
preferences.
160+
maxreplicas (int): :py:class:`~docker.types.Placement` maxreplicas
161+
or (int) representing maximum number of replicas per node.
160162
platforms (list of tuple): A list of platform constraints
161163
expressed as ``(arch, os)`` tuples.
162164
container_labels (dict): Labels to apply to the container.
@@ -319,6 +321,7 @@ def list(self, **kwargs):
319321
'constraints',
320322
'preferences',
321323
'platforms',
324+
'maxreplicas',
322325
]
323326

324327

docker/types/services.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,10 +659,12 @@ class Placement(dict):
659659
are provided in order from highest to lowest precedence and
660660
are expressed as ``(strategy, descriptor)`` tuples. See
661661
:py:class:`PlacementPreference` for details.
662+
maxreplicas (int): Maximum number of replicas per node
662663
platforms (:py:class:`list` of tuple): A list of platforms
663664
expressed as ``(arch, os)`` tuples
664665
"""
665-
def __init__(self, constraints=None, preferences=None, platforms=None):
666+
def __init__(self, constraints=None, preferences=None, platforms=None,
667+
maxreplicas=None):
666668
if constraints is not None:
667669
self['Constraints'] = constraints
668670
if preferences is not None:
@@ -671,6 +673,8 @@ def __init__(self, constraints=None, preferences=None, platforms=None):
671673
if isinstance(pref, tuple):
672674
pref = PlacementPreference(*pref)
673675
self['Preferences'].append(pref)
676+
if maxreplicas is not None:
677+
self['MaxReplicas'] = maxreplicas
674678
if platforms:
675679
self['Platforms'] = []
676680
for plat in platforms:

tests/integration/api_service_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,19 @@ def test_create_service_with_placement_preferences_tuple(self):
471471
assert 'Placement' in svc_info['Spec']['TaskTemplate']
472472
assert svc_info['Spec']['TaskTemplate']['Placement'] == placemt
473473

474+
@requires_api_version('1.40')
475+
def test_create_service_with_placement_maxreplicas(self):
476+
container_spec = docker.types.ContainerSpec(TEST_IMG, ['true'])
477+
placemt = docker.types.Placement(maxreplicas=1)
478+
task_tmpl = docker.types.TaskTemplate(
479+
container_spec, placement=placemt
480+
)
481+
name = self.get_service_name()
482+
svc_id = self.client.create_service(task_tmpl, name=name)
483+
svc_info = self.client.inspect_service(svc_id)
484+
assert 'Placement' in svc_info['Spec']['TaskTemplate']
485+
assert svc_info['Spec']['TaskTemplate']['Placement'] == placemt
486+
474487
def test_create_service_with_endpoint_spec(self):
475488
container_spec = docker.types.ContainerSpec(TEST_IMG, ['true'])
476489
task_tmpl = docker.types.TaskTemplate(container_spec)

tests/unit/models_services_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def test_get_create_service_kwargs(self):
2828
'constraints': ['foo=bar'],
2929
'preferences': ['bar=baz'],
3030
'platforms': [('x86_64', 'linux')],
31+
'maxreplicas': 1
3132
})
3233

3334
task_template = kwargs.pop('task_template')
@@ -47,6 +48,7 @@ def test_get_create_service_kwargs(self):
4748
'Constraints': ['foo=bar'],
4849
'Preferences': ['bar=baz'],
4950
'Platforms': [{'Architecture': 'x86_64', 'OS': 'linux'}],
51+
'MaxReplicas': 1,
5052
}
5153
assert task_template['LogDriver'] == {
5254
'Name': 'logdriver',

0 commit comments

Comments
 (0)