You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was wondering if this package would gain to switch to pyroute2? I was toying with that package and I thought it made the code quite clean. For instance, here is how I find the veths for a container:
from pyroute2 import NetNS, netns, IPDB, IPRoute
def get_veths_for_container(container_id: str, pid: int):
ns_name = None
for ns, pids in netns.ns_pids().items():
if pid in pids:
ns_name = ns
break
if not ns_name:
return set()
ns = NetNS(ns_name)
ipdb = IPDB()
ipdb_ns = IPDB(nl=ns)
veths = set()
try:
iface = ipdb_ns.interfaces["eth0"].ro
iface_container_index = iface["index"]
iface_container_link_to = iface["link"]
ifaces = ipdb.interfaces
for idx, iface in ifaces.items():
if iface["index"] == iface_container_link_to and \
iface["link"] == iface_container_index:
veths.add(iface["ifname"])
finally:
ipdb.release()
ipdb_ns.release()
ns.close()
return veths
@Lawouach
Thank you for your information and a good example.
pyroute2 looks very useful.
I agree with you that tcconfig source code is hard to read for the current implementation.
It might be possible to clean up the code by using pyroute2. I'll consider using the package.
I was wondering if this package would gain to switch to pyroute2? I was toying with that package and I thought it made the code quite clean. For instance, here is how I find the veths for a container:
It even exposes nicely tc.
pyroute2 is a native Python implementation which doesn't rely on the underlying ip commands, so avoid the multiple call to subprocessing.
I think I was drawn to it because I was a little lost in the code of tcconfig initially. But maybe I need more reading of it still :)
Just a thought really.
The text was updated successfully, but these errors were encountered: