Skip to content

Commit f1c68b7

Browse files
committed
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 containers#17370 Signed-off-by: Paul Holzinger <pholzing@redhat.com>
1 parent b6ec212 commit f1c68b7

File tree

4 files changed

+19
-54
lines changed

4 files changed

+19
-54
lines changed

libpod/networking_common.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,8 +513,7 @@ func (c *Container) NetworkConnect(nameOrID, netName string, netOpts types.PerNe
513513
// get network status before we connect
514514
networkStatus := c.getNetworkStatus()
515515

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

519518
if netOpts.InterfaceName == "" {
520519
netOpts.InterfaceName = getFreeInterfaceName(networks)
@@ -639,6 +638,16 @@ func getFreeInterfaceName(networks map[string]types.PerNetworkOptions) string {
639638
return ""
640639
}
641640

641+
func getExtraNetworkAliases(c *Container) []string {
642+
// always add the short id as alias for docker compat
643+
alias := []string{c.config.ID[:12]}
644+
// if an explicit hostname was set add it as well
645+
if c.config.Spec.Hostname != "" {
646+
alias = append(alias, c.config.Spec.Hostname)
647+
}
648+
return alias
649+
}
650+
642651
// DisconnectContainerFromNetwork removes a container from its network
643652
func (r *Runtime) DisconnectContainerFromNetwork(nameOrID, netName string, force bool) error {
644653
ctr, err := r.LookupContainer(nameOrID)

libpod/runtime_ctr.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,7 @@ func (r *Runtime) setupContainer(ctx context.Context, ctr *Container) (_ *Contai
282282
return nil, errors.New("failed to find free network interface name")
283283
}
284284
}
285-
// always add the short id as alias for docker compat
286-
opts.Aliases = append(opts.Aliases, ctr.config.ID[:12])
285+
opts.Aliases = append(opts.Aliases, getExtraNetworkAliases(ctr)...)
287286

288287
normalizeNetworks[netName] = opts
289288
}

test/e2e/run_networking_test.go

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,8 @@ EXPOSE 2004-2005/tcp`, ALPINE)
11081108
Expect(session).Should(Exit(0))
11091109

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

@@ -1128,40 +1129,8 @@ EXPOSE 2004-2005/tcp`, ALPINE)
11281129
session = podmanTest.Podman([]string{"run", "--name", "con4", "--network", net, ALPINE, "nslookup", pod2 + ".dns.podman"})
11291130
session.WaitWithDefaultTimeout()
11301131
Expect(session).Should(Exit(0))
1131-
})
1132-
1133-
It("podman run check dnsname plugin with Netavark", func() {
1134-
SkipIfCNI(podmanTest)
1135-
pod := "testpod"
1136-
session := podmanTest.Podman([]string{"pod", "create", "--name", pod})
1137-
session.WaitWithDefaultTimeout()
1138-
Expect(session).Should(Exit(0))
1139-
1140-
net := createNetworkName("IntTest")
1141-
session = podmanTest.Podman([]string{"network", "create", net})
1142-
session.WaitWithDefaultTimeout()
1143-
defer podmanTest.removeNetwork(net)
1144-
Expect(session).Should(Exit(0))
1145-
1146-
pod2 := "testpod2"
1147-
session = podmanTest.Podman([]string{"pod", "create", "--network", net, "--name", pod2})
1148-
session.WaitWithDefaultTimeout()
1149-
Expect(session).Should(Exit(0))
1150-
1151-
session = podmanTest.Podman([]string{"run", "--name", "con1", "--network", net, ALPINE, "nslookup", "con1"})
1152-
session.WaitWithDefaultTimeout()
1153-
Expect(session).Should(Exit(0))
1154-
1155-
session = podmanTest.Podman([]string{"run", "--name", "con2", "--pod", pod, "--network", net, ALPINE, "nslookup", "con2"})
1156-
session.WaitWithDefaultTimeout()
1157-
Expect(session).Should(Exit(0))
11581132

1159-
session = podmanTest.Podman([]string{"run", "--name", "con3", "--pod", pod2, ALPINE, "nslookup", "con1"})
1160-
session.WaitWithDefaultTimeout()
1161-
Expect(session).Should(Exit(1))
1162-
Expect(session.ErrorToString()).To(ContainSubstring("can't resolve 'con1'"))
1163-
1164-
session = podmanTest.Podman([]string{"run", "--name", "con4", "--network", net, ALPINE, "nslookup", pod2 + ".dns.podman"})
1133+
session = podmanTest.Podman([]string{"run", "--network", net, ALPINE, "nslookup", hostname})
11651134
session.WaitWithDefaultTimeout()
11661135
Expect(session).Should(Exit(0))
11671136
})
@@ -1179,20 +1148,6 @@ EXPOSE 2004-2005/tcp`, ALPINE)
11791148
Expect(session.OutputToString()).To(ContainSubstring("search dns.podman"))
11801149
})
11811150

1182-
It("podman run check dnsname adds dns search domain with Netavark", func() {
1183-
SkipIfCNI(podmanTest)
1184-
net := createNetworkName("dnsname")
1185-
session := podmanTest.Podman([]string{"network", "create", net})
1186-
session.WaitWithDefaultTimeout()
1187-
defer podmanTest.removeNetwork(net)
1188-
Expect(session).Should(Exit(0))
1189-
1190-
session = podmanTest.Podman([]string{"run", "--network", net, ALPINE, "cat", "/etc/resolv.conf"})
1191-
session.WaitWithDefaultTimeout()
1192-
Expect(session).Should(Exit(0))
1193-
Expect(session.OutputToString()).To(ContainSubstring("search dns.podman"))
1194-
})
1195-
11961151
It("Rootless podman run with --net=bridge works and connects to default network", func() {
11971152
// This is harmless when run as root, so we'll just let it run.
11981153
ctrName := "testctr"

test/system/500-networking.bats

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,10 @@ load helpers.network
471471
run_podman run -d --network $netname $IMAGE top
472472
background_cid=$output
473473

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

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

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

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

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

0 commit comments

Comments
 (0)