mirror of https://github.com/docker/docker-py.git
swarm: add sysctl support for services (#3029)
Signed-off-by: Quentin Mathorel <quentin.mathorel@outlook.fr>
This commit is contained in:
parent
ff0b4ac60b
commit
58aa62bb15
|
|
@ -217,6 +217,8 @@ class ServiceCollection(Collection):
|
||||||
the default set for the container.
|
the default set for the container.
|
||||||
cap_drop (:py:class:`list`): A list of kernel capabilities to drop
|
cap_drop (:py:class:`list`): A list of kernel capabilities to drop
|
||||||
from the default set for the container.
|
from the default set for the container.
|
||||||
|
sysctls (:py:class:`dict`): A dict of sysctl values to add to the
|
||||||
|
container
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
:py:class:`Service`: The created service.
|
:py:class:`Service`: The created service.
|
||||||
|
|
@ -305,6 +307,7 @@ CONTAINER_SPEC_KWARGS = [
|
||||||
'tty',
|
'tty',
|
||||||
'user',
|
'user',
|
||||||
'workdir',
|
'workdir',
|
||||||
|
'sysctls',
|
||||||
]
|
]
|
||||||
|
|
||||||
# kwargs to copy straight over to TaskTemplate
|
# kwargs to copy straight over to TaskTemplate
|
||||||
|
|
|
||||||
|
|
@ -115,6 +115,8 @@ class ContainerSpec(dict):
|
||||||
default set for the container.
|
default set for the container.
|
||||||
cap_drop (:py:class:`list`): A list of kernel capabilities to drop from
|
cap_drop (:py:class:`list`): A list of kernel capabilities to drop from
|
||||||
the default set for the container.
|
the default set for the container.
|
||||||
|
sysctls (:py:class:`dict`): A dict of sysctl values to add to
|
||||||
|
the container
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, image, command=None, args=None, hostname=None, env=None,
|
def __init__(self, image, command=None, args=None, hostname=None, env=None,
|
||||||
|
|
@ -123,7 +125,7 @@ class ContainerSpec(dict):
|
||||||
open_stdin=None, read_only=None, stop_signal=None,
|
open_stdin=None, read_only=None, stop_signal=None,
|
||||||
healthcheck=None, hosts=None, dns_config=None, configs=None,
|
healthcheck=None, hosts=None, dns_config=None, configs=None,
|
||||||
privileges=None, isolation=None, init=None, cap_add=None,
|
privileges=None, isolation=None, init=None, cap_add=None,
|
||||||
cap_drop=None):
|
cap_drop=None, sysctls=None):
|
||||||
self['Image'] = image
|
self['Image'] = image
|
||||||
|
|
||||||
if isinstance(command, str):
|
if isinstance(command, str):
|
||||||
|
|
@ -205,6 +207,12 @@ class ContainerSpec(dict):
|
||||||
|
|
||||||
self['CapabilityDrop'] = cap_drop
|
self['CapabilityDrop'] = cap_drop
|
||||||
|
|
||||||
|
if sysctls is not None:
|
||||||
|
if not isinstance(sysctls, dict):
|
||||||
|
raise TypeError('sysctls must be a dict')
|
||||||
|
|
||||||
|
self['Sysctls'] = sysctls
|
||||||
|
|
||||||
|
|
||||||
class Mount(dict):
|
class Mount(dict):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -1419,3 +1419,23 @@ class ServiceTest(BaseAPIIntegrationTest):
|
||||||
assert services[0]['ID'] == svc_id['ID']
|
assert services[0]['ID'] == svc_id['ID']
|
||||||
spec = services[0]['Spec']['TaskTemplate']['ContainerSpec']
|
spec = services[0]['Spec']['TaskTemplate']['ContainerSpec']
|
||||||
assert 'CAP_SYSLOG' in spec['CapabilityDrop']
|
assert 'CAP_SYSLOG' in spec['CapabilityDrop']
|
||||||
|
|
||||||
|
@requires_api_version('1.40')
|
||||||
|
def test_create_service_with_sysctl(self):
|
||||||
|
name = self.get_service_name()
|
||||||
|
sysctls = {
|
||||||
|
'net.core.somaxconn': '1024',
|
||||||
|
'net.ipv4.tcp_syncookies': '0',
|
||||||
|
}
|
||||||
|
container_spec = docker.types.ContainerSpec(
|
||||||
|
TEST_IMG, ['echo', 'hello'], sysctls=sysctls
|
||||||
|
)
|
||||||
|
task_tmpl = docker.types.TaskTemplate(container_spec)
|
||||||
|
svc_id = self.client.create_service(task_tmpl, name=name)
|
||||||
|
assert self.client.inspect_service(svc_id)
|
||||||
|
services = self.client.services(filters={'name': name})
|
||||||
|
assert len(services) == 1
|
||||||
|
assert services[0]['ID'] == svc_id['ID']
|
||||||
|
spec = services[0]['Spec']['TaskTemplate']['ContainerSpec']
|
||||||
|
assert spec['Sysctls']['net.core.somaxconn'] == '1024'
|
||||||
|
assert spec['Sysctls']['net.ipv4.tcp_syncookies'] == '0'
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,8 @@ class CreateServiceKwargsTest(unittest.TestCase):
|
||||||
'constraints': ['foo=bar'],
|
'constraints': ['foo=bar'],
|
||||||
'preferences': ['bar=baz'],
|
'preferences': ['bar=baz'],
|
||||||
'platforms': [('x86_64', 'linux')],
|
'platforms': [('x86_64', 'linux')],
|
||||||
'maxreplicas': 1
|
'maxreplicas': 1,
|
||||||
|
'sysctls': {'foo': 'bar'}
|
||||||
})
|
})
|
||||||
|
|
||||||
task_template = kwargs.pop('task_template')
|
task_template = kwargs.pop('task_template')
|
||||||
|
|
@ -59,5 +60,5 @@ class CreateServiceKwargsTest(unittest.TestCase):
|
||||||
assert task_template['Networks'] == [{'Target': 'somenet'}]
|
assert task_template['Networks'] == [{'Target': 'somenet'}]
|
||||||
assert set(task_template['ContainerSpec'].keys()) == {
|
assert set(task_template['ContainerSpec'].keys()) == {
|
||||||
'Image', 'Command', 'Args', 'Hostname', 'Env', 'Dir', 'User',
|
'Image', 'Command', 'Args', 'Hostname', 'Env', 'Dir', 'User',
|
||||||
'Labels', 'Mounts', 'StopGracePeriod'
|
'Labels', 'Mounts', 'StopGracePeriod', 'Sysctls'
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue