Skip to content

Commit

Permalink
Merge pull request #19193 from Luap99/hostname-alias
Browse files Browse the repository at this point in the history
add hostname to network alias
  • Loading branch information
openshift-merge-robot authored Jul 11, 2023
2 parents b994f86 + f1c68b7 commit 1be2ec1
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 60 deletions.
2 changes: 0 additions & 2 deletions libpod/boltdb_state_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
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
1 change: 1 addition & 0 deletions libpod/runtime_ctr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 0 additions & 6 deletions libpod/sqlite_state_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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 1be2ec1

Please sign in to comment.