Skip to content

Commit

Permalink
always add short container id as net alias
Browse files Browse the repository at this point in the history
This matches what docker does. Also make sure the net aliases are also
shown when the container is stopped.

docker-compose uses this special alias entry to check if it is already
correclty connected to the network. [1]
Because we do not support static ips on network connect at the moment
calling disconnect && connect will loose the static ip.

Fixes containers#11748

[1] https://github.com/docker/compose/blob/0bea52b18dda3de8c28fcfb0c80cc08b8950645e/compose/service.py#L663-L667

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
  • Loading branch information
Luap99 committed Sep 27, 2021
1 parent 800d594 commit 82001b5
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 16 deletions.
6 changes: 6 additions & 0 deletions libpod/boltdb_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -1246,6 +1246,12 @@ func (s *BoltState) NetworkConnect(ctr *Container, network string, aliases []str
if err != nil {
return errors.Wrapf(err, "error adding container %s network aliases bucket for network %s", ctr.ID(), network)
}

// always add the short container id as alias
if err := ctrNetAliasesBkt.Put([]byte(ctrID[:12]), ctrID); err != nil {
return errors.Wrapf(err, "error adding container id network alias %s for network %s", ctr.ID(), network)
}

for _, alias := range aliases {
if err := ctrNetAliasesBkt.Put([]byte(alias), ctrID); err != nil {
return errors.Wrapf(err, "error adding container %s network alias %s for network %s", ctr.ID(), alias, network)
Expand Down
29 changes: 17 additions & 12 deletions libpod/boltdb_state_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,21 +706,26 @@ func (s *BoltState) addContainer(ctr *Container, pod *Pod) error {
if err := ctrNetworksBkt.Put([]byte(network), ctrID); err != nil {
return errors.Wrapf(err, "error adding network %q to networks bucket for container %s", network, ctr.ID())
}
}
}
if ctr.config.NetworkAliases != nil {
ctrAliasesBkt, err := newCtrBkt.CreateBucket(aliasesBkt)
if err != nil {
return errors.Wrapf(err, "error creating network aliases bucket for container %s", ctr.ID())
}
for net, aliases := range ctr.config.NetworkAliases {
netAliasesBkt, err := ctrAliasesBkt.CreateBucket([]byte(net))

ctrAliasesBkt, err := newCtrBkt.CreateBucket(aliasesBkt)
if err != nil {
return errors.Wrapf(err, "error creating network aliases bucket for network %q in container %s", net, ctr.ID())
return errors.Wrapf(err, "error creating network aliases bucket for container %s", ctr.ID())
}
for _, alias := range aliases {

netAliasesBkt, err := ctrAliasesBkt.CreateBucket([]byte(network))
if err != nil {
return errors.Wrapf(err, "error creating network aliases bucket for network %q in container %s", network, ctr.ID())
}

// always add the short container id as alias
if err := netAliasesBkt.Put([]byte(ctrID[:12]), ctrID); err != nil {
return errors.Wrapf(err, "error creating container id alias in network %q for container %s", network, ctr.ID())
}
logrus.Error(ctrID[:12])

for _, alias := range ctr.config.NetworkAliases[network] {
if err := netAliasesBkt.Put([]byte(alias), ctrID); err != nil {
return errors.Wrapf(err, "error creating network alias %q in network %q for container %s", alias, net, ctr.ID())
return errors.Wrapf(err, "error creating network alias %q in network %q for container %s", alias, network, ctr.ID())
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions libpod/networking_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,11 @@ func (c *Container) getContainerNetworkInfo() (*define.InspectNetworkSettings, e
for _, net := range networks {
cniNet := new(define.InspectAdditionalNetwork)
cniNet.NetworkID = net
aliases, err := c.runtime.state.GetNetworkAliases(c, net)
if err != nil {
return nil, err
}
cniNet.Aliases = aliases
settings.Networks[net] = cniNet
}
}
Expand Down
18 changes: 14 additions & 4 deletions test/e2e/network_connect_disconnect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ var _ = Describe("Podman network connect and disconnect", func() {
})

It("network disconnect with net mode slirp4netns should result in error", func() {
SkipIfRootless("network connect and disconnect are only rootful")
netName := "slirp" + stringid.GenerateNonCryptoID()
session := podmanTest.Podman([]string{"network", "create", netName})
session.WaitWithDefaultTimeout()
Expand Down Expand Up @@ -118,7 +117,6 @@ var _ = Describe("Podman network connect and disconnect", func() {
})

It("network connect with net mode slirp4netns should result in error", func() {
SkipIfRootless("network connect and disconnect are only rootful")
netName := "slirp" + stringid.GenerateNonCryptoID()
session := podmanTest.Podman([]string{"network", "create", netName})
session.WaitWithDefaultTimeout()
Expand Down Expand Up @@ -146,14 +144,20 @@ var _ = Describe("Podman network connect and disconnect", func() {
ctr := podmanTest.Podman([]string{"create", "--name", "test", "--network", netName, ALPINE, "top"})
ctr.WaitWithDefaultTimeout()
Expect(ctr).Should(Exit(0))
cid := session.OutputToString()

// network alias container short id is always added and shown in inspect
inspect := podmanTest.Podman([]string{"container", "inspect", "test", "--format", "{{(index .NetworkSettings.Networks \"" + netName + "\").Aliases}}"})
inspect.WaitWithDefaultTimeout()
Expect(inspect).Should(Exit(0))
Expect(inspect.OutputToString()).To(Equal("[" + cid[0:12] + "]"))

con := podmanTest.Podman([]string{"network", "connect", netName, "test"})
con.WaitWithDefaultTimeout()
Expect(con).Should(ExitWithError())
})

It("podman network connect", func() {
SkipIfRemote("This requires a pending PR to be merged before it will work")
netName := "aliasTest" + stringid.GenerateNonCryptoID()
session := podmanTest.Podman([]string{"network", "create", netName})
session.WaitWithDefaultTimeout()
Expand All @@ -163,6 +167,7 @@ var _ = Describe("Podman network connect and disconnect", func() {
ctr := podmanTest.Podman([]string{"run", "-dt", "--name", "test", "--network", netName, ALPINE, "top"})
ctr.WaitWithDefaultTimeout()
Expect(ctr).Should(Exit(0))
cid := session.OutputToString()

exec := podmanTest.Podman([]string{"exec", "-it", "test", "ip", "addr", "show", "eth0"})
exec.WaitWithDefaultTimeout()
Expand All @@ -184,6 +189,12 @@ var _ = Describe("Podman network connect and disconnect", func() {
Expect(inspect).Should(Exit(0))
Expect(inspect.OutputToString()).To(Equal("2"))

// network alias container short id is always added and shown in inspect
inspect = podmanTest.Podman([]string{"container", "inspect", "test", "--format", "{{(index .NetworkSettings.Networks \"" + newNetName + "\").Aliases}}"})
inspect.WaitWithDefaultTimeout()
Expect(inspect).Should(Exit(0))
Expect(inspect.OutputToString()).To(Equal("[" + cid[0:12] + "]"))

exec = podmanTest.Podman([]string{"exec", "-it", "test", "ip", "addr", "show", "eth1"})
exec.WaitWithDefaultTimeout()
Expect(exec).Should(Exit(0))
Expand All @@ -193,7 +204,6 @@ var _ = Describe("Podman network connect and disconnect", func() {
rm.WaitWithDefaultTimeout()
Expect(rm).Should(Exit(0))
Expect(rm.ErrorToString()).To(Equal(""))

})

It("podman network connect when not running", func() {
Expand Down
3 changes: 3 additions & 0 deletions test/system/500-networking.bats
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,9 @@ load helpers
run_podman inspect $cid --format "{{(index .NetworkSettings.Networks \"$netname\").MacAddress}}"
mac="$output"

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

run_podman network disconnect $netname $cid

# check that we cannot curl (timeout after 3 sec)
Expand Down

0 comments on commit 82001b5

Please sign in to comment.