Skip to content

Commit

Permalink
Use label "com.amazonaws.ecs.container-name" to render container names
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfonso Acosta committed Sep 7, 2015
1 parent e81efe6 commit ebefa5e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion render/detailed_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func containerOriginTable(nmd report.Node, addHostTag bool) (Table, bool) {

var (
title = "Container"
name, nameFound = nmd.Metadata[docker.ContainerName]
name, nameFound = GetRenderableContainerName(nmd)
)
if nameFound {
title += ` "` + name + `"`
Expand Down
2 changes: 2 additions & 0 deletions render/detailed_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func TestOriginTable(t *testing.T) {
{"Host", test.ServerHostID, "", false},
{"ID", test.ServerContainerID, "", false},
{"Image ID", test.ServerContainerImageID, "", false},
{fmt.Sprintf(`Label %q`, render.AmazonECSContainerNameLabel), `server`, "", false},
{`Label "foo1"`, `bar1`, "", false},
{`Label "foo2"`, `bar2`, "", false},
},
Expand Down Expand Up @@ -158,6 +159,7 @@ func TestMakeDetailedContainerNode(t *testing.T) {
Rows: []render.Row{
{"ID", test.ServerContainerID, "", false},
{"Image ID", test.ServerContainerImageID, "", false},
{fmt.Sprintf(`Label %q`, render.AmazonECSContainerNameLabel), `server`, "", false},
{`Label "foo1"`, `bar1`, "", false},
{`Label "foo2"`, `bar2`, "", false},
},
Expand Down
24 changes: 21 additions & 3 deletions render/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const (

containersKey = "containers"
processesKey = "processes"

AmazonECSContainerNameLabel = "com.amazonaws.ecs.container-name"
)

// MapFunc is anything which can take an arbitrary RenderableNode and
Expand Down Expand Up @@ -123,9 +125,9 @@ func MapContainerIdentity(m RenderableNode, _ report.Networks) RenderableNodes {
}

var (
major = m.Metadata[docker.ContainerName]
minor = report.ExtractHostID(m.Node)
rank = m.Metadata[docker.ImageID]
major, _ = GetRenderableContainerName(m.Node)
minor = report.ExtractHostID(m.Node)
rank = m.Metadata[docker.ImageID]
)

node := NewRenderableNodeWith(id, major, minor, rank, m)
Expand All @@ -136,6 +138,22 @@ func MapContainerIdentity(m RenderableNode, _ report.Networks) RenderableNodes {
return RenderableNodes{id: node}
}

// GetRenderableContainerName obtains a user-friendly container name, to render in the UI
func GetRenderableContainerName(nmd report.Node) (string, bool) {
// Amazon's ecs-agent produces huge Docker container names, destructively
// derived from mangling Container Definition names in Task
// Definitions.
//
// However, the ecs-agent provides a label containing the original Container
// Definition name.
if labelValue, ok := nmd.Metadata[docker.LabelPrefix+AmazonECSContainerNameLabel]; ok {
return labelValue, true
}

name, ok := nmd.Metadata[docker.ContainerName]
return name, ok
}

// MapContainerImageIdentity maps a container image topology node to container
// image renderable node. As it is only ever run on container image topology
// nodes, we expect that certain keys are present.
Expand Down
14 changes: 8 additions & 6 deletions test/report_fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/weaveworks/scope/probe/docker"
"github.com/weaveworks/scope/probe/endpoint"
"github.com/weaveworks/scope/probe/process"
"github.com/weaveworks/scope/render"
"github.com/weaveworks/scope/report"
)

Expand Down Expand Up @@ -199,12 +200,13 @@ var (
report.HostNodeID: ClientHostNodeID,
}),
ServerContainerNodeID: report.MakeNodeWith(map[string]string{
docker.ContainerID: ServerContainerID,
docker.ContainerName: "server",
docker.ImageID: ServerContainerImageID,
report.HostNodeID: ServerHostNodeID,
docker.LabelPrefix + "foo1": "bar1",
docker.LabelPrefix + "foo2": "bar2",
docker.ContainerID: ServerContainerID,
docker.ContainerName: "task-name-5-server-aceb93e2f2b797caba01",
docker.ImageID: ServerContainerImageID,
report.HostNodeID: ServerHostNodeID,
docker.LabelPrefix + render.AmazonECSContainerNameLabel: "server",
docker.LabelPrefix + "foo1": "bar1",
docker.LabelPrefix + "foo2": "bar2",
}),
},
},
Expand Down

0 comments on commit ebefa5e

Please sign in to comment.