Skip to content

Commit

Permalink
Add options for resource limits to podman_pod (#635)
Browse files Browse the repository at this point in the history
* Add resource limiting paramters for podman_pod

Signed-off-by: nishipy <[email protected]>

* Fix doc-elements-mismatch

Signed-off-by: nishipy <[email protected]>

* Update tests for podman_pod

Signed-off-by: nishipy <[email protected]>

---------

Signed-off-by: nishipy <[email protected]>
  • Loading branch information
nishipy authored Sep 8, 2023
1 parent e025e92 commit 2a8aaf4
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
42 changes: 42 additions & 0 deletions plugins/module_utils/podman/podman_pod_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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,
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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']]

Expand Down
40 changes: 40 additions & 0 deletions plugins/modules/podman_pod.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions tests/integration/targets/podman_pod/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
38 changes: 38 additions & 0 deletions tests/integration/targets/podman_pod/tasks/resource-limit.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2a8aaf4

Please sign in to comment.