Skip to content

Commit

Permalink
set container IPs to host IPs for containers with --net=host
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbellamy committed Feb 17, 2016
1 parent 092342c commit df856d7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
4 changes: 4 additions & 0 deletions 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 @@ -355,6 +358,7 @@ func (c *container) GetNode(hostID string, localAddrs []net.IP) report.Node {
result = result.WithLatests(map[string]string{
ContainerUptime: uptime.String(),
ContainerRestartCount: strconv.Itoa(c.container.RestartCount),
ContainerNetworkMode: c.container.HostConfig.NetworkMode,
})
result = result.WithControls(
RestartContainer, StopContainer, PauseContainer, AttachContainer, ExecContainer,
Expand Down
54 changes: 53 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,56 @@ 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
}

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()
for _, cidr := range hostNetworks {
if ip, _, err := net.ParseCIDR(cidr); err == nil {
hostIPs = hostIPs.Add(ip.String())
}
}

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

return containers
}

var ContainerWithHostIPsRenderer = containerWithHostIPsRenderer{ContainerRenderer}

type containerWithImageNameRenderer struct {
Renderer
}
Expand Down Expand Up @@ -149,7 +201,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

0 comments on commit df856d7

Please sign in to comment.