Answer architecture round 2: 0 🔴, 1 🟡 (0 misread); capped adds only the limit a container lacks and removes only an unset or unlimited swap, so a size in the container's own command stays

This commit is contained in:
2026-10-24 11:25:58 +02:00
parent 52641079b7
commit 5649331a5e
2 changed files with 15 additions and 13 deletions
+10 -9
View File
@@ -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:
+5 -4
View File
@@ -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)