From 39624473b0dd65f96f2ee93d4edc5fb22fce68df Mon Sep 17 00:00:00 2001 From: David Gibson Date: Thu, 29 Jun 2023 11:02:25 +1000 Subject: [PATCH] pasta: Create /etc/hosts entries for pods using pasta networking For pods with bridged and slirp4netns networking we create /etc/hosts entries to make it more convenient for the containers to address each other. We omitted to do this for pasta networking, however. Add the necessary code to do this. Closes: https://github.com/containers/podman/issues/17922 Signed-off-by: David Gibson --- libpod/container_internal_common.go | 6 ++++++ libpod/networking_freebsd.go | 4 ++++ libpod/networking_linux.go | 11 +++++++++++ test/system/505-networking-pasta.bats | 15 +++++++++++++++ 4 files changed, 36 insertions(+) diff --git a/libpod/container_internal_common.go b/libpod/container_internal_common.go index a389abe4eb..7dfceac8a6 100644 --- a/libpod/container_internal_common.go +++ b/libpod/container_internal_common.go @@ -2203,6 +2203,12 @@ func (c *Container) getHostsEntries() (etchosts.HostEntries, error) { switch { case c.config.NetMode.IsBridge(): entries = etchosts.GetNetworkHostEntries(c.state.NetworkStatus, names...) + case c.config.NetMode.IsPasta(): + ip, err := getPastaIP(c.state) + if err != nil { + return nil, err + } + entries = etchosts.HostEntries{{IP: ip.String(), Names: names}} case c.config.NetMode.IsSlirp4netns(): ip, err := getSlirp4netnsIP(c.slirp4netnsSubnet) if err != nil { diff --git a/libpod/networking_freebsd.go b/libpod/networking_freebsd.go index aa0f9bbb3a..ab2d4557de 100644 --- a/libpod/networking_freebsd.go +++ b/libpod/networking_freebsd.go @@ -271,3 +271,7 @@ func (c *Container) reloadRootlessRLKPortMapping() error { func (c *Container) setupRootlessNetwork() error { return nil } + +func getPastaIP(state *ContainerState) (net.IP, error) { + return nil, fmt.Errorf("pasta networking is Linux only") +} diff --git a/libpod/networking_linux.go b/libpod/networking_linux.go index 8e45759153..b8485a2844 100644 --- a/libpod/networking_linux.go +++ b/libpod/networking_linux.go @@ -19,6 +19,7 @@ import ( "github.com/containers/common/libnetwork/resolvconf" "github.com/containers/common/libnetwork/slirp4netns" "github.com/containers/common/libnetwork/types" + netUtil "github.com/containers/common/libnetwork/util" "github.com/containers/common/pkg/netns" "github.com/containers/common/pkg/util" "github.com/containers/podman/v4/pkg/rootless" @@ -757,3 +758,13 @@ func (c *Container) inspectJoinedNetworkNS(networkns string) (q types.StatusBloc }) return result, err } + +func getPastaIP(state *ContainerState) (net.IP, error) { + var ip string + err := ns.WithNetNSPath(state.NetNS, func(_ ns.NetNS) error { + // get the first ip in the netns + ip = netUtil.GetLocalIP() + return nil + }) + return net.ParseIP(ip), err +} diff --git a/test/system/505-networking-pasta.bats b/test/system/505-networking-pasta.bats index e3f0e97f8a..137bf4f7ee 100644 --- a/test/system/505-networking-pasta.bats +++ b/test/system/505-networking-pasta.bats @@ -235,6 +235,21 @@ function teardown() { "Container has IPv6 global address with IPv6 disabled" } +@test "podman networking with pasta(1) - podman puts pasta IP in /etc/hosts" { + skip_if_no_ipv4 "IPv4 not routable on the host" + + pname="p$(random_string 30)" + ip="$(default_addr 4)" + + run_podman pod create --net=pasta --name "${pname}" + run_podman run --pod="${pname}" "${IMAGE}" getent hosts "${pname}" + + assert "$(echo ${output} | cut -f1 -d' ')" = "${ip}" "Correct /etc/hsots entry missing" + + run_podman pod rm "${pname}" + run_podman rmi $(pause_image) +} + ### Routes ##################################################################### @test "podman networking with pasta(1) - IPv4 default route" {