Add runtime option to docker_container module (#47247)

* Add runtime option to docker_container module

Signed-off-by: Antoine Bardoux <abardoux@nvidia.com>

* Add changelog fragment

Signed-off-by: Antoine Bardoux <abardoux@nvidia.com>

* Add idempotency test for docker_container.runtime

Signed-off-by: Antoine Bardoux <abardoux@nvidia.com>
This commit is contained in:
Pluggi 2018-10-19 01:04:17 -07:00 committed by John R Barker
parent 131efcff9d
commit f13091d142
3 changed files with 51 additions and 0 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- "docker_container - Add runtime option."

View file

@ -359,6 +359,10 @@ options:
description:
- Use with restart policy to control maximum number of restart attempts.
default: 0
runtime:
description:
- Runtime to use for the container.
version_added: "2.8"
shm_size:
description:
- "Size of C(/dev/shm) (format: C(<number>[<unit>])). Number is positive integer.
@ -855,6 +859,7 @@ class TaskParameters(DockerBaseClass):
self.restart = None
self.restart_retries = None
self.restart_policy = None
self.runtime = None
self.shm_size = None
self.security_opts = None
self.state = None
@ -1118,6 +1123,9 @@ class TaskParameters(DockerBaseClass):
if self.client.HAS_UTS_MODE_OPT:
host_config_params['uts_mode'] = 'uts'
if self.client.HAS_RUNTIME_OPT:
host_config_params['runtime'] = 'runtime'
params = dict()
for key, value in host_config_params.items():
if getattr(self, value, None) is not None:
@ -1561,6 +1569,7 @@ class Container(DockerBaseClass):
expected_ports=host_config.get('PortBindings'),
read_only=host_config.get('ReadonlyRootfs'),
restart_policy=restart_policy.get('Name'),
runtime=host_config.get('Runtime'),
# Cannot test shm_size, as shm_size is not included in container inspection results.
# shm_size=host_config.get('ShmSize'),
security_opts=host_config.get("SecurityOpt"),
@ -2421,6 +2430,10 @@ class AnsibleDockerClientContainer(AnsibleDockerClient):
self.module.warn("docker API version is %s. Minimum version required is 1.25 to set or "
"update the container's stop_timeout configuration." % (docker_api_version,))
runtime_supported = LooseVersion(docker_api_version) >= LooseVersion('1.12')
if self.module.params.get("runtime") and not runtime_supported:
self.fail('docker API version is %s. Minimum version required is 1.12 to set runtime option.' % (docker_api_version,))
self.HAS_INIT_OPT = init_supported
self.HAS_UTS_MODE_OPT = uts_mode_supported
self.HAS_BLKIO_WEIGHT_OPT = blkio_weight_supported
@ -2428,6 +2441,8 @@ class AnsibleDockerClientContainer(AnsibleDockerClient):
self.HAS_STOP_TIMEOUT_OPT = stop_timeout_supported
self.HAS_AUTO_REMOVE_OPT = HAS_DOCKER_PY_2 or HAS_DOCKER_PY_3
self.HAS_RUNTIME_OPT = runtime_supported
if self.module.params.get('auto_remove') and not self.HAS_AUTO_REMOVE_OPT:
self.fail("'auto_remove' is not compatible with the 'docker-py' Python package. It requires the newer 'docker' Python package.")
@ -2496,6 +2511,7 @@ def main():
restart=dict(type='bool', default=False),
restart_policy=dict(type='str', choices=['no', 'on-failure', 'always', 'unless-stopped']),
restart_retries=dict(type='int', default=None),
runtime=dict(type='str', default=None),
security_opts=dict(type='list'),
shm_size=dict(type='str'),
state=dict(type='str', choices=['absent', 'present', 'started', 'stopped'], default='started'),

View file

@ -2457,6 +2457,39 @@
- restart_retries_2 is not changed
- restart_retries_3 is changed
####################################################################
## runtime #########################################################
####################################################################
- name: runtime
docker_container:
image: alpine:3.8
command: '/bin/sh -v -c "sleep 10m"'
name: "{{ cname }}"
runtime: runc
state: started
register: runtime_1
- name: runtime (idempotency)
docker_container:
image: alpine:3.8
command: '/bin/sh -v -c "sleep 10m"'
name: "{{ cname }}"
runtime: runc
state: started
register: runtime_2
- name: cleanup
docker_container:
name: "{{ cname }}"
state: absent
stop_timeout: 1
- assert:
that:
- runtime_1 is changed
- runtime_2 is not changed
####################################################################
## security_opts ###################################################
####################################################################