add hostname to network alias

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 #17370

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger 2023-07-11 15:38:24 +02:00
parent b6ec2127b8
commit f1c68b79eb
No known key found for this signature in database
GPG Key ID: EB145DD938A3CAF2
4 changed files with 19 additions and 54 deletions

View File

@ -513,8 +513,7 @@ func (c *Container) NetworkConnect(nameOrID, netName string, netOpts types.PerNe
// get network status before we connect // get network status before we connect
networkStatus := c.getNetworkStatus() networkStatus := c.getNetworkStatus()
// always add the short id as alias for docker compat netOpts.Aliases = append(netOpts.Aliases, getExtraNetworkAliases(c)...)
netOpts.Aliases = append(netOpts.Aliases, c.config.ID[:12])
if netOpts.InterfaceName == "" { if netOpts.InterfaceName == "" {
netOpts.InterfaceName = getFreeInterfaceName(networks) netOpts.InterfaceName = getFreeInterfaceName(networks)
@ -639,6 +638,16 @@ func getFreeInterfaceName(networks map[string]types.PerNetworkOptions) string {
return "" 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 // DisconnectContainerFromNetwork removes a container from its network
func (r *Runtime) DisconnectContainerFromNetwork(nameOrID, netName string, force bool) error { func (r *Runtime) DisconnectContainerFromNetwork(nameOrID, netName string, force bool) error {
ctr, err := r.LookupContainer(nameOrID) ctr, err := r.LookupContainer(nameOrID)

View File

@ -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") 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, getExtraNetworkAliases(ctr)...)
opts.Aliases = append(opts.Aliases, ctr.config.ID[:12])
normalizeNetworks[netName] = opts normalizeNetworks[netName] = opts
} }

View File

@ -1108,7 +1108,8 @@ EXPOSE 2004-2005/tcp`, ALPINE)
Expect(session).Should(Exit(0)) Expect(session).Should(Exit(0))
pod2 := "testpod2" 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() session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0)) Expect(session).Should(Exit(0))
@ -1128,40 +1129,8 @@ EXPOSE 2004-2005/tcp`, ALPINE)
session = podmanTest.Podman([]string{"run", "--name", "con4", "--network", net, ALPINE, "nslookup", pod2 + ".dns.podman"}) session = podmanTest.Podman([]string{"run", "--name", "con4", "--network", net, ALPINE, "nslookup", pod2 + ".dns.podman"})
session.WaitWithDefaultTimeout() session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0)) Expect(session).Should(Exit(0))
})
It("podman run check dnsname plugin with Netavark", func() { session = podmanTest.Podman([]string{"run", "--network", net, ALPINE, "nslookup", hostname})
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.WaitWithDefaultTimeout() session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0)) Expect(session).Should(Exit(0))
}) })
@ -1179,20 +1148,6 @@ EXPOSE 2004-2005/tcp`, ALPINE)
Expect(session.OutputToString()).To(ContainSubstring("search dns.podman")) 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() { 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. // This is harmless when run as root, so we'll just let it run.
ctrName := "testctr" ctrName := "testctr"

View File

@ -471,8 +471,10 @@ load helpers.network
run_podman run -d --network $netname $IMAGE top run_podman run -d --network $netname $IMAGE top
background_cid=$output background_cid=$output
local hostname=host-$(random_string 10)
# Run a httpd container on first network with exposed port # Run a httpd container on first network with exposed port
run_podman run -d -p "$HOST_PORT:80" \ run_podman run -d -p "$HOST_PORT:80" \
--hostname $hostname \
--network $netname \ --network $netname \
-v $INDEX1:/var/www/index.txt:Z \ -v $INDEX1:/var/www/index.txt:Z \
-w /var/www \ -w /var/www \
@ -490,7 +492,7 @@ load helpers.network
# check network alias for container short id # check network alias for container short id
run_podman inspect $cid --format "{{(index .NetworkSettings.Networks \"$netname\").Aliases}}" 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 # check /etc/hosts for our entry
run_podman exec $cid cat /etc/hosts run_podman exec $cid cat /etc/hosts
@ -550,7 +552,7 @@ load helpers.network
# check network2 alias for container short id # check network2 alias for container short id
run_podman inspect $cid --format "{{(index .NetworkSettings.Networks \"$netname2\").Aliases}}" 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 # curl should work
run curl --max-time 3 -s $SERVER/index.txt run curl --max-time 3 -s $SERVER/index.txt