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 e81cdb1
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 10 deletions.
36 changes: 36 additions & 0 deletions render/container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package render

import (
"github.com/weaveworks/scope/probe/docker"
"github.com/weaveworks/scope/report"
)

// AmazonECSContainerNameLabel is exported for testing
const AmazonECSContainerNameLabel = "com.amazonaws.ecs.container-name"

// GetRenderableContainerName obtains a user-friendly container name, to render in the UI
func GetRenderableContainerName(nmd report.Node) (string, bool) {
// Amazon's ecs-agent uses huge Docker container names, destructively
// derived from mangling the Container Definition name in Task
// Definitions.
//
// taskFamiliyName-Version-TransformedContainerName-UUID
//
// For instance, a Container Definition name "http-server" declared in
// Task Definition "task-example" with version 5 will lead to a container name
// "task-example-5-httpserver-aceb93e2f2b797caba01"
//
// Note how the dash in http-server is removed not allowing to recover the original
// Container Definition name.
//
// Luckily, the ecs-agent provides label
// "com.amazonaws.ecs.container-name" containing the original Container
// Definition name, which we will use for rendering.
//
if labelValue, ok := nmd.Metadata[docker.LabelPrefix+AmazonECSContainerNameLabel]; ok {
return labelValue, true
}

name, ok := nmd.Metadata[docker.ContainerName]
return name, ok
}
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
6 changes: 3 additions & 3 deletions render/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,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 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 e81cdb1

Please sign in to comment.