Skip to content

Commit 4696e24

Browse files
author
omesser
authored
Adding resource limit support (#15)
1 parent eaa7d87 commit 4696e24

File tree

1 file changed

+152
-23
lines changed

1 file changed

+152
-23
lines changed

manof/image.py

Lines changed: 152 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ def run(self):
8787
if self.pid:
8888
command += '--pid {0} '.format(self.pid)
8989

90-
# add cpuset_cpus if needed
91-
if self.cpuset_cpus:
92-
command += '--cpuset-cpus {0} '.format(self.cpuset_cpus)
90+
command = self._add_resource_limit_arguments(command)
9391

9492
# add devices
9593
for device in self.devices:
@@ -186,25 +184,7 @@ def run(self):
186184
if cap:
187185
command += '--cap-drop={0} '.format(cap)
188186

189-
# set device cgroup rule
190-
if self.device_cgroup_rule:
191-
command += '--device-cgroup-rule={0} '.format(self.device_cgroup_rule)
192-
193-
# set device read bps
194-
if self.device_read_bps:
195-
command += '--device-read-bps={0} '.format(self.device_read_bps)
196-
197-
# set device read iops
198-
if self.device_read_iops:
199-
command += '--device-read-iops={0} '.format(self.device_read_iops)
200-
201-
# set device write bps
202-
if self.device_write_bps:
203-
command += '--device-write-bps={0} '.format(self.device_write_bps)
204-
205-
# set device write iops
206-
if self.device_write_iops:
207-
command += '--device-write-iops={0} '.format(self.device_write_iops)
187+
command = self._add_device_arguments(command)
208188

209189
# set tag
210190
command += self.image_name + ' '
@@ -328,6 +308,73 @@ def lift(self):
328308
yield self.provision()
329309
yield self.run()
330310

311+
def _add_resource_limit_arguments(self, command):
312+
"""
313+
Those route directly to docker args, see docker docs for more info:
314+
https://docs.docker.com/config/containers/resource_constraints/
315+
"""
316+
317+
# add memory limit args
318+
if self.memory:
319+
command += '--memory {0} '.format(self.memory)
320+
321+
if self.memory_reservation:
322+
command += '--memory-reservation {0} '.format(self.memory_reservation)
323+
324+
if self.kernel_memory:
325+
command += '--kernel-memory {0} '.format(self.kernel_memory)
326+
327+
if self.memory_swap:
328+
command += '--memory-swap {0} '.format(self.memory_swap)
329+
330+
if self.memory_swappiness:
331+
command += '--memory-swappiness {0} '.format(self.memory_swappiness)
332+
333+
if self.oom_kill_disable:
334+
command += '--oom-kill-disable '
335+
336+
# add cpus limit args
337+
if self.cpus:
338+
command += '--cpus {0} '.format(self.cpus)
339+
340+
if self.cpu_period:
341+
command += '--cpu-period {0} '.format(self.cpu_period)
342+
343+
if self.cpu_quota:
344+
command += '--cpu-quota {0} '.format(self.cpu_quota)
345+
346+
if self.cpuset_cpus:
347+
command += '--cpuset-cpus {0} '.format(self.cpuset_cpus)
348+
349+
if self.cpu_shares:
350+
command += '--cpu-shares {0} '.format(self.cpu_shares)
351+
352+
return command
353+
354+
def _add_device_arguments(self, command):
355+
356+
# set device cgroup rule
357+
if self.device_cgroup_rule:
358+
command += '--device-cgroup-rule={0} '.format(self.device_cgroup_rule)
359+
360+
# set device read bps
361+
if self.device_read_bps:
362+
command += '--device-read-bps={0} '.format(self.device_read_bps)
363+
364+
# set device read iops
365+
if self.device_read_iops:
366+
command += '--device-read-iops={0} '.format(self.device_read_iops)
367+
368+
# set device write bps
369+
if self.device_write_bps:
370+
command += '--device-write-bps={0} '.format(self.device_write_bps)
371+
372+
# set device write iops
373+
if self.device_write_iops:
374+
command += '--device-write-iops={0} '.format(self.device_write_iops)
375+
376+
return command
377+
331378
@property
332379
def remote_image_name(self):
333380
return os.path.join(self._determine_repository(), self.image_name)
@@ -386,10 +433,92 @@ def volumes(self):
386433
def detach(self):
387434
return True
388435

436+
@property
437+
def memory(self):
438+
"""
439+
hard mem limit
440+
:return: string e.g. "256m"
441+
"""
442+
return None
443+
444+
@property
445+
def memory_reservation(self):
446+
"""
447+
soft mem limit
448+
:return: string e.g. "256m"
449+
"""
450+
return None
451+
452+
@property
453+
def kernel_memory(self):
454+
"""
455+
max kernel mem limit
456+
:return: string e.g. "256m"
457+
"""
458+
return None
459+
460+
@property
461+
def memory_swap(self):
462+
"""
463+
amount of memory allowed to swap to disk
464+
:return: string e.g. "256m"
465+
"""
466+
return None
467+
468+
@property
469+
def memory_swappiness(self):
470+
"""
471+
mem swappiness, int, percentage [0-100]
472+
:return: string/int
473+
"""
474+
return None
475+
476+
@property
477+
def oom_kill_disable(self):
478+
"""
479+
disable default OOM kill behavior for this container
480+
:return: bool
481+
"""
482+
return False
483+
484+
@property
485+
def cpus(self):
486+
"""
487+
specify how much of the available CPU resources a container can use
488+
:return: string e.g. "1.5"
489+
"""
490+
return None
491+
492+
@property
493+
def cpu_period(self):
494+
"""
495+
specify the CPU CFS scheduler period
496+
:return: string e.g. "100000"
497+
"""
498+
return None
499+
500+
@property
501+
def cpu_quota(self):
502+
"""
503+
impose a CPU CFS quota on the container
504+
:return: string e.g. "150000"
505+
"""
506+
return None
507+
389508
@property
390509
def cpuset_cpus(self):
510+
"""
511+
Limit the specific CPUs or cores a container can use.
512+
:return: string - comma separated list "0-1,3,4" etc
513+
"""
514+
return None
391515

392-
# may be "0-1,3,4" etc
516+
@property
517+
def cpu_shares(self):
518+
"""
519+
cpu share (weight) for the container - default 1024
520+
:return: string - e.g. "2048"
521+
"""
393522
return None
394523

395524
@property

0 commit comments

Comments
 (0)