Skip to content

Commit

Permalink
fix: only remove container id network aliases (#1724)
Browse files Browse the repository at this point in the history
  • Loading branch information
piksel committed Sep 16, 2023
1 parent 650acde commit 897b171
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
15 changes: 12 additions & 3 deletions pkg/container/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/container/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}))
})
})
})
Expand Down

0 comments on commit 897b171

Please sign in to comment.