Skip to content

Commit

Permalink
add hostname to network alias
Browse files Browse the repository at this point in the history
We use the name as alias but using the hostname makes also sense and
this is what docker does. We have to keep the short id as well for
docker compat.

While adding some tests I removed some duplicated tests that were
executed twice for nv for no reason.

Fixes containers#17370

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed Jul 11, 2023
1 parent b6ec212 commit f1c68b7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 54 deletions.
13 changes: 11 additions & 2 deletions libpod/networking_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,7 @@ func (c *Container) NetworkConnect(nameOrID, netName string, netOpts types.PerNe
// get network status before we connect
networkStatus := c.getNetworkStatus()

// always add the short id as alias for docker compat
netOpts.Aliases = append(netOpts.Aliases, c.config.ID[:12])
netOpts.Aliases = append(netOpts.Aliases, getExtraNetworkAliases(c)...)

if netOpts.InterfaceName == "" {
netOpts.InterfaceName = getFreeInterfaceName(networks)
Expand Down Expand Up @@ -639,6 +638,16 @@ func getFreeInterfaceName(networks map[string]types.PerNetworkOptions) string {
return ""
}

func getExtraNetworkAliases(c *Container) []string {
// always add the short id as alias for docker compat
alias := []string{c.config.ID[:12]}
// if an explicit hostname was set add it as well
if c.config.Spec.Hostname != "" {
alias = append(alias, c.config.Spec.Hostname)
}
return alias
}

// DisconnectContainerFromNetwork removes a container from its network
func (r *Runtime) DisconnectContainerFromNetwork(nameOrID, netName string, force bool) error {
ctr, err := r.LookupContainer(nameOrID)
Expand Down
3 changes: 1 addition & 2 deletions libpod/runtime_ctr.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,7 @@ func (r *Runtime) setupContainer(ctx context.Context, ctr *Container) (_ *Contai
return nil, errors.New("failed to find free network interface name")
}
}
// always add the short id as alias for docker compat
opts.Aliases = append(opts.Aliases, ctr.config.ID[:12])
opts.Aliases = append(opts.Aliases, getExtraNetworkAliases(ctr)...)

normalizeNetworks[netName] = opts
}
Expand Down
51 changes: 3 additions & 48 deletions test/e2e/run_networking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,8 @@ EXPOSE 2004-2005/tcp`, ALPINE)
Expect(session).Should(Exit(0))

pod2 := "testpod2"
session = podmanTest.Podman([]string{"pod", "create", "--network", net, "--name", pod2})
hostname := "hostn1"
session = podmanTest.Podman([]string{"pod", "create", "--network", net, "--name", pod2, "--hostname", hostname})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))

Expand All @@ -1128,40 +1129,8 @@ EXPOSE 2004-2005/tcp`, ALPINE)
session = podmanTest.Podman([]string{"run", "--name", "con4", "--network", net, ALPINE, "nslookup", pod2 + ".dns.podman"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
})

It("podman run check dnsname plugin with Netavark", func() {
SkipIfCNI(podmanTest)
pod := "testpod"
session := podmanTest.Podman([]string{"pod", "create", "--name", pod})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))

net := createNetworkName("IntTest")
session = podmanTest.Podman([]string{"network", "create", net})
session.WaitWithDefaultTimeout()
defer podmanTest.removeNetwork(net)
Expect(session).Should(Exit(0))

pod2 := "testpod2"
session = podmanTest.Podman([]string{"pod", "create", "--network", net, "--name", pod2})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))

session = podmanTest.Podman([]string{"run", "--name", "con1", "--network", net, ALPINE, "nslookup", "con1"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))

session = podmanTest.Podman([]string{"run", "--name", "con2", "--pod", pod, "--network", net, ALPINE, "nslookup", "con2"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))

session = podmanTest.Podman([]string{"run", "--name", "con3", "--pod", pod2, ALPINE, "nslookup", "con1"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(1))
Expect(session.ErrorToString()).To(ContainSubstring("can't resolve 'con1'"))

session = podmanTest.Podman([]string{"run", "--name", "con4", "--network", net, ALPINE, "nslookup", pod2 + ".dns.podman"})
session = podmanTest.Podman([]string{"run", "--network", net, ALPINE, "nslookup", hostname})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
})
Expand All @@ -1179,20 +1148,6 @@ EXPOSE 2004-2005/tcp`, ALPINE)
Expect(session.OutputToString()).To(ContainSubstring("search dns.podman"))
})

It("podman run check dnsname adds dns search domain with Netavark", func() {
SkipIfCNI(podmanTest)
net := createNetworkName("dnsname")
session := podmanTest.Podman([]string{"network", "create", net})
session.WaitWithDefaultTimeout()
defer podmanTest.removeNetwork(net)
Expect(session).Should(Exit(0))

session = podmanTest.Podman([]string{"run", "--network", net, ALPINE, "cat", "/etc/resolv.conf"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
Expect(session.OutputToString()).To(ContainSubstring("search dns.podman"))
})

It("Rootless podman run with --net=bridge works and connects to default network", func() {
// This is harmless when run as root, so we'll just let it run.
ctrName := "testctr"
Expand Down
6 changes: 4 additions & 2 deletions test/system/500-networking.bats
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,10 @@ load helpers.network
run_podman run -d --network $netname $IMAGE top
background_cid=$output

local hostname=host-$(random_string 10)
# Run a httpd container on first network with exposed port
run_podman run -d -p "$HOST_PORT:80" \
--hostname $hostname \
--network $netname \
-v $INDEX1:/var/www/index.txt:Z \
-w /var/www \
Expand All @@ -490,7 +492,7 @@ load helpers.network

# check network alias for container short id
run_podman inspect $cid --format "{{(index .NetworkSettings.Networks \"$netname\").Aliases}}"
is "$output" "[${cid:0:12}]" "short container id in network aliases"
is "$output" "[${cid:0:12} $hostname]" "short container id and hostname in network aliases"

# check /etc/hosts for our entry
run_podman exec $cid cat /etc/hosts
Expand Down Expand Up @@ -550,7 +552,7 @@ load helpers.network

# check network2 alias for container short id
run_podman inspect $cid --format "{{(index .NetworkSettings.Networks \"$netname2\").Aliases}}"
is "$output" "[${cid:0:12}]" "short container id in network aliases"
is "$output" "[${cid:0:12} $hostname]" "short container id and hostname in network2 aliases"

# curl should work
run curl --max-time 3 -s $SERVER/index.txt
Expand Down

0 comments on commit f1c68b7

Please sign in to comment.