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: empty adresses with docker internal networks #579

Merged
merged 2 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
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
50 changes: 50 additions & 0 deletions internal/context/address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package context

import (
docker "github.com/fsouza/go-dockerclient"
)

type Address struct {
IP string
IP6LinkLocal string
IP6Global string
Port string
HostPort string
Proto string
HostIP string
}

func renderAddress(container *docker.Container, port docker.Port) Address {
return Address{
IP: container.NetworkSettings.IPAddress,
IP6LinkLocal: container.NetworkSettings.LinkLocalIPv6Address,
IP6Global: container.NetworkSettings.GlobalIPv6Address,
Port: port.Port(),
Proto: port.Proto(),
}
}

func GetContainerAddresses(container *docker.Container) []Address {
addresses := []Address{}

for port, bindings := range container.NetworkSettings.Ports {
address := renderAddress(container, port)

if len(bindings) > 0 {
address.HostPort = bindings[0].HostPort
address.HostIP = bindings[0].HostIP
}

addresses = append(addresses, address)
}

if len(addresses) == 0 {
// internal docker network has empty 'container.NetworkSettings.Ports'
for port := range container.Config.ExposedPorts {
address := renderAddress(container, port)
addresses = append(addresses, address)
}
}

return addresses
}
116 changes: 116 additions & 0 deletions internal/context/address_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package context

import (
"testing"

docker "github.com/fsouza/go-dockerclient"
"github.com/stretchr/testify/assert"
)

type FakePortBinding struct{}

var httpPort = docker.Port("80/tcp")
var httpPortBinding = docker.PortBinding{
HostIP: "100.100.100.100",
HostPort: "8080",
}

var httpsPort = docker.Port("443/tcp")

var httpTestPort = docker.Port("8080/tcp")
var httpsTestPort = docker.Port("8443/tcp")

func TestGenerateContainerAddresses(t *testing.T) {
testContainer := &docker.Container{
Config: &docker.Config{
ExposedPorts: map[docker.Port]struct{}{},
},
NetworkSettings: &docker.NetworkSettings{
IPAddress: "10.0.0.10",
LinkLocalIPv6Address: "24",
GlobalIPv6Address: "10.0.0.1",
Ports: map[docker.Port][]docker.PortBinding{},
},
}
testContainer.NetworkSettings.Ports[httpPort] = []docker.PortBinding{httpPortBinding}
testContainer.NetworkSettings.Ports[httpsPort] = []docker.PortBinding{}

addresses := GetContainerAddresses(testContainer)
assert.Len(t, addresses, len(testContainer.NetworkSettings.Ports))
assert.Contains(t, addresses, Address{
IP: "10.0.0.10",
IP6LinkLocal: "24",
IP6Global: "10.0.0.1",
Port: "80",
Proto: "tcp",
HostIP: "100.100.100.100",
HostPort: "8080",
})
assert.Contains(t, addresses, Address{
IP: "10.0.0.10",
IP6LinkLocal: "24",
IP6Global: "10.0.0.1",
Port: "443",
Proto: "tcp",
HostIP: "",
HostPort: "",
})
}

func TestGenerateContainerAddressesWithExposedPorts(t *testing.T) {
testContainer := &docker.Container{
Config: &docker.Config{
ExposedPorts: map[docker.Port]struct{}{},
},
NetworkSettings: &docker.NetworkSettings{
IPAddress: "10.0.0.10",
LinkLocalIPv6Address: "24",
GlobalIPv6Address: "10.0.0.1",
Ports: map[docker.Port][]docker.PortBinding{},
},
}
testContainer.NetworkSettings.Ports[httpPort] = []docker.PortBinding{}
testContainer.NetworkSettings.Ports[httpsPort] = []docker.PortBinding{}
testContainer.Config.ExposedPorts[httpPort] = struct{}{}
testContainer.Config.ExposedPorts[httpsPort] = struct{}{}
testContainer.Config.ExposedPorts[httpTestPort] = struct{}{}

assert.Len(t, GetContainerAddresses(testContainer), 2)
}

func TestGenerateContainerAddressesWithNoPorts(t *testing.T) {
testContainer := &docker.Container{
Config: &docker.Config{
ExposedPorts: map[docker.Port]struct{}{},
},
NetworkSettings: &docker.NetworkSettings{
IPAddress: "10.0.0.10",
LinkLocalIPv6Address: "24",
GlobalIPv6Address: "10.0.0.1",
Ports: map[docker.Port][]docker.PortBinding{},
},
}
testContainer.Config.ExposedPorts[httpTestPort] = FakePortBinding{}
testContainer.Config.ExposedPorts[httpsTestPort] = FakePortBinding{}

addresses := GetContainerAddresses(testContainer)
assert.Len(t, addresses, len(testContainer.Config.ExposedPorts))
assert.Contains(t, addresses, Address{
IP: "10.0.0.10",
IP6LinkLocal: "24",
IP6Global: "10.0.0.1",
Port: "8080",
Proto: "tcp",
HostIP: "",
HostPort: "",
})
assert.Contains(t, addresses, Address{
IP: "10.0.0.10",
IP6LinkLocal: "24",
IP6Global: "10.0.0.1",
Port: "8443",
Proto: "tcp",
HostIP: "",
HostPort: "",
})
}
10 changes: 0 additions & 10 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,6 @@ func SetDockerEnv(d *docker.Env) {
dockerEnv = d
}

type Address struct {
IP string
IP6LinkLocal string
IP6Global string
Port string
HostPort string
Proto string
HostIP string
}

type Network struct {
IP string
Name string
Expand Down
19 changes: 4 additions & 15 deletions internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,22 +420,10 @@ func (g *generator) getContainers() ([]*context.RuntimeContainer, error) {
IP6LinkLocal: container.NetworkSettings.LinkLocalIPv6Address,
IP6Global: container.NetworkSettings.GlobalIPv6Address,
}
for k, v := range container.NetworkSettings.Ports {
address := context.Address{
IP: container.NetworkSettings.IPAddress,
IP6LinkLocal: container.NetworkSettings.LinkLocalIPv6Address,
IP6Global: container.NetworkSettings.GlobalIPv6Address,
Port: k.Port(),
Proto: k.Proto(),
}
if len(v) > 0 {
address.HostPort = v[0].HostPort
address.HostIP = v[0].HostIP
}
runtimeContainer.Addresses = append(runtimeContainer.Addresses,
address)

}
adresses := context.GetContainerAddresses(container)
runtimeContainer.Addresses = append(runtimeContainer.Addresses, adresses...)

for k, v := range container.NetworkSettings.Networks {
network := context.Network{
IP: v.IPAddress,
Expand All @@ -453,6 +441,7 @@ func (g *generator) getContainers() ([]*context.RuntimeContainer, error) {
runtimeContainer.Networks = append(runtimeContainer.Networks,
network)
}

for k, v := range container.Volumes {
runtimeContainer.Volumes[k] = context.Volume{
Path: k,
Expand Down