mirror of
https://github.com/weaveworks/scope.git
synced 2026-03-03 18:20:27 +00:00
probe/docker: Populate SwarmService topology based on docker labels
This isn't the best way to do it, but it will work well enough for an initial implementation
This commit is contained in:
@@ -21,6 +21,7 @@ const (
|
||||
ImageLabelPrefix = "docker_image_label_"
|
||||
IsInHostNetwork = "docker_is_in_host_network"
|
||||
ImageTableID = "image_table"
|
||||
ServiceName = "service_name"
|
||||
)
|
||||
|
||||
// Exposed for testing
|
||||
@@ -132,6 +133,10 @@ var (
|
||||
Rank: 8,
|
||||
},
|
||||
}
|
||||
|
||||
SwarmServiceMetadataTemplates = report.MetadataTemplates{
|
||||
ServiceName: {ID: ServiceName, Label: "Service Name", From: report.FromLatest, Priority: 0},
|
||||
}
|
||||
)
|
||||
|
||||
// Reporter generate Reports containing Container and ContainerImage topologies
|
||||
@@ -177,6 +182,7 @@ func (r *Reporter) Report() (report.Report, error) {
|
||||
result.Container = result.Container.Merge(r.containerTopology(localAddrs))
|
||||
result.ContainerImage = result.ContainerImage.Merge(r.containerImageTopology())
|
||||
result.Overlay = result.Overlay.Merge(r.overlayTopology())
|
||||
result.SwarmService = result.SwarmService.Merge(r.swarmServiceTopology())
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -298,6 +304,10 @@ func (r *Reporter) overlayTopology() report.Topology {
|
||||
return report.MakeTopology().AddNode(node)
|
||||
}
|
||||
|
||||
func (r *Reporter) swarmServiceTopology() report.Topology {
|
||||
return report.MakeTopology().WithMetadataTemplates(SwarmServiceMetadataTemplates)
|
||||
}
|
||||
|
||||
// Docker sometimes prefixes ids with a "type" annotation, but it renders a bit
|
||||
// ugly and isn't necessary, so we should strip it off
|
||||
func trimImageID(id string) string {
|
||||
|
||||
@@ -2,6 +2,7 @@ package docker
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/weaveworks/scope/probe/process"
|
||||
"github.com/weaveworks/scope/report"
|
||||
@@ -21,6 +22,7 @@ var (
|
||||
|
||||
// Tagger is a tagger that tags Docker container information to process
|
||||
// nodes that have a PID.
|
||||
// It also populates the SwarmService topology if any of the associated docker labels are present.
|
||||
type Tagger struct {
|
||||
registry Registry
|
||||
procWalker process.Walker
|
||||
@@ -44,6 +46,31 @@ func (t *Tagger) Tag(r report.Report) (report.Report, error) {
|
||||
return report.MakeReport(), err
|
||||
}
|
||||
t.tag(tree, &r.Process)
|
||||
|
||||
// Scan for Swarm service info
|
||||
for containerID, container := range r.Container.Nodes {
|
||||
serviceID, ok := container.Latest.Lookup(LabelPrefix + "com.docker.swarm.service.id")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
serviceName, ok := container.Latest.Lookup(LabelPrefix + "com.docker.swarm.service.name")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.HasPrefix(serviceName, "dockerswarm_") {
|
||||
serviceName = serviceName[len("dockerswarm_"):]
|
||||
}
|
||||
|
||||
nodeID := report.MakeSwarmServiceNodeID(serviceID)
|
||||
node := report.MakeNodeWith(nodeID, map[string]string{
|
||||
ServiceName: serviceName,
|
||||
})
|
||||
r.SwarmService = r.SwarmService.AddNode(node)
|
||||
|
||||
r.Container.Nodes[containerID] = container.WithParents(container.Parents.Add(report.SwarmService, report.MakeStringSet(nodeID)))
|
||||
}
|
||||
|
||||
return r, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user