Use image name without version as id. (#1531)

* Use image name without version as id.

* Review feedback
This commit is contained in:
Tom Wilkie
2016-06-15 16:14:51 +01:00
committed by GitHub
parent d888509865
commit fba555c985
13 changed files with 127 additions and 46 deletions

View File

@@ -43,6 +43,7 @@ type Registry interface {
WatchContainerUpdates(ContainerUpdateWatcher)
GetContainer(string) (Container, bool)
GetContainerByPrefix(string) (Container, bool)
GetContainerImage(string) (*docker_client.APIImages, bool)
}
// ContainerUpdateWatcher is the type of functions that get called when containers are updated.
@@ -390,6 +391,13 @@ func (r *registry) GetContainerByPrefix(prefix string) (Container, bool) {
return nil, false
}
func (r *registry) GetContainerImage(id string) (*docker_client.APIImages, bool) {
r.RLock()
defer r.RUnlock()
image, ok := r.images[id]
return image, ok
}
// WalkImages runs f on every image of running containers the registry
// knows of. f may be run on the same image more than once.
func (r *registry) WalkImages(f func(*docker_client.APIImages)) {

View File

@@ -38,7 +38,7 @@ var (
}
ContainerImageMetadataTemplates = report.MetadataTemplates{
ImageID: {ID: ImageID, Label: "Image ID", From: report.FromLatest, Truncate: 12, Priority: 1},
ImageID: {ID: ImageID, Label: "Image ID", From: report.FromSets, Truncate: 12, Priority: 1},
report.Container: {ID: report.Container, Label: "# Containers", From: report.FromCounters, Datatype: "number", Priority: 2},
}

View File

@@ -40,6 +40,11 @@ func (r *mockRegistry) GetContainer(_ string) (docker.Container, bool) { return
func (r *mockRegistry) GetContainerByPrefix(_ string) (docker.Container, bool) { return nil, false }
func (r *mockRegistry) GetContainerImage(id string) (*client.APIImages, bool) {
image, ok := r.images[id]
return image, ok
}
var (
mockRegistryInstance = &mockRegistry{
containersByPID: map[int]docker.Container{

View File

@@ -82,12 +82,21 @@ func (t *Tagger) tag(tree process.Tree, topology *report.Topology) {
continue
}
topology.AddNode(report.MakeNodeWith(nodeID, map[string]string{
node := report.MakeNodeWith(nodeID, map[string]string{
ContainerID: c.ID(),
}).WithParents(report.EmptySets.
Add(report.Container, report.MakeStringSet(report.MakeContainerNodeID(c.ID()))).
Add(report.ContainerImage, report.MakeStringSet(report.MakeContainerImageNodeID(c.Image()))),
))
Add(report.Container, report.MakeStringSet(report.MakeContainerNodeID(c.ID()))),
)
// If we can work out the image name, add a parent tag for it
image, ok := t.registry.GetContainerImage(c.Image())
if ok && len(image.RepoTags) > 0 {
imageName := ImageNameWithoutVersion(image.RepoTags[0])
node = node.WithParents(report.EmptySets.
Add(report.ContainerImage, report.MakeStringSet(report.MakeContainerImageNodeID(imageName))),
)
}
topology.AddNode(node)
}
}

View File

@@ -70,8 +70,8 @@ func TestTagger(t *testing.T) {
}
// node should have the container image as a parent
if have, ok := node.Parents.Lookup(report.ContainerImage); !ok || !have.Contains(report.MakeContainerImageNodeID("baz")) {
t.Errorf("Expected process node %s to have container image %q as a parent, got %q", nodeID, "baz", have)
if have, ok := node.Parents.Lookup(report.ContainerImage); !ok || !have.Contains(report.MakeContainerImageNodeID("bang")) {
t.Errorf("Expected process node %s to have container image %q as a parent, got %q", nodeID, "bang", have)
}
}
}