From 2a8aaf4522244287758bd8e95fb4c7be4ad8dc49 Mon Sep 17 00:00:00 2001 From: nishipy <41185206+nishipy@users.noreply.github.com> Date: Fri, 8 Sep 2023 19:36:31 +0900 Subject: [PATCH] Add options for resource limits to podman_pod (#635) * Add resource limiting paramters for podman_pod Signed-off-by: nishipy * Fix doc-elements-mismatch Signed-off-by: nishipy * Update tests for podman_pod Signed-off-by: nishipy --------- Signed-off-by: nishipy --- plugins/module_utils/podman/podman_pod_lib.py | 42 +++++++++++++++++++ plugins/modules/podman_pod.py | 40 ++++++++++++++++++ .../targets/podman_pod/tasks/main.yml | 8 ++++ .../podman_pod/tasks/resource-limit.yml | 38 +++++++++++++++++ 4 files changed, 128 insertions(+) create mode 100644 tests/integration/targets/podman_pod/tasks/resource-limit.yml diff --git a/plugins/module_utils/podman/podman_pod_lib.py b/plugins/module_utils/podman/podman_pod_lib.py index 860f00ed..4106136e 100644 --- a/plugins/module_utils/podman/podman_pod_lib.py +++ b/plugins/module_utils/podman/podman_pod_lib.py @@ -27,10 +27,15 @@ recreate=dict(type='bool', default=False), add_host=dict(type='list', required=False, elements='str'), cgroup_parent=dict(type='str', required=False), + blkio_weight=dict(type='str', required=False), + blkio_weight_device=dict(type='list', elements='str', required=False), cpus=dict(type='str', required=False), cpuset_cpus=dict(type='str', required=False), + cpuset_mems=dict(type='str', required=False), + cpu_shares=dict(type='str', required=False), device=dict(type='list', elements='str', required=False), device_read_bps=dict(type='list', elements='str', required=False), + device_write_bps=dict(type='list', elements='str', required=False), dns=dict(type='list', elements='str', required=False), dns_opt=dict(type='list', elements='str', required=False), dns_search=dict(type='list', elements='str', required=False), @@ -46,6 +51,8 @@ label=dict(type='dict', required=False), label_file=dict(type='str', required=False), mac_address=dict(type='str', required=False), + memory=dict(type='str', required=False), + memory_swap=dict(type='str', required=False), name=dict(type='str', required=True), network=dict(type='list', elements='str', required=False), network_alias=dict(type='list', elements='str', required=False, @@ -135,25 +142,52 @@ def addparam_add_host(self, c): c += ['--add-host', g] return c + def addparam_blkio_weight(self, c): + self.check_version('--blkio-weight', minv='4.3.0') + return c + ['--blkio-weight', self.params['blkio_weight']] + + def addparam_blkio_weight_device(self, c): + self.check_version('--blkio-weight-device', minv='4.3.0') + for dev in self.params['blkio_weight_device']: + c += ['--blkio-weight-device', dev] + return c + def addparam_cgroup_parent(self, c): return c + ['--cgroup-parent', self.params['cgroup_parent']] def addparam_cpus(self, c): + self.check_version('--cpus', minv='4.2.0') return c + ['--cpus', self.params['cpus']] def addparam_cpuset_cpus(self, c): + self.check_version('--cpus', minv='4.2.0') return c + ['--cpuset-cpus', self.params['cpuset_cpus']] + def addparam_cpuset_mems(self, c): + self.check_version('--cpuset-mems', minv='4.3.0') + return c + ['--cpuset-mems', self.params['cpuset_mems']] + + def addparam_cpu_shares(self, c): + self.check_version('--cpu-shares', minv='4.3.0') + return c + ['--cpu-shares', self.params['cpu_shares']] + def addparam_device(self, c): for dev in self.params['device']: c += ['--device', dev] return c def addparam_device_read_bps(self, c): + self.check_version('--device-read-bps', minv='4.3.0') for dev in self.params['device_read_bps']: c += ['--device-read-bps', dev] return c + def addparam_device_write_bps(self, c): + self.check_version('--device-write-bps', minv='4.3.0') + for dev in self.params['device_write_bps']: + c += ['--device-write-bps', dev] + return c + def addparam_dns(self, c): for g in self.params['dns']: c += ['--dns', g] @@ -209,6 +243,14 @@ def addparam_label_file(self, c): def addparam_mac_address(self, c): return c + ['--mac-address', self.params['mac_address']] + def addparam_memory(self, c): + self.check_version('--memory', minv='4.2.0') + return c + ['--memory', self.params['memory']] + + def addparam_memory_swap(self, c): + self.check_version('--memory-swap', minv='4.3.0') + return c + ['--memory-swap', self.params['memory_swap']] + def addparam_name(self, c): return c + ['--name', self.params['name']] diff --git a/plugins/modules/podman_pod.py b/plugins/modules/podman_pod.py index 0d3ca412..5dd3e1df 100644 --- a/plugins/modules/podman_pod.py +++ b/plugins/modules/podman_pod.py @@ -42,6 +42,18 @@ type: list elements: str required: false + blkio_weight: + description: + - Block IO relative weight. The weight is a value between 10 and 1000. + - This option is not supported on cgroups V1 rootless systems. + type: str + required: false + blkio_weight_device: + description: + - Block IO relative device weight. + type: list + elements: str + required: false cgroup_parent: description: - Path to cgroups under which the cgroup for the pod will be created. If the path @@ -61,6 +73,16 @@ Unlike `cpus` this is of type string and parsed as a list of numbers. Format is 0-3,0,1 required: false type: str + cpuset_mems: + description: + - Memory nodes in which to allow execution (0-3, 0,1). Only effective on NUMA systems. + required: false + type: str + cpu_shares: + description: + - CPU shares (relative weight). + required: false + type: str device: description: - Add a host device to the pod. Optional permissions parameter can be used to specify @@ -74,6 +96,12 @@ elements: str required: false type: list + device_write_bps: + description: + - Limit write rate (in bytes per second) to a device. + type: list + elements: str + required: false dns: description: - Set custom DNS servers in the /etc/resolv.conf file that will be shared between @@ -254,6 +282,18 @@ - Set a static MAC address for the pod's shared network. type: str required: false + memory: + description: + - Set memory limit. + - A unit can be b (bytes), k (kibibytes), m (mebibytes), or g (gibibytes). + type: str + required: false + memory_swap: + description: + - Set limit value equal to memory plus swap. + - A unit can be b (bytes), k (kibibytes), m (mebibytes), or g (gibibytes). + type: str + required: false name: description: - Assign a name to the pod. diff --git a/tests/integration/targets/podman_pod/tasks/main.yml b/tests/integration/targets/podman_pod/tasks/main.yml index 5583f11f..abee1759 100644 --- a/tests/integration/targets/podman_pod/tasks/main.yml +++ b/tests/integration/targets/podman_pod/tasks/main.yml @@ -925,3 +925,11 @@ args: apply: become: true + +- name: Test podman_pod for limiting resources + include_tasks: resource-limit.yml + vars: + ansible_python_interpreter: "/usr/bin/python3" + args: + apply: + become: true diff --git a/tests/integration/targets/podman_pod/tasks/resource-limit.yml b/tests/integration/targets/podman_pod/tasks/resource-limit.yml new file mode 100644 index 00000000..94017d9f --- /dev/null +++ b/tests/integration/targets/podman_pod/tasks/resource-limit.yml @@ -0,0 +1,38 @@ +- name: Test podman_pod for limiting resources + block: + + - name: Set variables for limiting resources + set_fact: + limit: + blkio_weight: 123 + cpuset_mems: '0-1' + cpu_shares: 1024 + device_write_bps: ['/dev/zero:1048576'] + + - name: Create pod for limiting resources + containers.podman.podman_pod: + name: limited-pod + state: created + blkio_weight: "{{ limit.blkio_weight }}" + cpuset_mems: "{{ limit.cpuset_mems }}" + cpu_shares: "{{ limit.cpu_shares }}" + device_write_bps: "{{ limit.device_write_bps }}" + + - name: Get information on pod for limiting resources + containers.podman.podman_pod_info: + name: limited-pod + register: pod_info + + - name: Check if the result is as expected + assert: + that: + - item.blkio_weight == limit.blkio_weight + - item.cpuset_mems == limit.cpuset_mems + - item.cpu_shares == limit.cpu_shares + with_items: "{{ pod_info.pods }}" + + always: + - name: Cleanup + containers.podman.podman_pod: + name: limited-pod + state: absent