Skip to content

Commit

Permalink
Review Feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbellamy committed Feb 19, 2016
1 parent df856d7 commit 5535f5e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
6 changes: 5 additions & 1 deletion probe/docker/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,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: c.container.HostConfig.NetworkMode,
ContainerNetworkMode: networkMode,
})
result = result.WithControls(
RestartContainer, StopContainer, PauseContainer, AttachContainer, ExecContainer,
Expand Down
19 changes: 6 additions & 13 deletions render/topologies.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,35 +137,28 @@ func (r containerWithHostIPsRenderer) Render(rpt report.Report) RenderableNodes
continue
}

ips, ok := c.Node.Sets.Lookup(docker.ContainerIPs)
if ok && len(ips) > 0 {
continue
}

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

hostNetworks, ok := h.Sets.Lookup(host.LocalNetworks)
if !ok {
continue
}

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

c.Sets = c.Sets.Add(docker.ContainerIPs, hostIPs)
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 {
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 5535f5e

Please sign in to comment.