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

Would this package benefit from switching to pyroute2? #129

Open
Lawouach opened this issue Jan 19, 2020 · 1 comment
Open

Would this package benefit from switching to pyroute2? #129

Lawouach opened this issue Jan 19, 2020 · 1 comment
Assignees

Comments

@Lawouach
Copy link

Lawouach commented Jan 19, 2020

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

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.

@thombashi thombashi self-assigned this Jan 28, 2020
@thombashi
Copy link
Owner

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants