From ebefa5e8f0ba9f67dc18cf8c542c753223c1d067 Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Mon, 7 Sep 2015 16:42:52 +0000 Subject: [PATCH] Use label "com.amazonaws.ecs.container-name" to render container names --- render/detailed_node.go | 2 +- render/detailed_node_test.go | 2 ++ render/mapping.go | 24 +++++++++++++++++++++--- test/report_fixture.go | 14 ++++++++------ 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/render/detailed_node.go b/render/detailed_node.go index 83e013d10f..82dd28267d 100644 --- a/render/detailed_node.go +++ b/render/detailed_node.go @@ -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 + `"` diff --git a/render/detailed_node_test.go b/render/detailed_node_test.go index 5eb00e034d..9aa937627e 100644 --- a/render/detailed_node_test.go +++ b/render/detailed_node_test.go @@ -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}, }, @@ -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}, }, diff --git a/render/mapping.go b/render/mapping.go index 366a34babd..40c9242c35 100644 --- a/render/mapping.go +++ b/render/mapping.go @@ -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 @@ -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) @@ -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. diff --git a/test/report_fixture.go b/test/report_fixture.go index 1dc1eddd12..65cf19e5de 100644 --- a/test/report_fixture.go +++ b/test/report_fixture.go @@ -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" ) @@ -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", }), }, },