Skip to content

Commit

Permalink
some renames and add depends_on
Browse files Browse the repository at this point in the history
  • Loading branch information
stavros-k committed Oct 8, 2024
1 parent 9c00916 commit 6c30ba1
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 18 deletions.
6 changes: 6 additions & 0 deletions library/2.0.0/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .device import Devices
from .error import RenderError
from .deploy import Deploy
from .depends import Depends
from .environment import Environment
from .formatter import escape_dollar
from .validations import (
Expand All @@ -18,6 +19,7 @@
from device import Devices
from error import RenderError
from deploy import Deploy
from depends import Depends
from environment import Environment
from formatter import escape_dollar
from validations import (
Expand Down Expand Up @@ -54,6 +56,7 @@ def __init__(self, render_instance, name: str, image: str):
self.deploy: Deploy = Deploy(self.render_instance)
self.environment: Environment = Environment(self.render_instance, self.deploy.resources)
self.dns: Dns = Dns(self.render_instance)
self.depends: Depends = Depends(self.render_instance)

# self.portals: set[Portal] = set()
# self.notes: str = ""
Expand Down Expand Up @@ -166,6 +169,9 @@ def render(self) -> dict[str, Any]:
if self.dns.has_dns_opts():
result["dns_opt"] = self.dns.render_dns_opts()

if self.depends.has_dependencies():
result["depends_on"] = self.depends.render()

# if self.volume_mounts:
# result["volume_mounts"] = self.volume_mounts

Expand Down
26 changes: 26 additions & 0 deletions library/2.0.0/depends.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
try:
from .error import RenderError
from .validations import must_be_valid_depend_condition
except ImportError:
from error import RenderError
from validations import must_be_valid_depend_condition


class Depends:
def __init__(self, render_instance):
self.render_instance = render_instance
self.dependencies: dict[str, str] = {}

def add_dependency(self, name: str, condition: str):
if name in self.dependencies.keys():
raise RenderError(f"Dependency [{name}] already added")
if name not in self.render_instance.containers.keys():
raise RenderError(f"Dependency [{name}] not found in defined containers")
must_be_valid_depend_condition(condition)
self.dependencies[name] = condition

def has_dependencies(self):
return len(self.dependencies) > 0

def render(self):
return self.dependencies
13 changes: 7 additions & 6 deletions library/2.0.0/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,27 @@ def __init__(self, render_instance):
self.render_instance = render_instance
self.devices: set[Device] = set()

self._container_devices: set[str] = set()
# Tracks all container device paths to make sure they are not duplicated
self.container_device_paths: set[str] = set()
# Scan values for devices we should automatically add
# for example /dev/dri for gpus
self.add_devices_from_values()
self.auto_add_devices_from_values()

def add_devices_from_values(self):
def auto_add_devices_from_values(self):
resources = self.render_instance.values.get("resources", {})

if resources.get("gpus", {}).get("use_all_gpus", False):
self.add_device("/dev/dri", "/dev/dri", allow_disallowed=True)
self._container_devices.add("/dev/dri")
self.container_device_paths.add("/dev/dri")

def add_device(self, host_device: str, container_device: str, cgroup_perm: str = "", allow_disallowed=False):
# Host device can be mapped to multiple container devices,
# so we only make sure container devices are not duplicated
if container_device in self._container_devices:
if container_device in self.container_device_paths:
raise RenderError(f"Device with container path [{container_device}] already added")

self.devices.add(Device(host_device, container_device, cgroup_perm, allow_disallowed))
self._container_devices.add(container_device)
self.container_device_paths.add(container_device)

def has_devices(self):
return len(self.devices) > 0
Expand Down
12 changes: 6 additions & 6 deletions library/2.0.0/dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ def __init__(self, render_instance):

self._dns_opt_keys: set[str] = set()

self.auto_add_dns_opts()
self.auto_add_dns_searches()
self.auto_add_dns_nameservers()
self.auto_add_dns_opts_from_values()
self.auto_add_dns_searches_from_values()
self.auto_add_dns_nameservers_from_values()

def auto_add_dns_opts(self):
def auto_add_dns_opts_from_values(self):
values = self.render_instance.values
for dns_opt in values.get("network", {}).get("dns_opts", []):
self.add_dns_opt(dns_opt)

def auto_add_dns_searches(self):
def auto_add_dns_searches_from_values(self):
values = self.render_instance.values
for dns_search in values.get("network", {}).get("dns_searches", []):
self.add_dns_search(dns_search)

def auto_add_dns_nameservers(self):
def auto_add_dns_nameservers_from_values(self):
values = self.render_instance.values
for dns_nameserver in values.get("network", {}).get("dns_nameservers", []):
self.add_dns_nameserver(dns_nameserver)
Expand Down
12 changes: 6 additions & 6 deletions library/2.0.0/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ def __init__(self, render_instance):
self.limits: dict = {}
self.reservations: dict = {}
self.nvidia_ids: set[str] = set()
self.add_cpu_from_values()
self.add_memory_from_values()
self.add_gpus_from_values()
self.auto_add_cpu_from_values()
self.auto_add_memory_from_values()
self.auto_add_gpus_from_values()

def add_cpu_from_values(self):
def auto_add_cpu_from_values(self):
resources = self.render_instance.values.get("resources", {})
cpus = str(resources.get("limits", {}).get("cpus", DEFAULT_CPUS))
if not re.match(r"^[1-9][0-9]*(\.[0-9]+)?$", cpus):
raise RenderError(f"Expected cpus to be a number or a float, got [{cpus}]")
self.limits.update({"cpus": cpus})

def add_memory_from_values(self):
def auto_add_memory_from_values(self):
resources = self.render_instance.values.get("resources", {})
memory = str(resources.get("limits", {}).get("memory", DEFAULT_MEMORY))
if not re.match(r"^[1-9][0-9]*$", memory):
raise RenderError(f"Expected memory to be a number, got [{memory}]")
self.limits.update({"memory": f"{memory}M"})

def add_gpus_from_values(self):
def auto_add_gpus_from_values(self):
resources = self.render_instance.values.get("resources", {})
gpus = resources.get("gpus", {}).get("nvidia_gpu_selection", {})
if not gpus:
Expand Down
48 changes: 48 additions & 0 deletions library/2.0.0/tests/test_depends.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import pytest


from render import Render


@pytest.fixture
def mock_values():
return {
"images": {
"test_image": {
"repository": "nginx",
"tag": "latest",
}
},
}


def test_add_dependency(mock_values):
render = Render(mock_values)
c1 = render.add_container("test_container", "test_image")
render.add_container("test_container2", "test_image")
c1.depends.add_dependency("test_container2", "service_started")
output = render.render()
assert output["services"]["test_container"]["depends_on"] == {"test_container2": "service_started"}


def test_add_dependency_invalid_condition(mock_values):
render = Render(mock_values)
c1 = render.add_container("test_container", "test_image")
with pytest.raises(Exception):
c1.depends.add_dependency("test_container2", "invalid_condition")


def test_add_dependency_missing_container(mock_values):
render = Render(mock_values)
c1 = render.add_container("test_container", "test_image")
with pytest.raises(Exception):
c1.depends.add_dependency("test_container2", "service_started")


def test_add_dependency_duplicate(mock_values):
render = Render(mock_values)
c1 = render.add_container("test_container", "test_image")
render.add_container("test_container2", "test_image")
c1.depends.add_dependency("test_container2", "service_started")
with pytest.raises(Exception):
c1.depends.add_dependency("test_container2", "service_started")
8 changes: 8 additions & 0 deletions library/2.0.0/validations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
from error import RenderError


def must_be_valid_depend_condition(condition: str):
valid_conditions = ("service_started", "service_healthy", "service_completed_successfully")
if condition not in valid_conditions:
raise RenderError(
f"Depend Condition [{condition}] is not valid. Valid options are: [{', '.join(valid_conditions)}]"
)


def must_be_valid_cgroup_perm(cgroup_perm: str):
valid_cgroup_perms = ("r", "w", "m", "rw", "rm", "wm", "rwm")
if cgroup_perm not in valid_cgroup_perms:
Expand Down

0 comments on commit 6c30ba1

Please sign in to comment.