Skip to content

Commit

Permalink
Merge pull request #2741 from WojciechowskiPiotr/maxreplicas
Browse files Browse the repository at this point in the history
Support for docker.types.Placement.MaxReplicas
  • Loading branch information
aiordache committed Feb 10, 2021
2 parents ccab788 + 6d1dffe commit caef663
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docker/models/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ def create(self, image, command=None, **kwargs):
constraints.
preferences (list of tuple): :py:class:`~docker.types.Placement`
preferences.
maxreplicas (int): :py:class:`~docker.types.Placement` maxreplicas
or (int) representing maximum number of replicas per node.
platforms (list of tuple): A list of platform constraints
expressed as ``(arch, os)`` tuples.
container_labels (dict): Labels to apply to the container.
Expand Down Expand Up @@ -319,6 +321,7 @@ def list(self, **kwargs):
'constraints',
'preferences',
'platforms',
'maxreplicas',
]


Expand Down
6 changes: 5 additions & 1 deletion docker/types/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,10 +659,12 @@ class Placement(dict):
are provided in order from highest to lowest precedence and
are expressed as ``(strategy, descriptor)`` tuples. See
:py:class:`PlacementPreference` for details.
maxreplicas (int): Maximum number of replicas per node
platforms (:py:class:`list` of tuple): A list of platforms
expressed as ``(arch, os)`` tuples
"""
def __init__(self, constraints=None, preferences=None, platforms=None):
def __init__(self, constraints=None, preferences=None, platforms=None,
maxreplicas=None):
if constraints is not None:
self['Constraints'] = constraints
if preferences is not None:
Expand All @@ -671,6 +673,8 @@ def __init__(self, constraints=None, preferences=None, platforms=None):
if isinstance(pref, tuple):
pref = PlacementPreference(*pref)
self['Preferences'].append(pref)
if maxreplicas is not None:
self['MaxReplicas'] = maxreplicas
if platforms:
self['Platforms'] = []
for plat in platforms:
Expand Down
13 changes: 13 additions & 0 deletions tests/integration/api_service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,19 @@ def test_create_service_with_placement_preferences_tuple(self):
assert 'Placement' in svc_info['Spec']['TaskTemplate']
assert svc_info['Spec']['TaskTemplate']['Placement'] == placemt

@requires_api_version('1.40')
def test_create_service_with_placement_maxreplicas(self):
container_spec = docker.types.ContainerSpec(TEST_IMG, ['true'])
placemt = docker.types.Placement(maxreplicas=1)
task_tmpl = docker.types.TaskTemplate(
container_spec, placement=placemt
)
name = self.get_service_name()
svc_id = self.client.create_service(task_tmpl, name=name)
svc_info = self.client.inspect_service(svc_id)
assert 'Placement' in svc_info['Spec']['TaskTemplate']
assert svc_info['Spec']['TaskTemplate']['Placement'] == placemt

def test_create_service_with_endpoint_spec(self):
container_spec = docker.types.ContainerSpec(TEST_IMG, ['true'])
task_tmpl = docker.types.TaskTemplate(container_spec)
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/models_services_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def test_get_create_service_kwargs(self):
'constraints': ['foo=bar'],
'preferences': ['bar=baz'],
'platforms': [('x86_64', 'linux')],
'maxreplicas': 1
})

task_template = kwargs.pop('task_template')
Expand All @@ -47,6 +48,7 @@ def test_get_create_service_kwargs(self):
'Constraints': ['foo=bar'],
'Preferences': ['bar=baz'],
'Platforms': [{'Architecture': 'x86_64', 'OS': 'linux'}],
'MaxReplicas': 1,
}
assert task_template['LogDriver'] == {
'Name': 'logdriver',
Expand Down

0 comments on commit caef663

Please sign in to comment.