package container import ( "context" "fmt" "net/netip" "github.com/moby/moby/client" ) // NetworkName is the Docker network managed worker containers join. func (p *Pool) NetworkName() string { return p.network } // HostGateway is the host-side IPv4 gateway of NetworkName. It is non-empty // only when Config.RequireHostGateway asked for it. func (p *Pool) HostGateway() string { return p.hostGateway } func resolveHostGateway( ctx context.Context, cli *client.Client, networkName string, required bool, ) (string, error) { if !required { return "", nil } result, err := cli.NetworkInspect(ctx, networkName, client.NetworkInspectOptions{}) if err != nil { return "", fmt.Errorf("inspect Docker network %q for task containers: %w", networkName, err) } network := result.Network.Network if network.Driver != "bridge" { return "", fmt.Errorf("docker network %q uses driver %q, want bridge for task containers", networkName, network.Driver) } if network.Scope != "" && network.Scope != "local" { return "", fmt.Errorf("docker network %q has scope %q, want local for task containers", networkName, network.Scope) } for _, ipam := range network.IPAM.Config { gateway := ipam.Gateway if gateway.IsValid() && gateway.Is4() && !gateway.IsUnspecified() { return gateway.String(), nil } } return "", fmt.Errorf("docker network %q has no usable IPv4 host gateway for task containers", networkName) } // ContainerAddrs is every address container id holds on its networks. func (p *Pool) ContainerAddrs(ctx context.Context, id string) ([]netip.Addr, error) { inspected, err := p.cli.ContainerInspect(ctx, id, client.ContainerInspectOptions{}) if err != nil { return nil, fmt.Errorf("inspect container %s: %w", id, err) } var addrs []netip.Addr if settings := inspected.Container.NetworkSettings; settings != nil { for _, endpoint := range settings.Networks { if endpoint != nil && endpoint.IPAddress.IsValid() { addrs = append(addrs, endpoint.IPAddress) } } } return addrs, nil }