From 7ab3db89247f85a366ea3c70b076d4e28b178292 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?nils=20m=C3=A5s=C3=A9n?= Date: Tue, 8 Aug 2023 19:27:36 +0200 Subject: [PATCH] fix: only remove container id network aliases --- pkg/container/client.go | 15 ++++++++++++--- pkg/container/client_test.go | 7 ++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/pkg/container/client.go b/pkg/container/client.go index 14ca23707..5bad0cde3 100644 --- a/pkg/container/client.go +++ b/pkg/container/client.go @@ -230,9 +230,18 @@ func (client dockerClient) GetNetworkConfig(c t.Container) *network.NetworkingCo } for _, ep := range config.EndpointsConfig { - // This keeps accumulating across upgrades with no apparent added value - // so throwing the information away to prevent overflows. - ep.Aliases = nil + aliases := make([]string, 0, len(ep.Aliases)) + cidAlias := c.ID().ShortID() + + // Remove the old container ID alias from the network aliases, as it would accumulate across updates otherwise + for _, alias := range ep.Aliases { + if alias == cidAlias { + continue + } + aliases = append(aliases, alias) + } + + ep.Aliases = aliases } return config } diff --git a/pkg/container/client_test.go b/pkg/container/client_test.go index e8fcca2a4..bcb261af1 100644 --- a/pkg/container/client_test.go +++ b/pkg/container/client_test.go @@ -317,19 +317,20 @@ var _ = Describe("the client", func() { }) Describe(`GetNetworkConfig`, func() { When(`providing a container with network aliases`, func() { - It(`should purge the aliases`, func() { - aliases := []string{"One", "Two"} + It(`should omit the container ID alias`, func() { client := dockerClient{ api: docker, ClientOptions: ClientOptions{PullImages: false, IncludeRestarting: false}, } container := MockContainer(WithImageName("docker.io/prefix/imagename:latest")) + + aliases := []string{"One", "Two", container.ID().ShortID(), "Four"} endpoints := map[string]*network.EndpointSettings{ `test`: {Aliases: aliases}, } container.containerInfo.NetworkSettings = &types.NetworkSettings{Networks: endpoints} Expect(container.ContainerInfo().NetworkSettings.Networks[`test`].Aliases).To(Equal(aliases)) - Expect(client.GetNetworkConfig(container).EndpointsConfig[`test`].Aliases).To(BeEmpty()) + Expect(client.GetNetworkConfig(container).EndpointsConfig[`test`].Aliases).To(Equal([]string{"One", "Two", "Four"})) }) }) })