Skip to content

Commit

Permalink
Merge pull request moby#46056 from thaJeztah/daemon_cleanup_assorted
Browse files Browse the repository at this point in the history
daemon: assorted cleanups
  • Loading branch information
thaJeztah authored Aug 7, 2023
2 parents b1c112d + 4f9540f commit b32c759
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 66 deletions.
91 changes: 34 additions & 57 deletions daemon/container_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,7 @@ func (daemon *Daemon) findAndAttachNetwork(container *container.Container, idOrN

n, err := daemon.FindNetwork(id)
if err != nil {
// We should always be able to find the network for a
// managed container.
// We should always be able to find the network for a managed container.
if container.Managed {
return nil, nil, err
}
Expand Down Expand Up @@ -385,22 +384,18 @@ func (daemon *Daemon) findAndAttachNetwork(container *container.Container, idOrN
if epConfig.IPAMConfig.IPv4Address != "" {
addresses = append(addresses, epConfig.IPAMConfig.IPv4Address)
}

if epConfig.IPAMConfig.IPv6Address != "" {
addresses = append(addresses, epConfig.IPAMConfig.IPv6Address)
}
}

var (
nwCfg *networktypes.NetworkingConfig
retryCount int
)

if n == nil && daemon.attachableNetworkLock != nil {
daemon.attachableNetworkLock.Lock(id)
defer daemon.attachableNetworkLock.Unlock(id)
}

retryCount := 0
var nwCfg *networktypes.NetworkingConfig
for {
// In all other cases, attempt to attach to the network to
// trigger attachment in the swarm cluster manager.
Expand Down Expand Up @@ -526,13 +521,10 @@ func (daemon *Daemon) allocateNetwork(cfg *config.Config, container *container.C
return nil
}

var (
start = time.Now()
controller = daemon.netController
)
start := time.Now()

// Cleanup any stale sandbox left over due to ungraceful daemon shutdown
if err := controller.SandboxDestroy(container.ID); err != nil {
if err := daemon.netController.SandboxDestroy(container.ID); err != nil {
log.G(context.TODO()).WithError(err).Errorf("failed to cleanup up stale network sandbox for container %s", container.ID)
}

Expand All @@ -541,7 +533,6 @@ func (daemon *Daemon) allocateNetwork(cfg *config.Config, container *container.C
}

updateSettings := false

if len(container.NetworkSettings.Networks) == 0 {
daemon.updateContainerNetworkSettings(container, nil)
updateSettings = true
Expand Down Expand Up @@ -728,8 +719,7 @@ func (daemon *Daemon) connectToNetwork(cfg *config.Config, container *container.
if container.HostConfig.NetworkMode.IsContainer() {
return runconfig.ErrConflictSharedNetwork
}
if containertypes.NetworkMode(idOrName).IsBridge() &&
cfg.DisableBridge {
if cfg.DisableBridge && containertypes.NetworkMode(idOrName).IsBridge() {
container.Config.NetworkDisabled = true
return nil
}
Expand All @@ -744,14 +734,12 @@ func (daemon *Daemon) connectToNetwork(cfg *config.Config, container *container.
if n == nil {
return nil
}
nwName := n.Name()

var operIPAM bool
if nwCfg != nil {
if epConfig, ok := nwCfg.EndpointsConfig[n.Name()]; ok {
if endpointConfig.IPAMConfig == nil ||
(endpointConfig.IPAMConfig.IPv4Address == "" &&
endpointConfig.IPAMConfig.IPv6Address == "" &&
len(endpointConfig.IPAMConfig.LinkLocalIPs) == 0) {
if epConfig, ok := nwCfg.EndpointsConfig[nwName]; ok {
if endpointConfig.IPAMConfig == nil || (endpointConfig.IPAMConfig.IPv4Address == "" && endpointConfig.IPAMConfig.IPv6Address == "" && len(endpointConfig.IPAMConfig.LinkLocalIPs) == 0) {
operIPAM = true
}

Expand All @@ -765,7 +753,6 @@ func (daemon *Daemon) connectToNetwork(cfg *config.Config, container *container.
return err
}

controller := daemon.netController
sb := daemon.getNetworkSandbox(container)
createOptions, err := buildCreateEndpointOptions(container, n, endpointConfig, sb, cfg.DNS)
if err != nil {
Expand All @@ -784,7 +771,7 @@ func (daemon *Daemon) connectToNetwork(cfg *config.Config, container *container.
}
}
}()
container.NetworkSettings.Networks[n.Name()] = &network.EndpointSettings{
container.NetworkSettings.Networks[nwName] = &network.EndpointSettings{
EndpointSettings: endpointConfig,
IPAMOperational: operIPAM,
}
Expand All @@ -800,7 +787,7 @@ func (daemon *Daemon) connectToNetwork(cfg *config.Config, container *container.
if err != nil {
return err
}
sb, err = controller.NewSandbox(container.ID, sbOptions...)
sb, err = daemon.netController.NewSandbox(container.ID, sbOptions...)
if err != nil {
return err
}
Expand Down Expand Up @@ -885,8 +872,7 @@ func (daemon *Daemon) disconnectFromNetwork(container *container.Container, n *l
ep *libnetwork.Endpoint
sbox *libnetwork.Sandbox
)

s := func(current *libnetwork.Endpoint) bool {
n.WalkEndpoints(func(current *libnetwork.Endpoint) bool {
epInfo := current.Info()
if epInfo == nil {
return false
Expand All @@ -899,19 +885,17 @@ func (daemon *Daemon) disconnectFromNetwork(container *container.Container, n *l
}
}
return false
}
n.WalkEndpoints(s)

if ep == nil && force {
epName := strings.TrimPrefix(container.Name, "/")
ep, err := n.EndpointByName(epName)
if err != nil {
return err
}
return ep.Delete(force)
}
})

if ep == nil {
if force {
var err error
ep, err = n.EndpointByName(strings.TrimPrefix(container.Name, "/"))
if err != nil {
return err
}
return ep.Delete(force)
}
return fmt.Errorf("container %s is not connected to network %s", container.ID, n.Name())
}

Expand All @@ -933,23 +917,20 @@ 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 {
var err error

if container.HostConfig.NetworkMode.IsContainer() {
// we need to get the hosts files from the container to join
nc, err := daemon.getNetworkedContainer(container.ID, container.HostConfig.NetworkMode.ConnectedContainer())
Expand All @@ -967,13 +948,12 @@ func (daemon *Daemon) initializeNetworking(cfg *config.Config, container *contai
return nil
}

if container.HostConfig.NetworkMode.IsHost() {
if container.Config.Hostname == "" {
container.Config.Hostname, err = os.Hostname()
if err != nil {
return err
}
if container.HostConfig.NetworkMode.IsHost() && container.Config.Hostname == "" {
hn, err := os.Hostname()
if err != nil {
return err
}
container.Config.Hostname = hn
}

if err := daemon.allocateNetwork(cfg, container); err != nil {
Expand All @@ -992,8 +972,7 @@ func (daemon *Daemon) getNetworkedContainer(containerID, connectedContainerID st
return nil, fmt.Errorf("cannot join own network")
}
if !nc.IsRunning() {
err := fmt.Errorf("cannot join network of a non running container: %s", connectedContainerID)
return nil, errdefs.Conflict(err)
return nil, errdefs.Conflict(fmt.Errorf("cannot join network of a non running container: %s", connectedContainerID))
}
if nc.IsRestarting() {
return nil, errContainerIsRestarting(connectedContainerID)
Expand All @@ -1010,16 +989,14 @@ func (daemon *Daemon) releaseNetwork(container *container.Container) {
return
}

sid := container.NetworkSettings.SandboxID
settings := container.NetworkSettings.Networks
container.NetworkSettings.Ports = nil

sid := container.NetworkSettings.SandboxID
if sid == "" {
return
}

var networks []*libnetwork.Network
for n, epSettings := range settings {
for n, epSettings := range container.NetworkSettings.Networks {
if nw, err := daemon.FindNetwork(getNetworkID(n, epSettings.EndpointSettings)); err == nil {
networks = append(networks, nw)
}
Expand Down
19 changes: 10 additions & 9 deletions daemon/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -1030,33 +1030,34 @@ func buildEndpointInfo(networkSettings *internalnetwork.Settings, n *libnetwork.
return nil
}

if _, ok := networkSettings.Networks[n.Name()]; !ok {
networkSettings.Networks[n.Name()] = &internalnetwork.EndpointSettings{
nwName := n.Name()
if _, ok := networkSettings.Networks[nwName]; !ok {
networkSettings.Networks[nwName] = &internalnetwork.EndpointSettings{
EndpointSettings: &network.EndpointSettings{},
}
}
networkSettings.Networks[n.Name()].NetworkID = n.ID()
networkSettings.Networks[n.Name()].EndpointID = ep.ID()
networkSettings.Networks[nwName].NetworkID = n.ID()
networkSettings.Networks[nwName].EndpointID = ep.ID()

iface := epInfo.Iface()
if iface == nil {
return nil
}

if iface.MacAddress() != nil {
networkSettings.Networks[n.Name()].MacAddress = iface.MacAddress().String()
networkSettings.Networks[nwName].MacAddress = iface.MacAddress().String()
}

if iface.Address() != nil {
ones, _ := iface.Address().Mask.Size()
networkSettings.Networks[n.Name()].IPAddress = iface.Address().IP.String()
networkSettings.Networks[n.Name()].IPPrefixLen = ones
networkSettings.Networks[nwName].IPAddress = iface.Address().IP.String()
networkSettings.Networks[nwName].IPPrefixLen = ones
}

if iface.AddressIPv6() != nil && iface.AddressIPv6().IP.To16() != nil {
onesv6, _ := iface.AddressIPv6().Mask.Size()
networkSettings.Networks[n.Name()].GlobalIPv6Address = iface.AddressIPv6().IP.String()
networkSettings.Networks[n.Name()].GlobalIPv6PrefixLen = onesv6
networkSettings.Networks[nwName].GlobalIPv6Address = iface.AddressIPv6().IP.String()
networkSettings.Networks[nwName].GlobalIPv6PrefixLen = onesv6
}

return nil
Expand Down

0 comments on commit b32c759

Please sign in to comment.