add hostname to network alias

We use the name as alias but using the hostname makes also sense and
this is what docker does. We have to keep the short id as well for
docker compat.

While adding some tests I removed some duplicated tests that were
executed twice for nv for no reason.

Fixes #17370

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger
2023-07-11 15:38:24 +02:00
parent b6ec2127b8
commit f1c68b79eb
4 changed files with 19 additions and 54 deletions

View File

@ -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)