Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(docker): network error only for overlays [EE-5650] #553

Merged
merged 1 commit into from
May 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 61 additions & 16 deletions docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/portainer/agent"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/system"
"github.com/docker/docker/client"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -76,35 +77,37 @@ func (service *InfoService) GetContainerIpFromDockerEngine(containerName string,
return "", err
}

if len(containerInspect.NetworkSettings.Networks) > 1 {
networks, err := fetchNetworkInfo(cli, containerInspect.NetworkSettings.Networks)
if err != nil {
return "", err
}

overlayCount := countOverlays(networks)

if overlayCount > 1 {
log.Warn().
Int("network_count", len(containerInspect.NetworkSettings.Networks)).
Msg("agent container running in more than a single Docker network. This might cause communication issues")
Msg("Agent container running in more than one overlay network. This might cause communication issues")
}

for networkName, network := range containerInspect.NetworkSettings.Networks {
networkInspect, err := cli.NetworkInspect(context.Background(), network.NetworkID, types.NetworkInspectOptions{})
if err != nil {
return "", err
}

if networkInspect.Ingress || (ignoreNonSwarmNetworks && networkInspect.Scope != "swarm") {
for _, network := range networks {
if network.resource.Ingress || (ignoreNonSwarmNetworks && network.resource.Scope != "swarm") {
log.Debug().
Str("network_name", networkInspect.Name).
Str("scope", networkInspect.Scope).
Bool("ingress", networkInspect.Ingress).
Str("network_name", network.resource.Name).
Str("scope", network.resource.Scope).
Bool("ingress", network.resource.Ingress).
Msg("skipping invalid container network")

continue
}

if network.IPAddress != "" {
if network.settings.IPAddress != "" {
log.Debug().
Str("ip_address", network.IPAddress).
Str("network_name", networkName).
Str("ip_address", network.settings.IPAddress).
Str("network_name", network.name).
Msg("retrieving IP address from container network")

return network.IPAddress, nil
return network.settings.IPAddress, nil
}
}

Expand Down Expand Up @@ -169,3 +172,45 @@ func withCli(callback func(cli *client.Client) error) error {

return callback(cli)
}

type networkInfo struct {
resource types.NetworkResource
settings *network.EndpointSettings
name string
}

func fetchNetworkInfo(cli *client.Client, networkSettings map[string]*network.EndpointSettings) ([]networkInfo, error) {
networks := []networkInfo{}

for networkName, network := range networkSettings {
networkInspect, err := cli.NetworkInspect(context.Background(), network.NetworkID, types.NetworkInspectOptions{})
if err != nil {
return nil, err
}

networks = append(networks, networkInfo{
resource: networkInspect,
settings: network,
name: networkName,
})

}

return networks, nil
}

func countOverlays(networks []networkInfo) int {
overlayCount := 0

for _, network := range networks {
if network.resource.Driver == "overlay" && !network.resource.Ingress {
log.Debug().
Str("network_name", network.name).
Msg("found overlay network")

overlayCount++
}
}

return overlayCount
}
Loading