Skip to content

Commit

Permalink
pkg/netns: remove NewNSWithName()
Browse files Browse the repository at this point in the history
This API is not used anywhere, as such remove it and make the loop in
NewNS() better as the netns dir parts should not be part of the loop.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
  • Loading branch information
Luap99 committed Sep 20, 2024
1 parent 8a5b951 commit 2776f6b
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions pkg/netns/netns_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,26 @@ func NewNSAtPath(nsPath string) (ns.NetNS, error) {
// NewNS creates a new persistent (bind-mounted) network namespace and returns
// an object representing that namespace, without switching to it.
func NewNS() (ns.NetNS, error) {
nsRunDir, err := GetNSRunDir()
if err != nil {
return nil, err
}

// Create the directory for mounting network namespaces
// This needs to be a shared mountpoint in case it is mounted in to
// other namespaces (containers)
err = makeNetnsDir(nsRunDir)
if err != nil {
return nil, err
}

for i := 0; i < 10000; i++ {
nsName, err := getRandomNetnsName()
if err != nil {
return nil, err
}
ns, err := NewNSWithName(nsName)
nsPath := path.Join(nsRunDir, nsName)
ns, err := newNSPath(nsPath)
if err == nil {
return ns, nil
}
Expand All @@ -80,26 +94,6 @@ func NewNS() (ns.NetNS, error) {
return nil, errNoFreeName
}

// NewNSWithName creates a new persistent (bind-mounted) network namespace and returns
// an object representing that namespace, without switching to it.
func NewNSWithName(name string) (ns.NetNS, error) {
nsRunDir, err := GetNSRunDir()
if err != nil {
return nil, err
}

// Create the directory for mounting network namespaces
// This needs to be a shared mountpoint in case it is mounted in to
// other namespaces (containers)
err = makeNetnsDir(nsRunDir)
if err != nil {
return nil, err
}

nsPath := path.Join(nsRunDir, name)
return newNSPath(nsPath)
}

// NewNSFrom creates a persistent (bind-mounted) network namespace from the
// given netns path, i.e. /proc/<pid>/ns/net, and returns the new full path to
// the bind mounted file in the netns run dir.
Expand Down

0 comments on commit 2776f6b

Please sign in to comment.