Skip to content

Commit

Permalink
Merge pull request #1998 from Luap99/rootlessnetns-info
Browse files Browse the repository at this point in the history
libnetwork: add option to return rootless-netns ips
  • Loading branch information
openshift-merge-bot[bot] committed May 16, 2024
2 parents 6310b06 + fca82ba commit 907018c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
7 changes: 7 additions & 0 deletions libnetwork/cni/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,10 @@ func (n *cniNetwork) RunInRootlessNetns(toRun func() error) error {
}
return n.rootlessNetns.Run(n.lock, toRun)
}

func (n *cniNetwork) RootlessNetnsInfo() (*types.RootlessNetnsInfo, error) {
if n.rootlessNetns == nil {
return nil, types.ErrNotRootlessNetns
}
return n.rootlessNetns.Info(), nil
}
5 changes: 5 additions & 0 deletions libnetwork/internal/rootlessnetns/netns_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rootlessnetns
import (
"errors"

"github.com/containers/common/libnetwork/types"
"github.com/containers/common/pkg/config"
"github.com/containers/storage/pkg/lockfile"
)
Expand All @@ -26,3 +27,7 @@ func (n *Netns) Teardown(nets int, toRun func() error) error {
func (n *Netns) Run(lock *lockfile.LockFile, toRun func() error) error {
return ErrNotSupported
}

func (n *Netns) Info() *types.RootlessNetnsInfo {
return &types.RootlessNetnsInfo{}
}
35 changes: 34 additions & 1 deletion libnetwork/internal/rootlessnetns/netns_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io/fs"
"net"
"os"
"path/filepath"
"strconv"
Expand All @@ -13,6 +14,7 @@ import (
"github.com/containers/common/libnetwork/pasta"
"github.com/containers/common/libnetwork/resolvconf"
"github.com/containers/common/libnetwork/slirp4netns"
"github.com/containers/common/libnetwork/types"
"github.com/containers/common/pkg/config"
"github.com/containers/common/pkg/netns"
"github.com/containers/common/pkg/systemd"
Expand Down Expand Up @@ -51,6 +53,12 @@ type Netns struct {

// config contains containers.conf options.
config *config.Config

// ipAddresses used in the netns, this is needed to store
// the netns ips that are used by pasta. This is then handed
// back to the caller via IPAddresses() which then can make
// sure to not use them for host.containers.internal.
ipAddresses []net.IP
}

type rootlessNetnsError struct {
Expand Down Expand Up @@ -521,7 +529,24 @@ func (n *Netns) runInner(toRun func() error) (err error) {
if err := n.setupMounts(); err != nil {
return err
}
return toRun()
if err := toRun(); err != nil {
return err
}

// get the current active addresses in the netns, and store them
addrs, err := net.InterfaceAddrs()
if err != nil {
return err
}
ips := make([]net.IP, 0, len(addrs))
for _, addr := range addrs {
// make sure to skip localhost and other special addresses
if ipnet, ok := addr.(*net.IPNet); ok && ipnet.IP.IsGlobalUnicast() {
ips = append(ips, ipnet.IP)
}
}
n.ipAddresses = ips
return nil
})
}

Expand Down Expand Up @@ -597,6 +622,14 @@ func (n *Netns) Run(lock *lockfile.LockFile, toRun func() error) error {
return inErr
}

// IPAddresses returns the currently used ip addresses in the netns
// These should then not be assigned for the host.containers.internal entry.
func (n *Netns) Info() *types.RootlessNetnsInfo {
return &types.RootlessNetnsInfo{
IPAddresses: n.ipAddresses,
}
}

func refCount(dir string, inc int) (int, error) {
file := filepath.Join(dir, refCountFile)
content, err := os.ReadFile(file)
Expand Down
7 changes: 7 additions & 0 deletions libnetwork/netavark/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,10 @@ func (n *netavarkNetwork) RunInRootlessNetns(toRun func() error) error {
}
return n.rootlessNetns.Run(n.lock, toRun)
}

func (n *netavarkNetwork) RootlessNetnsInfo() (*types.RootlessNetnsInfo, error) {
if n.rootlessNetns == nil {
return nil, types.ErrNotRootlessNetns
}
return n.rootlessNetns.Info(), nil
}
10 changes: 10 additions & 0 deletions libnetwork/types/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ type ContainerNetwork interface {
// Only used as rootless and should return an error as root.
RunInRootlessNetns(toRun func() error) error

// RootlessNetnsInfo return extra information about the rootless netns.
// Only valid when called after Setup().
// Only used as rootless and should return an error as root.
RootlessNetnsInfo() (*RootlessNetnsInfo, error)

// Drivers will return the list of supported network drivers
// for this interface.
Drivers() []string
Expand Down Expand Up @@ -334,6 +339,11 @@ type TeardownOptions struct {
NetworkOptions
}

type RootlessNetnsInfo struct {
// IPAddresses used in the netns, must not be used for host.containers.internal
IPAddresses []net.IP
}

// FilterFunc can be passed to NetworkList to filter the networks.
type FilterFunc func(Network) bool

Expand Down

1 comment on commit 907018c

@packit-as-a-service
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

podman-next COPR build failed. @containers/packit-build please check.

Please sign in to comment.