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:
+10
-9
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user