generate: container: Implement Memory Accounting support
Add a Memory class and create it and its options from the Accounting class. Signed-off-by: Enno Tensing <tenno@suij.in>
This commit is contained in:
parent
6767d4e3af
commit
058af68169
1 changed files with 50 additions and 3 deletions
|
@ -180,32 +180,79 @@ class Cpu:
|
|||
return cmd
|
||||
|
||||
|
||||
@dataclass
|
||||
class Memory:
|
||||
"""Memory accounting."""
|
||||
|
||||
limit: str
|
||||
reservation: str
|
||||
swap: str
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, val: ConfigValue, logger: Log) -> Self:
|
||||
"""Create from JSON."""
|
||||
if val is None:
|
||||
return cls("", "", "")
|
||||
|
||||
if not isinstance(val, dict):
|
||||
logger.log_warning("memory Config is invalid!")
|
||||
return cls("", "", "")
|
||||
|
||||
limit = maybe_or(val, "limit", "")
|
||||
reservation = maybe_or(val, "reservation", "")
|
||||
swap = maybe_or(val, "swap", "")
|
||||
if limit == "":
|
||||
return cls("", "", "")
|
||||
return cls(limit, reservation, swap)
|
||||
|
||||
def command(self) -> str:
|
||||
"""Option for podman container create."""
|
||||
if self.limit == "":
|
||||
return ""
|
||||
cmd = ""
|
||||
seperator = " \\\n"
|
||||
|
||||
cmd += f"\t--memory={self.limit}{seperator}"
|
||||
if self.reservation != "":
|
||||
cmd += f"\t--memory-reservation={self.reservation}{seperator}"
|
||||
if self.swap != "":
|
||||
cmd += f"\t--memory-swap={self.swap}{seperator}"
|
||||
|
||||
return cmd
|
||||
|
||||
|
||||
@dataclass
|
||||
class Accounting:
|
||||
"""Resource Accounting."""
|
||||
|
||||
cgroup: Cgroup
|
||||
cpu: Cpu
|
||||
memory: Memory
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, data: ConfigValue, logger: Log) -> Self:
|
||||
"""Create from JSON."""
|
||||
if data is None:
|
||||
return cls(
|
||||
Cgroup.from_json(None, logger), Cpu.from_json(None, logger)
|
||||
Cgroup.from_json(None, logger),
|
||||
Cpu.from_json(None, logger),
|
||||
Memory.from_json(None, logger),
|
||||
)
|
||||
|
||||
cgroup_data = maybe(data, "cgroup")
|
||||
cpu_data = maybe(data, "cpu")
|
||||
memory_data = maybe(data, "memory")
|
||||
cgroup = Cgroup.from_json(cgroup_data, logger)
|
||||
cpu = Cpu.from_json(cpu_data, logger)
|
||||
return cls(cgroup, cpu)
|
||||
memory = Memory.from_json(memory_data, logger)
|
||||
return cls(cgroup, cpu, memory)
|
||||
|
||||
def command(self) -> str:
|
||||
"""Options for podman container create."""
|
||||
cgroup = self.cgroup.command()
|
||||
cpu = self.cpu.command()
|
||||
return cgroup + cpu
|
||||
memory = self.memory.command()
|
||||
return cgroup + cpu + memory
|
||||
|
||||
|
||||
@dataclass
|
||||
|
|
Loading…
Add table
Reference in a new issue