daemon: Daemon.tryDetachContainerFromClusterNetwork: cleanup

- Remove intermediate variable
- Optimize the order of checks in the condition; check for unmanaged containers
  first, before getting information about cluster state and network information.
- Simplify the log messages, as the error would already contain the same
  information about the network (name or ID) and container (ID), so would
  print the network ID twice:

    error detaching from network <ID>: could not find network attachment for container <ID> to network <name or ID>

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-07-23 23:55:28 +02:00
parent a018bb1da7
commit 8153a7760e
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -933,18 +933,17 @@ func (daemon *Daemon) disconnectFromNetwork(container *container.Container, n *l
}
func (daemon *Daemon) tryDetachContainerFromClusterNetwork(network *libnetwork.Network, container *container.Container) {
if daemon.clusterProvider != nil && network.Info().Dynamic() && !container.Managed {
if !container.Managed && daemon.clusterProvider != nil && network.Info().Dynamic() {
if err := daemon.clusterProvider.DetachNetwork(network.Name(), container.ID); err != nil {
log.G(context.TODO()).Warnf("error detaching from network %s: %v", network.Name(), err)
log.G(context.TODO()).WithError(err).Warn("error detaching from network")
if err := daemon.clusterProvider.DetachNetwork(network.ID(), container.ID); err != nil {
log.G(context.TODO()).Warnf("error detaching from network %s: %v", network.ID(), err)
log.G(context.TODO()).WithError(err).Warn("error detaching from network")
}
}
}
attributes := map[string]string{
daemon.LogNetworkEventWithAttributes(network, "disconnect", map[string]string{
"container": container.ID,
}
daemon.LogNetworkEventWithAttributes(network, "disconnect", attributes)
})
}
func (daemon *Daemon) initializeNetworking(cfg *config.Config, container *container.Container) error {