Merge pull request #233 from jacobtruman/main · adobe/buildrunner@bd09d84 · GitHub
Skip to content

Commit bd09d84

Browse files
authored
Merge pull request #233 from jacobtruman/main
Add docker resource limit options
2 parents 2682638 + 08d5785 commit bd09d84

7 files changed

Lines changed: 368 additions & 0 deletions

File tree

‎README.rst‎

Lines changed: 5 additions & 0 deletions

‎buildrunner/config/models_step.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ class RunAndServicesBase(StepTask):
108108
systemd_cgroup2: Optional[bool] = None
109109
containers: Optional[List[str]] = None
110110
caches: Optional[Dict[str, Union[str, List[str]]]] = None
111+
# Resource limits
112+
mem_limit: Optional[Union[str, int]] = Field(alias="mem-limit", default=None)
113+
cpu_shares: Optional[int] = Field(alias="cpu-shares", default=None)
114+
cpu_period: Optional[int] = Field(alias="cpu-period", default=None)
115+
cpu_quota: Optional[int] = Field(alias="cpu-quota", default=None)
111116

112117

113118
class Service(RunAndServicesBase):

‎buildrunner/docker/runner.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ def start(
176176
cap_add=None,
177177
privileged=False,
178178
network=None,
179+
mem_limit=None,
180+
cpu_shares=None,
181+
cpu_period=None,
182+
cpu_quota=None,
179183
):
180184
"""
181185
Kwargs:
@@ -266,6 +270,10 @@ def start(
266270
privileged=privileged,
267271
tmpfs=tmpfs,
268272
cgroupns=cgroupns,
273+
mem_limit=mem_limit,
274+
cpu_shares=cpu_shares,
275+
cpu_period=cpu_period,
276+
cpu_quota=cpu_quota,
269277
),
270278
}
271279
if entrypoint:

‎buildrunner/steprunner/tasks/run.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,10 @@ def _start_service_container(self, name, service: Service):
673673
systemd=systemd,
674674
systemd_cgroup2=self.is_systemd_cgroup2(systemd, service, _image),
675675
network=self.step_runner.network_name,
676+
mem_limit=service.mem_limit,
677+
cpu_shares=service.cpu_shares,
678+
cpu_period=service.cpu_period,
679+
cpu_quota=service.cpu_quota,
676680
)
677681
self._service_links[cont_name] = name
678682

@@ -1052,6 +1056,16 @@ def run(self, context: dict): # pylint: disable=too-many-statements,too-many-br
10521056
if self.step.privileged:
10531057
container_args["privileged"] = self.step.privileged
10541058

1059+
# set resource limits if specified
1060+
if self.step.mem_limit:
1061+
container_args["mem_limit"] = self.step.mem_limit
1062+
if self.step.cpu_shares:
1063+
container_args["cpu_shares"] = self.step.cpu_shares
1064+
if self.step.cpu_period:
1065+
container_args["cpu_period"] = self.step.cpu_period
1066+
if self.step.cpu_quota:
1067+
container_args["cpu_quota"] = self.step.cpu_quota
1068+
10551069
# only expose ports for a run step if the flag is set
10561070
if self.step_runner.build_runner.publish_ports and self.step.ports:
10571071
container_args["ports"] = self.step.ports

‎docs/resource-limits.rst‎

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
####################################
2+
CPU and Memory Resource Limits
3+
####################################
4+
5+
Overview
6+
========
7+
8+
This feature adds the ability to specify CPU and memory limits for Docker containers in buildrunner configurations. These limits are passed to Docker's ``create_host_config`` method, allowing fine-grained control over container resource usage.
9+
10+
.. contents::
11+
:local:
12+
13+
Configuration Options
14+
=====================
15+
16+
The following resource limit fields are available for both run steps and service containers:
17+
18+
- ``mem-limit``: Memory limit (string like "512m", "1g" or integer in bytes)
19+
- ``cpu-shares``: CPU shares (relative weight, default is 1024)
20+
- ``cpu-period``: CPU period in microseconds (default is 100000)
21+
- ``cpu-quota``: CPU quota in microseconds
22+
23+
Usage Examples
24+
==============
25+
26+
Memory Limit
27+
------------
28+
29+
.. code:: yaml
30+
31+
steps:
32+
my-step:
33+
run:
34+
image: ubuntu:20.04
35+
mem-limit: "512m" # or 536870912 for bytes
36+
cmd: echo "Running with memory limit"
37+
38+
CPU Limits
39+
----------
40+
41+
.. code:: yaml
42+
43+
steps:
44+
my-step:
45+
run:
46+
image: ubuntu:20.04
47+
cpu-shares: 512 # Relative weight (default 1024)
48+
cpu-period: 100000 # Period in microseconds
49+
cpu-quota: 50000 # Quota in microseconds (50% of one CPU)
50+
cmd: echo "Running with CPU limits"
51+
52+
Combined Limits
53+
---------------
54+
55+
.. code:: yaml
56+
57+
steps:
58+
my-step:
59+
run:
60+
image: ubuntu:20.04
61+
mem-limit: "1g"
62+
cpu-shares: 1024
63+
cpu-period: 100000
64+
cpu-quota: 100000
65+
cmd: echo "Running with all limits"
66+
67+
Service Container Limits
68+
-------------------------
69+
70+
.. code:: yaml
71+
72+
steps:
73+
my-step:
74+
run:
75+
image: ubuntu:20.04
76+
services:
77+
database:
78+
image: postgres:13
79+
mem-limit: "256m"
80+
cpu-shares: 512
81+
env:
82+
POSTGRES_PASSWORD: example
83+
cmd: echo "Main container with limited database service"
84+
85+
Docker Resource Limit Parameters
86+
=================================
87+
88+
Memory Limit (``mem-limit``)
89+
-----------------------------
90+
91+
- **Accepts**: String (e.g., "512m", "1g", "2g") or integer (bytes)
92+
- **Description**: Sets the maximum amount of memory the container can use
93+
- **Behavior**: Docker will enforce this limit and may kill the container if exceeded
94+
95+
CPU Shares (``cpu-shares``)
96+
----------------------------
97+
98+
- **Accepts**: Integer
99+
- **Default**: 1024
100+
- **Description**: Relative weight for CPU time allocation
101+
- **Behavior**: Higher values get more CPU time when there's contention
102+
103+
CPU Period (``cpu-period``)
104+
----------------------------
105+
106+
- **Accepts**: Integer (microseconds)
107+
- **Default**: 100000 (100ms)
108+
- **Description**: Defines the period for CPU quota enforcement
109+
110+
CPU Quota (``cpu-quota``)
111+
--------------------------
112+
113+
- **Accepts**: Integer (microseconds)
114+
- **Description**: Limits CPU time within the period
115+
- **Example**: quota=50000, period=100000 = 50% of one CPU core
116+
117+
Implementation Details
118+
======================
119+
120+
Configuration Model
121+
-------------------
122+
123+
Resource limit fields are defined in the ``RunAndServicesBase`` class in ``buildrunner/config/models_step.py``. These fields are inherited by both ``StepRun`` and ``Service`` configurations.
124+
125+
Docker Runner
126+
-------------
127+
128+
The ``DockerRunner.start()`` method in ``buildrunner/docker/runner.py`` accepts resource limit parameters and passes them to Docker's ``create_host_config`` method.
129+
130+
Run Task Integration
131+
--------------------
132+
133+
The run task layer in ``buildrunner/steprunner/tasks/run.py`` extracts resource limits from step and service configurations and passes them to the Docker runner for both main containers and service containers.
134+
135+
See Also
136+
========
137+
138+
- :doc:`global-configuration` - Global buildrunner configuration options
139+
- ``examples/resource-limits-example.yaml`` - Complete working examples
140+
Lines changed: 57 additions & 0 deletions

0 commit comments

Comments
 (0)