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

Add support to multi host port binding #116

Merged
merged 7 commits into from
Sep 9, 2024
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
10 changes: 9 additions & 1 deletion fixtures.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ fi
sudocker network rm runlike_fixture_bridge
sudocker network create runlike_fixture_bridge

sudocker network rm runlike_custom-net
sudocker network create --subnet=10.10.0.0/16 runlike_custom-net


sudocker run -d --name runlike_fixture1 \
--hostname Essos \
--expose 1000 \
Expand Down Expand Up @@ -79,4 +83,8 @@ sudocker run -d --name runlike_fixture5 \
--link runlike_fixture1 \
runlike_fixture


sudocker run -d --name runlike_fixture6 \
-p 127.0.0.1:602:600/udp \
-p 10.10.0.1:602:600/udp \
runlike_fixture \
bash -c 'bash sleep.sh'
2 changes: 1 addition & 1 deletion inspect_fixtures.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ function sudocker() {
sudo docker "$@"
}

for i in {1..5}; do
for i in {1..6}; do
sudocker container inspect runlike_fixture$i;
done
30 changes: 19 additions & 11 deletions runlike/inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,32 +90,38 @@ def parse_macaddress(self):
pass

def parse_ports(self):
ports = self.get_container_fact("NetworkSettings.Ports") or {}
ports = self.get_container_fact("NetworkSettings.Ports") or {}
ports.update(self.get_container_fact("HostConfig.PortBindings") or {})

if ports:
for container_port_and_protocol, options in ports.items():
for container_port_and_protocol, options_loop in ports.items():
container_port, protocol = container_port_and_protocol.split('/')
protocol_part = '' if protocol == 'tcp' else '/udp'
option_part = '-p '
host_port_part = ''
hostname_part = ''

if options is None:
if options_loop is None:
# --expose
option_part = '--expose='

self.options.append(f"{option_part}{hostname_part}{host_port_part}{container_port}{protocol_part}")
else:
# -p
host_ip = options[0]['HostIp']
host_port = options[0]['HostPort']
for host in options_loop:
# -p
host_ip = host['HostIp']
host_port = host['HostPort']

if host_port != '0' and host_port != '':
host_port_part = f"{host_port}:"

if host_port != '0' and host_port != '':
host_port_part = f"{host_port}:"
if host_ip not in ['0.0.0.0', '::', '']:
hostname_part = f"{host_ip}:"

self.options.append(f"{option_part}{hostname_part}{host_port_part}{container_port}{protocol_part}")

if host_ip not in ['0.0.0.0', '']:
hostname_part = f"{host_ip}:"
if self.options[-1] == self.options[-2] : self.options.pop()

self.options.append(f"{option_part}{hostname_part}{host_port_part}{container_port}{protocol_part}")

def parse_links(self):
links = self.get_container_fact("HostConfig.Links")
Expand Down Expand Up @@ -149,6 +155,8 @@ def parse_restart(self):
restart = self.get_container_fact("HostConfig.RestartPolicy.Name")
if not restart:
return
elif restart in ["no"]:
return
elif restart == 'on-failure':
max_retries = self.get_container_fact(
"HostConfig.RestartPolicy.MaximumRetryCount")
Expand Down
9 changes: 6 additions & 3 deletions test_runlike.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class BaseTest(unittest.TestCase):
@classmethod
def start_runlike(cls, args: List[str]):
runner = CliRunner()
cls.outputs = [""] * 6
for i in range(1, 6):
cls.outputs = [""] * 7
for i in range(1, 7):
result = runner.invoke(cli, args + [f"runlike_fixture{i}"])
assert result.exit_code == 0, "runlike did not finish successfully"
cls.outputs[i] = result.output
Expand Down Expand Up @@ -57,6 +57,8 @@ def test_udp_with_host_port(self):

def test_udp_with_host_port_and_ip(self):
self.expect_substr("-p 127.0.0.1:601:600/udp \\")
self.expect_substr("-p 127.0.0.1:602:600/udp \\", 6)
self.expect_substr("-p 10.10.0.1:602:600/udp \\", 6)

def test_host_volumes(self):
cur_dir = os.path.dirname(os.path.realpath(__file__))
Expand Down Expand Up @@ -91,7 +93,8 @@ def test_hostname_not_present(self):
self.dont_expect_substr('--hostname \\', 2)

def test_network_mode(self):
self.dont_expect_substr('--network', 1)
self.dont_expect_substr('--network=host', 1)
self.dont_expect_substr('--network=runlike_fixture_bridge', 1)
self.expect_substr('--network=host', 2)
self.expect_substr('--network=runlike_fixture_bridge', 3)

Expand Down
Loading