mirror of https://github.com/containers/podman.git
Merge pull request #19193 from Luap99/hostname-alias
add hostname to network alias
This commit is contained in:
commit
1be2ec1d4f
|
@ -607,8 +607,6 @@ func (s *BoltState) addContainer(ctr *Container, pod *Pod) error {
|
|||
if opts.InterfaceName == "" {
|
||||
return fmt.Errorf("network interface name cannot be an empty string: %w", define.ErrInvalidArg)
|
||||
}
|
||||
// always add the short id as alias for docker compat
|
||||
opts.Aliases = append(opts.Aliases, ctr.config.ID[:12])
|
||||
optBytes, err := json.Marshal(opts)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshalling network options JSON for container %s: %w", ctr.ID(), err)
|
||||
|
|
|
@ -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)
|
||||
|
@ -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)
|
||||
|
|
|
@ -282,6 +282,7 @@ func (r *Runtime) setupContainer(ctx context.Context, ctr *Container) (_ *Contai
|
|||
return nil, errors.New("failed to find free network interface name")
|
||||
}
|
||||
}
|
||||
opts.Aliases = append(opts.Aliases, getExtraNetworkAliases(ctr)...)
|
||||
|
||||
normalizeNetworks[netName] = opts
|
||||
}
|
||||
|
|
|
@ -385,12 +385,6 @@ func (s *SQLiteState) rewriteContainerConfig(ctr *Container, newCfg *ContainerCo
|
|||
}
|
||||
|
||||
func (s *SQLiteState) addContainer(ctr *Container) (defErr error) {
|
||||
for net := range ctr.config.Networks {
|
||||
opts := ctr.config.Networks[net]
|
||||
opts.Aliases = append(opts.Aliases, ctr.config.ID[:12])
|
||||
ctr.config.Networks[net] = opts
|
||||
}
|
||||
|
||||
configJSON, err := json.Marshal(ctr.config)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshalling container config json: %w", err)
|
||||
|
|
|
@ -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))
|
||||
|
||||
|
@ -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))
|
||||
})
|
||||
|
@ -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"
|
||||
|
|
|
@ -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 \
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue