From faa0f98430b2ca2f7904551c7586cc0ff5b3632d Mon Sep 17 00:00:00 2001 From: Lior Okman Date: Sat, 15 Jul 2023 11:00:14 +0300 Subject: [PATCH] Added NewInNamespace function The NewInNamespace function allows initializing an IPVS handle using an already initialized netns.Handle. Signed-off-by: Lior Okman --- ipvs_linux.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ipvs_linux.go b/ipvs_linux.go index 686e746..761e3c1 100644 --- a/ipvs_linux.go +++ b/ipvs_linux.go @@ -83,8 +83,6 @@ type Handle struct { // passed path. It will return a valid handle or an error in case an // error occurred while creating the handle. func New(path string) (*Handle, error) { - setup() - n := netns.None() if path != "" { var err error @@ -94,6 +92,14 @@ func New(path string) (*Handle, error) { } } defer n.Close() + return NewInNamespace(n) +} + +// NewInNamespace provides a new ipvs handle in the namespace provided +// as a parameter. It will return a valid handle or an error in case an +// error occurred while creating the handle. +func NewInNamespace(n netns.NsHandle) (*Handle, error) { + setup() sock, err := nl.GetNetlinkSocketAt(n, netns.None(), unix.NETLINK_GENERIC) if err != nil {