Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance port mapping logic, add exposed ports to Host object #299

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
JacobCallahan marked this conversation as resolved.
Show resolved Hide resolved
}
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