cli: add container support to show interfaces

Make show interfaces container aware. This is done by looking though
all podman containers for interfaces and looking up there info from
network namespaces on the main system / hypervisor.

Interfaces controlled by containers are clearly marked in the "show
interfaces" output, with a single gray row. Telling the user that they
belong to one or more named containers. The user can then run "show
interface name NAME" on interfaces owned by containers, which provided
some additional info, such as mac address and stat counters.

The patch also add support for printing veth peers which are owned by
containers.

Lastly, the patch also adds test cases for this functionality.

Signed-off-by: Richard Alpe <richard@bit42.se>
This commit is contained in:
Richard Alpe
2024-10-29 17:23:30 +01:00
parent 31061fcc10
commit b9492c7bee
14 changed files with 762 additions and 7 deletions
+24
View File
@@ -80,6 +80,10 @@ class Decore():
def underline(txt):
return Decore.decorate("4", txt, "24")
@staticmethod
def gray_bg(txt):
return Decore.decorate("100", txt)
def datetime_now():
if UNIT_TEST:
return datetime(2023, 1, 1, 12, 0, 0, tzinfo=timezone.utc)
@@ -273,6 +277,7 @@ class Iface:
self.bridge = get_json_data('', self.data, 'infix-interfaces:bridge-port', 'bridge')
self.pvid = get_json_data('', self.data, 'infix-interfaces:bridge-port', 'pvid')
self.stp_state = get_json_data('', self.data, 'infix-interfaces:bridge-port', 'stp-state')
self.containers = get_json_data('', self.data, 'infix-interfaces:container-network', 'containers')
if data.get('statistics'):
self.in_octets = data.get('statistics').get('in-octets', '')
@@ -302,6 +307,10 @@ class Iface:
def is_vlan(self):
return self.type == "infix-if-type:vlan"
def is_in_container(self):
# Return negative if cointainer isn't set or is an empty list
return getattr(self, 'containers', None)
def is_bridge(self):
return self.type == "infix-if-type:bridge"
@@ -436,7 +445,18 @@ class Iface:
parent.pr_name(pipe='')
parent.pr_proto_eth()
def pr_container(self):
row = f"{self.name:<{Pad.iface}}"
row += f"{'container':<{Pad.proto}}"
row += f"{'':<{Pad.state}}"
row += f"{', ' . join(self.containers):<{Pad.data}}"
print(Decore.gray_bg(row))
def pr_iface(self):
if self.is_in_container():
print(Decore.gray_bg(f"{'owned by container':<{20}}: {', ' . join(self.containers)}"))
print(f"{'name':<{20}}: {self.name}")
print(f"{'index':<{20}}: {self.index}")
if self.mtu:
@@ -564,6 +584,10 @@ def pr_interface_list(json):
if iface.name == "lo":
continue
if iface.is_in_container():
iface.pr_container()
continue
if iface.is_bridge():
iface.pr_bridge(ifaces)
continue