Files

138 lines
5.2 KiB
Python

import enum
class TolerationOperation(enum.Enum):
Exists = "Exists"
Equal = "Equal"
class TolerationEffect(enum.Enum):
NoSchedule = "NoSchedule"
PreferNoSchedule = "PreferNoSchedule"
NoExecute = "NoExecute"
class Toleration:
"""
Toleration is used by compute template to pick specific nodes for placing pods.
It provides APIs to create, stringify and convert to dict.
Methods:
- Create toleration: gets the following parameters:
key - required, key created by the node's taint
operator - required, operator to apply, supported operators are "Exists" and "Equal"
effect - required, toleration effect supported effects are "NoSchedule", "PreferNoSchedule", "NoExecute"
value - optional, value
- to_string() -> str: convert toleration to string for printing
- to_dict() -> dict[str, any] convert to dict
"""
def __init__(self, key: str, operator: TolerationOperation, effect: TolerationEffect,
value: str = None) -> None:
self.key = key
self.operator = operator
self.value = value
self.effect = effect
def to_string(self) -> str:
val = f"key = {self.key}, operator = {self.operator.name}, effect = {self.effect.name}"
if self.value is None:
return val
else:
return val + f", value = {self.value}"
def to_dict(self) -> dict[str, any]:
dct = {"key": self.key, "operator": self.operator.value, "effect": self.effect.value}
if self.value is not None:
dct["value"] = self.value
return dct
# Here the default gpu-accelerator is "nvidia.com/gpu", that is used for generating limits.
# If it is specified, it has to be in the format that is understood by kubernetes as a valid
# The following devices are currently supported by kubernetes:
# AMD - gpu accelerator amd.com/gpu
# Intel - gpu accelerator gpu.intel.com/i915
# NVIDIA - gpu accelerator nvidia.com/gpu
class Template:
"""
Template is used to define specific nodes configuration.
It provides APIs to create, stringify and convert to dict.
Methods:
- Create templates: gets the following parameters:
name - required, template name
namespace - required, template namespace
cpus - required, template number of cpus
memory - required, template memory (GB)
gpus - optional, number of GPUs, default 0
gpu_accelerator - optional, if not defined nvidia.com/gpu is assumed
tolerations - optional, tolerations for pod placing, default none
- to_string() -> str: convert toleration to string for printing
- to_dict() -> dict[str, any] convert to dict
- to_json() -> str convert to json string
"""
def __init__(self, name: str, namespace: str, cpu: int, memory: int, gpu: int = 0,
gpu_accelerator: str = None, tolerations: list[Toleration] = None) -> None:
self.name = name
self.namespace = namespace
self.cpu = cpu
self.memory = memory
self.gpu = gpu
self.gpu_accelerator = gpu_accelerator
self.tolerations = tolerations
def to_string(self) -> str:
val = f"name = {self.name}, namespace = {self.namespace}, cpu = {self.cpu}, memory = {self.memory}"
if self.gpu > 0:
val = val + f", gpu {self.gpu}"
if self.gpu_accelerator is not None:
val = val + f", gpu accelerator {self.gpu_accelerator}"
if self.tolerations is None:
return val
val = val + ", tolerations ["
first = True
for tol in self.tolerations:
if first:
first = False
val = val + "{" + tol.to_string() + "}"
else:
val = val + ", {" + tol.to_string() + "}"
return val + "]"
def to_dict(self) -> dict[str, any]:
dct = {"name": self.name, "namespace": self.namespace, "cpu": self.cpu, "memory": self.memory}
if self.gpu > 0:
dct["gpu"] = self.gpu
if self.gpu_accelerator is not None:
dct["gpu accelerator"] = self.gpu_accelerator
if self.tolerations is not None:
dct["tolerations"] = [tl.to_dict() for tl in self.tolerations]
return dct
"""
Creates new toleration from dictionary, used for unmarshalling json. Python does not
support multiple constructors, so do it this way
"""
def toleration_decoder(dct: dict[str, any]) -> Toleration:
return Toleration(key=dct.get("key"), operator=TolerationOperation(dct.get("operator", "Exists")),
effect=TolerationEffect(dct.get("effect", "NoSchedule")), value=dct.get("value"))
def template_decoder(dct: dict[str, any]) -> Template:
tolerations = None
if "tolerations" in dct:
tolerations = [toleration_decoder(d) for d in dct["tolerations"]]
return Template(name=dct.get("name"), namespace=dct.get("namespace"), cpu=int(dct.get("cpu", "0")),
memory=int(dct.get("memory", "0")), gpu=int(dct.get("gpu", "0")),
gpu_accelerator=dct.get("gpu_accelerator"), tolerations=tolerations)
def templates_decoder(dct: dict[str, any]) -> list[Template]:
return [template_decoder(tmp) for tmp in dct["computeTemplates"]]