Skip to content

Commit

Permalink
Add support for cgroupns_mode parameter. (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein committed Jul 15, 2022
1 parent 2f1d9b3 commit 37c868e
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/427-docker_container-cgroupns_mode.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- "docker_container - add support for ``cgroupns_mode`` (https://github.com/ansible-collections/community.docker/issues/338, https://github.com/ansible-collections/community.docker/pull/427)."
6 changes: 6 additions & 0 deletions plugins/module_utils/module_container/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,11 @@ def _preprocess_ports(module, values):
.add_option('cap_drop', type='set', elements='str')
)

OPTION_CGROUP_NS_MODE = (
OptionGroup()
.add_option('cgroupns_mode', type='str', ansible_choices=['private', 'host'])
)

OPTION_CGROUP_PARENT = (
OptionGroup()
.add_option('cgroup_parent', type='str')
Expand Down Expand Up @@ -1119,6 +1124,7 @@ def _preprocess_ports(module, values):
OPTION_BLKIO_WEIGHT,
OPTION_CAPABILITIES,
OPTION_CAP_DROP,
OPTION_CGROUP_NS_MODE,
OPTION_CGROUP_PARENT,
OPTION_COMMAND,
OPTION_CPU_PERIOD,
Expand Down
3 changes: 3 additions & 0 deletions plugins/module_utils/module_container/docker_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
OPTION_BLKIO_WEIGHT,
OPTION_CAPABILITIES,
OPTION_CAP_DROP,
OPTION_CGROUP_NS_MODE,
OPTION_CGROUP_PARENT,
OPTION_COMMAND,
OPTION_CPU_PERIOD,
Expand Down Expand Up @@ -1175,6 +1176,8 @@ def _preprocess_container_names(module, client, api_version, value):

OPTION_CAP_DROP.add_engine('docker_api', DockerAPIEngine.host_config_value('CapDrop'))

OPTION_CGROUP_NS_MODE.add_engine('docker_api', DockerAPIEngine.host_config_value('CgroupnsMode', min_api_version='1.41'))

OPTION_CGROUP_PARENT.add_engine('docker_api', DockerAPIEngine.host_config_value('CgroupParent'))

OPTION_COMMAND.add_engine('docker_api', DockerAPIEngine.config_value('Cmd'))
Expand Down
9 changes: 9 additions & 0 deletions plugins/modules/docker_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@
- List of capabilities to drop from the container.
type: list
elements: str
cgroupns_mode:
description:
- Specify the cgroup namespace mode for the container.
- The Docker CLI calls this simply C(cgroupns).
type: str
choices:
- host
- private
version_added: 3.0.0
cgroup_parent:
description:
- Specify the parent cgroup for the container.
Expand Down
54 changes: 54 additions & 0 deletions tests/integration/targets/docker_container/tasks/tests/options.yml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,60 @@
- capabilities_3 is not changed
- capabilities_4 is changed

####################################################################
## cgroupns_mode ###################################################
####################################################################

- name: cgroupns_mode
docker_container:
image: "{{ docker_test_image_alpine }}"
command: '/bin/sh -c "sleep 10m"'
name: "{{ cname }}"
state: started
cgroupns_mode: host
register: cgroupns_mode_1
ignore_errors: yes

- name: cgroupns_mode (idempotency)
docker_container:
image: "{{ docker_test_image_alpine }}"
command: '/bin/sh -c "sleep 10m"'
name: "{{ cname }}"
state: started
cgroupns_mode: host
register: cgroupns_mode_2
ignore_errors: yes

- name: cgroupns_mode (changed)
docker_container:
image: "{{ docker_test_image_alpine }}"
command: '/bin/sh -c "sleep 10m"'
name: "{{ cname }}"
state: started
cgroupns_mode: private
register: cgroupns_mode_3
ignore_errors: yes

- name: cleanup
docker_container:
name: "{{ cname }}"
state: absent
force_kill: yes
diff: no

- assert:
that:
- cgroupns_mode_1 is changed
- cgroupns_mode_2 is not changed and cgroupns_mode_2 is not failed
- "cgroupns_mode_3 is changed or 'Docker warning: Your kernel does not support cgroup namespaces. Cgroup namespace setting discarded.' in (cgroupns_mode_3.warnings | default([]))"
when: docker_api_version is version('1.41', '>=')
- assert:
that:
- cgroupns_mode_1 is failed
- |
('API version is ' ~ docker_api_version ~ '.') in cgroupns_mode_1.msg and 'Minimum version required is 1.41 ' in cgroupns_mode_1.msg
when: docker_api_version is version('1.41', '<')

####################################################################
## cgroup_parent ###################################################
####################################################################
Expand Down

0 comments on commit 37c868e

Please sign in to comment.