From 5649331a5eaea69cca8c9dd3f14b4f4e7fcd9807 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Sat, 24 Oct 2026 11:25:58 +0200 Subject: [PATCH] =?UTF-8?q?Answer=20architecture=20round=202:=200=20?= =?UTF-8?q?=F0=9F=94=B4,=201=20=F0=9F=9F=A1=20(0=20misread);=20capped=20ad?= =?UTF-8?q?ds=20only=20the=20limit=20a=20container=20lacks=20and=20removes?= =?UTF-8?q?=20only=20an=20unset=20or=20unlimited=20swap,=20so=20a=20size?= =?UTF-8?q?=20in=20the=20container's=20own=20command=20stays?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/docker_command.py | 19 ++++++++++--------- bin/test_docker_command.py | 9 +++++---- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/bin/docker_command.py b/bin/docker_command.py index abc47d86..a99ea2ad 100644 --- a/bin/docker_command.py +++ b/bin/docker_command.py @@ -9,10 +9,10 @@ STARTS_CONTAINER = (["container", "create"], ["container", "run"], ["create"], [ SIZE = r"\d+(?:\.\d+)*[kKmMgGtTpP]?[iI]?[bB]?" MEMORY_OPTIONS = ("-m", "--memory") SWAP_OPTIONS = ("--memory-swap",) -# A swap limit is a size other than 0: docker reads --memory-swap -1 as unlimited swap, and 0 as unset. -SWAP = rf"(?!0+(?:\.0+)*[kKmMgGtTpP]?[iI]?[bB]?$){SIZE}" -# Any value --memory-swap takes, -1 and 0 among them. -ANY_SWAP = rf"-1|{SIZE}" +# The values with which docker leaves swap without a limit: -1 for unlimited swap, and 0, in any unit, for unset. +UNSET_SWAP = r"-1|0+(?:\.0+)*[kKmMgGtTpP]?[iI]?[bB]?" +# A swap limit is any other size. +SWAP = rf"(?!(?:{UNSET_SWAP})$){SIZE}" def docker_subcommand(arguments: list[str]) -> list[str]: @@ -42,14 +42,15 @@ def unlimited(arguments: list[str]) -> bool: def capped(arguments: list[str], share: str) -> list[str]: - """docker's arguments with both limits set right after the subcommand, where a container they make lacks one: the container's own memory limit, or else its swap limit, or else share, for each. Its swap limit stays where it has one; an unlimited or unset one gives way.""" + """docker's arguments with the limit a container they make lacks put right after the subcommand: the container's other limit, or share where it sets neither, as docker refuses less swap than memory. An unset or unlimited swap value gives way; every other word stays.""" if not unlimited(arguments): return arguments - memory = option_value(arguments, MEMORY_OPTIONS, SIZE) or option_value(arguments, SWAP_OPTIONS, SWAP) or share - swap = option_value(arguments, SWAP_OPTIONS, SWAP) or memory - rest = without(without(arguments, MEMORY_OPTIONS, SIZE), SWAP_OPTIONS, ANY_SWAP) + memory = option_value(arguments, MEMORY_OPTIONS, SIZE) + swap = option_value(arguments, SWAP_OPTIONS, SWAP) + rest = without(arguments, SWAP_OPTIONS, UNSET_SWAP) + limits = ([] if memory else ["--memory", swap or share]) + ([] if swap else ["--memory-swap", memory or share]) after = rest.index(docker_subcommand(rest)[-1]) + 1 - return [*rest[:after], "--memory", memory, "--memory-swap", swap, *rest[after:]] + return [*rest[:after], *limits, *rest[after:]] def option_value(arguments: list[str], options: tuple[str, ...], pattern: str) -> str | None: diff --git a/bin/test_docker_command.py b/bin/test_docker_command.py index a8a44efb..01105663 100644 --- a/bin/test_docker_command.py +++ b/bin/test_docker_command.py @@ -16,11 +16,12 @@ class Capped(unittest.TestCase): def test_a_container_s_own_limit_is_kept_and_sets_the_limit_it_lacks(self): for given, expected in [ - (["run", "-m", "10g", "img"], ["run", "--memory", "10g", "--memory-swap", "10g", "img"]), - (["run", "--memory=12g", "img"], ["run", "--memory", "12g", "--memory-swap", "12g", "img"]), + (["run", "-m", "10g", "img"], ["run", "--memory-swap", "10g", "-m", "10g", "img"]), + (["run", "--memory=12g", "img"], ["run", "--memory-swap", "12g", "--memory=12g", "img"]), (["run", "--memory-swap", "2g", "img"], ["run", "--memory", "2g", "--memory-swap", "2g", "img"]), - (["run", "-m", "4g", "--memory-swap", "-1", "img"], ["run", "--memory", "4g", "--memory-swap", "4g", "img"]), - (["run", "-m4g", "--memory-swap=0", "img", "python", "-m", "pytest"], ["run", "--memory", "4g", "--memory-swap", "4g", "img", "python", "-m", "pytest"]), + (["run", "-m", "4g", "--memory-swap", "-1", "img"], ["run", "--memory-swap", "4g", "-m", "4g", "img"]), + (["run", "-m4g", "--memory-swap=0", "img", "python", "-m", "pytest"], ["run", "--memory-swap", "4g", "-m4g", "img", "python", "-m", "pytest"]), + (["run", "img", "app", "-m", "512m"], ["run", "--memory-swap", "512m", "img", "app", "-m", "512m"]), ]: with self.subTest(given): self.assertEqual(capped(given, "6144m"), expected)