Skip to content

Commit

Permalink
Merge pull request #960 from weaveworks/no-ips-for-no-network
Browse files Browse the repository at this point in the history
Don't show blank IPs metadata row for containers with no IP
  • Loading branch information
paulbellamy committed Feb 19, 2016
2 parents d744278 + 5535f5e commit d2bf204
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
13 changes: 12 additions & 1 deletion probe/docker/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
ContainerState = "docker_container_state"
ContainerUptime = "docker_container_uptime"
ContainerRestartCount = "docker_container_restart_count"
ContainerNetworkMode = "docker_container_network_mode"

NetworkRxDropped = "network_rx_dropped"
NetworkRxBytes = "network_rx_bytes"
Expand All @@ -58,6 +59,8 @@ const (
StateStopped = "stopped"
StatePaused = "paused"

NetworkModeHost = "host"

stopTimeout = 10
)

Expand Down Expand Up @@ -320,7 +323,10 @@ func (c *container) GetNode(hostID string, localAddrs []net.IP) report.Node {
c.RLock()
defer c.RUnlock()

ips := append(c.container.NetworkSettings.SecondaryIPAddresses, c.container.NetworkSettings.IPAddress)
ips := c.container.NetworkSettings.SecondaryIPAddresses
if c.container.NetworkSettings.IPAddress != "" {
ips = append(ips, c.container.NetworkSettings.IPAddress)
}
// Treat all Docker IPs as local scoped.
ipsWithScopes := []string{}
for _, ip := range ips {
Expand Down Expand Up @@ -351,9 +357,14 @@ func (c *container) GetNode(hostID string, localAddrs []net.IP) report.Node {
result = result.WithControls(UnpauseContainer)
} else if c.container.State.Running {
uptime := (mtime.Now().Sub(c.container.State.StartedAt) / time.Second) * time.Second
networkMode := ""
if c.container.HostConfig != nil {
networkMode = c.container.HostConfig.NetworkMode
}
result = result.WithLatests(map[string]string{
ContainerUptime: uptime.String(),
ContainerRestartCount: strconv.Itoa(c.container.RestartCount),
ContainerNetworkMode: networkMode,
})
result = result.WithControls(
RestartContainer, StopContainer, PauseContainer, AttachContainer, ExecContainer,
Expand Down
47 changes: 46 additions & 1 deletion render/topologies.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package render

import (
"fmt"
"net"

"github.com/weaveworks/scope/probe/docker"
"github.com/weaveworks/scope/probe/host"
"github.com/weaveworks/scope/probe/process"
"github.com/weaveworks/scope/report"
)
Expand Down Expand Up @@ -116,6 +118,49 @@ var ContainerRenderer = MakeReduce(
),
)

type containerWithHostIPsRenderer struct {
Renderer
}

// Render produces a process graph where the ips for host network mode are set
// to the host's IPs.
func (r containerWithHostIPsRenderer) Render(rpt report.Report) RenderableNodes {
containers := r.Renderer.Render(rpt)
hosts := MakeMap(
MapHostIdentity,
SelectHost,
).Render(rpt)

for id, c := range containers {
networkMode, ok := c.Node.Latest.Lookup(docker.ContainerNetworkMode)
if !ok || networkMode != docker.NetworkModeHost {
continue
}

h, ok := hosts[MakeHostID(report.ExtractHostID(c.Node))]
if !ok {
continue
}

newIPs := report.MakeStringSet()
hostNetworks, _ := h.Sets.Lookup(host.LocalNetworks)
for _, cidr := range hostNetworks {
if ip, _, err := net.ParseCIDR(cidr); err == nil {
newIPs = newIPs.Add(ip.String())
}
}

c.Sets = c.Sets.Add(docker.ContainerIPs, newIPs)
containers[id] = c
}

return containers
}

// ContainerWithHostIPsRenderer is a Renderer which produces a container graph
// enriched with host IPs on containers where NetworkMode is Host
var ContainerWithHostIPsRenderer = containerWithHostIPsRenderer{ContainerRenderer}

type containerWithImageNameRenderer struct {
Renderer
}
Expand Down Expand Up @@ -149,7 +194,7 @@ func (r containerWithImageNameRenderer) Render(rpt report.Report) RenderableNode

// ContainerWithImageNameRenderer is a Renderer which produces a container
// graph where the ranks are the image names, not their IDs
var ContainerWithImageNameRenderer = containerWithImageNameRenderer{ContainerRenderer}
var ContainerWithImageNameRenderer = containerWithImageNameRenderer{ContainerWithHostIPsRenderer}

// ContainerImageRenderer is a Renderer which produces a renderable container
// image graph by merging the container graph and the container image topology.
Expand Down
20 changes: 20 additions & 0 deletions render/topologies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/weaveworks/scope/probe/kubernetes"
"github.com/weaveworks/scope/render"
"github.com/weaveworks/scope/render/expected"
"github.com/weaveworks/scope/report"
"github.com/weaveworks/scope/test"
"github.com/weaveworks/scope/test/fixture"
)
Expand Down Expand Up @@ -51,6 +52,25 @@ func TestContainerFilterRenderer(t *testing.T) {
}
}

func TestContainerWithHostIPsRenderer(t *testing.T) {
input := fixture.Report.Copy()
input.Container.Nodes[fixture.ClientContainerNodeID] = input.Container.Nodes[fixture.ClientContainerNodeID].WithLatests(map[string]string{
docker.ContainerNetworkMode: "host",
})
nodes := render.ContainerWithHostIPsRenderer.Render(input)

// Test host network nodes get the host IPs added.
haveNode, ok := nodes[render.MakeContainerID(fixture.ClientContainerID)]
if !ok {
t.Fatal("Expected output to have the client container node")
}
have, _ := haveNode.Sets.Lookup(docker.ContainerIPs)
want := report.MakeStringSet("10.10.10.0")
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
}
}

func TestContainerFilterRendererImageName(t *testing.T) {
// Test nodes are filtered by image name as well.
input := fixture.Report.Copy()
Expand Down

0 comments on commit d2bf204

Please sign in to comment.