Show interface aliases above IPs

Fixes #9.
This commit is contained in:
LeoVerto 2020-06-05 01:01:30 +02:00
parent 0544246f7c
commit 23112d8e39
3 changed files with 25 additions and 6 deletions

View file

@ -52,4 +52,4 @@ command as if you were running it in a local shell.
If you'd like to contribute to this project, there is a sample docker-compose file If you'd like to contribute to this project, there is a sample docker-compose file
using dummy containers in `test`. using dummy containers in `test`.
You can deploy it using `docker-compose -f test/docker-compose.yml up -d` You can deploy it using `docker-compose -f test/docker-compose.yml up -d`.

View file

@ -28,6 +28,7 @@ class Network:
class Interface: class Interface:
endpoint_id: str endpoint_id: str
address: str address: str
aliases: typing.List[str]
@dataclass @dataclass
@ -103,7 +104,14 @@ def get_containers(client: docker.DockerClient, verbose: bool) -> (typing.List[C
for net_name, net_info in container.attrs["NetworkSettings"]["Networks"].items(): for net_name, net_info in container.attrs["NetworkSettings"]["Networks"].items():
endpoint_id = net_info["EndpointID"] endpoint_id = net_info["EndpointID"]
interfaces.append(Interface(endpoint_id, net_info['IPAddress'])) aliases = []
if net_info["Aliases"]:
for alias in net_info["Aliases"]:
# The aliases always contain the shortened container id and container name
if alias != container.id[:12] and alias != container.name:
aliases.append(alias)
interfaces.append(Interface(endpoint_id, net_info['IPAddress'], aliases))
links.append(Link(container.id, endpoint_id, net_name)) links.append(Link(container.id, endpoint_id, net_name))
if verbose: if verbose:
@ -131,7 +139,16 @@ def draw_network(g: Graph, net: Network):
def draw_container(g: Graph, c: Container): def draw_container(g: Graph, c: Container):
iface_labels = [f"<{iface.endpoint_id}> {iface.address}" for iface in c.interfaces] iface_labels = []
for iface in c.interfaces:
iface_label = "{"
for alias in iface.aliases:
iface_label += f" {alias} |"
iface_label += f"<{iface.endpoint_id}> {iface.address} }}"
iface_labels.append(iface_label)
label = f"{{ {c.name} | {{ {' | '.join(iface_labels)} }} }}" label = f"{{ {c.name} | {{ {' | '.join(iface_labels)} }} }}"

View file

@ -11,8 +11,10 @@ services:
container_name: service_2 container_name: service_2
image: leoverto/dummy-image image: leoverto/dummy-image
networks: networks:
- network_b network_b:
- network_c network_c:
aliases:
- "s2.netc"
service_3: service_3:
container_name: service_3 container_name: service_3
image: leoverto/dummy-image image: leoverto/dummy-image