Skip to content

Commit

Permalink
Enhance port mapping logic, add exposed ports to Host object
Browse files Browse the repository at this point in the history
  • Loading branch information
ogajduse authored and JacobCallahan committed Jul 2, 2024
1 parent c12812e commit 094b0f6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions broker/hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ def to_dict(self):
"os_distribution",
"os_distribution_version",
"reported_devices",
"exposed_ports",
)
ret_dict = {
"name": getattr(self, "name", None),
Expand Down
20 changes: 14 additions & 6 deletions broker/providers/container.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Container provider implementation."""

from functools import cache
import getpass
import inspect
Expand Down Expand Up @@ -154,6 +155,13 @@ def _port_mapping(self, image, **kwargs):
22:1337 23:1335.
"""
mapping = {}
# create mapping for all exposed ports in the image
if settings.container.auto_map_ports or kwargs.get("auto_map_ports"):
mapping = {
k: v or None
for k, v in self.runtime.image_info(image)["config"]["ExposedPorts"].items()
}
# add any ports that were passed as arguments
if ports := kwargs.pop("ports", None):
if isinstance(ports, str):
for _map in ports.split():
Expand All @@ -166,11 +174,6 @@ def _port_mapping(self, image, **kwargs):
else:
s = "tcp"
mapping[f"{p}/{s}"] = int(h) if h else None
elif settings.container.auto_map_ports:
mapping = {
k: v or None
for k, v in self.runtime.image_info(image)["config"]["ExposedPorts"].items()
}
return mapping

def _cont_inst_by_name(self, cont_name):
Expand Down Expand Up @@ -200,13 +203,18 @@ def construct_host(self, provider_params, host_classes, **kwargs):
logger.debug(cont_attrs)
hostname = cont_inst.id[:12]
if port := self._find_ssh_port(cont_attrs["ports"]):
hostname = f"{hostname}:{port}"
hostname = f"{self.runtime.host}:{port}"
if not hostname:
raise Exception(f"Could not determine container hostname:\n{cont_attrs}")
name = cont_attrs["name"]
logger.debug(f"hostname: {hostname}, name: {name}, host type: host")
host_inst = host_classes["host"](**{**kwargs, "hostname": hostname, "name": name})
self._set_attributes(host_inst, broker_args=kwargs, cont_inst=cont_inst)
# add the container's port mapping to the host instance only if there are any ports open
if cont_attrs.get("ports"):
host_inst.exposed_ports = {
f"{k.split('/')[0]}": v[0]["HostPort"] for k, v in cont_attrs["ports"].items()
}
return host_inst

def provider_help(
Expand Down
1 change: 1 addition & 0 deletions tests/providers/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def __init__(self, **kwargs):
"images": [MockStub({"tags": "ch-d:ubi8"})], # self.runtime.images
"containers": [MockStub({"tags": "f37d3058317f"})], # self.runtime.containers
"name": "f37d3058317f", # self.runtime.get_attrs(cont_inst)["name"]
"ports": MockStub({'22/tcp': [{'HostIp': '', 'HostPort': '1337'}]}), # self.runtime.get_attrs(cont_inst)["ports"]
}
if "job_id" in kwargs:
# we need to load in an image object
Expand Down

0 comments on commit 094b0f6

Please sign in to comment.