Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Check API version before setting swarm addr pool.
Also corrected a documentation error: the default API version from
constants is currently 1.35, not 1.30 as was sometimes listed.

Signed-off-by: Barry Shapira <[email protected]>
  • Loading branch information
Barry Shapira committed Dec 14, 2018
commit b7d93f62d98b3457a229d6c51a2573b309ed035f
2 changes: 1 addition & 1 deletion docker/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class APIClient(
base_url (str): URL to the Docker server. For example,
``unix:///var/run/docker.sock`` or ``tcp://127.0.0.1:1234``.
version (str): The version of the API to use. Set to ``auto`` to
automatically detect the server's version. Default: ``1.30``
automatically detect the server's version. Default: ``1.35``
timeout (int): Default timeout for API calls, in seconds.
tls (bool or :py:class:`~docker.tls.TLSConfig`): Enable TLS. Pass
``True`` to enable it with default options, or pass a
Expand Down
27 changes: 24 additions & 3 deletions docker/api/swarm.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from six.moves import http_client
from ..constants import DEFAULT_SWARM_ADDR_POOL, DEFAULT_SWARM_SUBNET_SIZE
from .. import errors
from .. import types
from .. import utils
Expand Down Expand Up @@ -82,7 +83,7 @@ def get_unlock_key(self):

@utils.minimum_version('1.24')
def init_swarm(self, advertise_addr=None, listen_addr='0.0.0.0:2377',
default_addr_pool=[], subnet_size=24,
default_addr_pool=None, subnet_size=None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should add new parameters at the end of the list so that callers of init_swarm that use ordered (i.e.: unnamed) parameters continue to work as expected.

e.g.: An existing function could call:

s.init_swarm('192.168.0.1', '0.0.0.0:2377', True)

Which would now break.

force_new_cluster=False, swarm_spec=None):
"""
Initialize a new Swarm using the current connected engine as the first
Expand All @@ -106,9 +107,9 @@ def init_swarm(self, advertise_addr=None, listen_addr='0.0.0.0:2377',
default_addr_pool (list of strings): Default Address Pool specifies
default subnet pools for global scope networks. Each pool
should be specified as a CIDR block, like '10.0.0.0/16'.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Might be good to use the default as the example.

Default: []
Default: None
subnet_size (int): SubnetSize specifies the subnet size of the
networks created from the default subnet pool. Default: 24
networks created from the default subnet pool. Default: None
force_new_cluster (bool): Force creating a new Swarm, even if
already part of one. Default: False
swarm_spec (dict): Configuration settings of the new Swarm. Use
Expand All @@ -124,8 +125,28 @@ def init_swarm(self, advertise_addr=None, listen_addr='0.0.0.0:2377',
"""

url = self._url('/swarm/init')

if swarm_spec is not None and not isinstance(swarm_spec, dict):
raise TypeError('swarm_spec must be a dictionary')

if default_addr_pool is not None:
if utils.version_lt(self._version, '1.39'):
raise errors.InvalidVersion(
'Address pool is only available for API version >= 1.39'
)
# subnet_size becomes 0 if not set with default_addr_pool
if subnet_size is None:
subnet_size = DEFAULT_SWARM_SUBNET_SIZE

if subnet_size is not None:
if utils.version_lt(self._version, '1.39'):
raise errors.InvalidVersion(
'Subnet size is only available for API version >= 1.39'
)
# subnet_size is ignored if set without default_addr_pool
if default_addr_pool is None:
default_addr_pool = DEFAULT_SWARM_ADDR_POOL

data = {
'AdvertiseAddr': advertise_addr,
'ListenAddr': listen_addr,
Expand Down
6 changes: 3 additions & 3 deletions docker/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DockerClient(object):
base_url (str): URL to the Docker server. For example,
``unix:///var/run/docker.sock`` or ``tcp://127.0.0.1:1234``.
version (str): The version of the API to use. Set to ``auto`` to
automatically detect the server's version. Default: ``1.30``
automatically detect the server's version. Default: ``1.35``
timeout (int): Default timeout for API calls, in seconds.
tls (bool or :py:class:`~docker.tls.TLSConfig`): Enable TLS. Pass
``True`` to enable it with default options, or pass a
Expand All @@ -51,7 +51,7 @@ def from_env(cls, **kwargs):

The URL to the Docker host.

.. envvar:: DOCKER_TLS_VERIFY
.. envvar:: DOCKE R_TLS_VERIFY

Verify the host against a CA certificate.

Expand All @@ -62,7 +62,7 @@ def from_env(cls, **kwargs):

Args:
version (str): The version of the API to use. Set to ``auto`` to
automatically detect the server's version. Default: ``1.30``
automatically detect the server's version. Default: ``1.35``
timeout (int): Default timeout for API calls, in seconds.
ssl_version (int): A valid `SSL version`_.
assert_hostname (bool): Verify the hostname of the server.
Expand Down
3 changes: 3 additions & 0 deletions docker/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
DEFAULT_USER_AGENT = "docker-sdk-python/{0}".format(version)
DEFAULT_NUM_POOLS = 25
DEFAULT_DATA_CHUNK_SIZE = 1024 * 2048

DEFAULT_SWARM_ADDR_POOL = ['10.0.0.0/8']
DEFAULT_SWARM_SUBNET_SIZE = 24
6 changes: 3 additions & 3 deletions docker/models/swarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def get_unlock_key(self):
get_unlock_key.__doc__ = APIClient.get_unlock_key.__doc__

def init(self, advertise_addr=None, listen_addr='0.0.0.0:2377',
default_addr_pool=[], subnet_size=24,
default_addr_pool=None, subnet_size=None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here for parameters.

force_new_cluster=False, **kwargs):
"""
Initialize a new swarm on this Engine.
Expand All @@ -58,9 +58,9 @@ def init(self, advertise_addr=None, listen_addr='0.0.0.0:2377',
default_addr_pool (list of str): Default Address Pool specifies
default subnet pools for global scope networks. Each pool
should be specified as a CIDR block, like '10.0.0.0/16'.
Default: []
Default: None
subnet_size (int): SubnetSize specifies the subnet size of the
networks created from the default subnet pool. Default: 24
networks created from the default subnet pool. Default: None
force_new_cluster (bool): Force creating a new Swarm, even if
already part of one. Default: False
task_history_retention_limit (int): Maximum number of tasks
Expand Down
23 changes: 14 additions & 9 deletions tests/integration/api_swarm_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,31 @@ def test_init_swarm_force_new_cluster(self):

@requires_api_version('1.39')
def test_init_swarm_custom_addr_pool(self):
# test defaults
assert self.init_swarm()
results_1 = self.client.inspect_swarm()
assert results_1['DefaultAddrPool'] is None
assert set(results_1['DefaultAddrPool']) == {'10.0.0.0/8'}
assert results_1['SubnetSize'] == 24

# test addr pool alone
assert self.init_swarm(default_addr_pool=['2.0.0.0/16'],
force_new_cluster=True)
results_2 = self.client.inspect_swarm()
assert set(results_2['DefaultAddrPool']) == (
{'2.0.0.0/16'}
)
assert set(results_2['DefaultAddrPool']) == {'2.0.0.0/16'}
assert results_2['SubnetSize'] == 24

# test subnet size alone
assert self.init_swarm(subnet_size=26,
force_new_cluster=True)
results_3 = self.client.inspect_swarm()
assert set(results_3['DefaultAddrPool']) == {'10.0.0.0/8'}
assert results_3['SubnetSize'] == 26
# test both arguments together
assert self.init_swarm(default_addr_pool=['2.0.0.0/16', '3.0.0.0/16'],
subnet_size=28, force_new_cluster=True)
results_3 = self.client.inspect_swarm()
assert set(results_3['DefaultAddrPool']) == (
results_4 = self.client.inspect_swarm()
assert set(results_4['DefaultAddrPool']) == (
{'2.0.0.0/16', '3.0.0.0/16'}
)
assert results_3['SubnetSize'] == 28
assert results_4['SubnetSize'] == 28

@requires_api_version('1.24')
def test_init_already_in_cluster(self):
Expand Down