Skip to content

Commit

Permalink
libnetwork/pasta: add new Setup2 to return result
Browse files Browse the repository at this point in the history
Currently both callers in podman and buildah join and inspect the netns
to get the local ip configured by pasta in order to add it to
/etc/hosts. So instead of doing this in two places let's just do it here
once and returnt the result to the caller.

In order to not cause vendoring issues I decided against breaking the
API and added a new Setup2 function instead. I will then update
podman and buildah to make use of it.

Also I plan on adding more fields in the result, i.e. dns address.

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed Mar 13, 2024
1 parent 4423761 commit de4093f
Showing 1 changed file with 40 additions and 9 deletions.
49 changes: 40 additions & 9 deletions libnetwork/pasta/pasta.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ package pasta
import (
"errors"
"fmt"
"net"
"os/exec"
"strings"

"github.com/containernetworking/plugins/pkg/ns"
"github.com/containers/common/libnetwork/types"
"github.com/containers/common/pkg/config"
"github.com/sirupsen/logrus"
Expand All @@ -37,11 +39,21 @@ type SetupOptions struct {
ExtraOptions []string
}

// Setup start the pasta process for the given netns.
// The pasta binary is looked up in the HelperBinariesDir and $PATH.
// Note that there is no need any special cleanup logic, the pasta process will
// automatically exit when the netns path is deleted.
type SetupResult struct {
// IpAddresses configured by pasta
IPAddresses []net.IP
}

func Setup(opts *SetupOptions) error {
_, err := Setup2(opts)
return err
}

// Setup2 start the pasta process for the given netns.
// The pasta binary is looked up in the HelperBinariesDir and $PATH.
// Note that there is no need for any special cleanup logic, the pasta
// process will automatically exit when the netns path is deleted.
func Setup2(opts *SetupOptions) (*SetupResult, error) {
NoTCPInitPorts := true
NoUDPInitPorts := true
NoTCPNamespacePorts := true
Expand All @@ -51,7 +63,7 @@ func Setup(opts *SetupOptions) error {

path, err := opts.Config.FindHelperBinary(BinaryName, true)
if err != nil {
return fmt.Errorf("could not find pasta, the network namespace can't be configured: %w", err)
return nil, fmt.Errorf("could not find pasta, the network namespace can't be configured: %w", err)
}

cmdArgs := []string{}
Expand All @@ -72,7 +84,7 @@ func Setup(opts *SetupOptions) error {
case "udp":
cmdArgs = append(cmdArgs, "-u")
default:
return fmt.Errorf("can't forward protocol: %s", protocol)
return nil, fmt.Errorf("can't forward protocol: %s", protocol)
}

arg := fmt.Sprintf("%s%d-%d:%d-%d", addr,
Expand Down Expand Up @@ -140,10 +152,10 @@ func Setup(opts *SetupOptions) error {
if err != nil {
exitErr := &exec.ExitError{}
if errors.As(err, &exitErr) {
return fmt.Errorf("pasta failed with exit code %d:\n%s",
return nil, fmt.Errorf("pasta failed with exit code %d:\n%s",
exitErr.ExitCode(), string(out))
}
return fmt.Errorf("failed to start pasta: %w", err)
return nil, fmt.Errorf("failed to start pasta: %w", err)
}

if len(out) > 0 {
Expand All @@ -154,5 +166,24 @@ func Setup(opts *SetupOptions) error {
logrus.Infof("pasta logged warnings: %q", string(out))
}

return nil
result := &SetupResult{}
err = ns.WithNetNSPath(opts.Netns, func(_ ns.NetNS) error {
addrs, err := net.InterfaceAddrs()
if err != nil {
return err
}
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && ipnet.IP.IsGlobalUnicast() {
// make sure to skip localhost and other special addresses
if ipnet.IP.IsGlobalUnicast() {
result.IPAddresses = append(result.IPAddresses, ipnet.IP)
}
}
}
return nil
})
if err != nil {
return nil, err
}
return result, nil
}

0 comments on commit de4093f

Please sign in to comment.