mirror of
https://github.com/weaveworks/scope.git
synced 2026-08-18 03:46:45 +00:00
Report docker container labels and render them in the details pane
This commit is contained in:
@@ -27,6 +27,7 @@ const (
|
||||
ContainerPorts = "docker_container_ports"
|
||||
ContainerCreated = "docker_container_created"
|
||||
ContainerIPs = "docker_container_ips"
|
||||
ContainerLabels = "docker_container_labels"
|
||||
|
||||
NetworkRxDropped = "network_rx_dropped"
|
||||
NetworkRxBytes = "network_rx_bytes"
|
||||
@@ -206,6 +207,7 @@ func (c *container) GetNodeMetadata() report.NodeMetadata {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
labels, _ := json.Marshal(c.container.Config.Labels)
|
||||
result := report.MakeNodeMetadataWith(map[string]string{
|
||||
ContainerID: c.ID(),
|
||||
ContainerName: strings.TrimPrefix(c.container.Name, "/"),
|
||||
@@ -215,6 +217,7 @@ func (c *container) GetNodeMetadata() report.NodeMetadata {
|
||||
ImageID: c.container.Image,
|
||||
ContainerIPs: strings.Join(append(c.container.NetworkSettings.SecondaryIPAddresses,
|
||||
c.container.NetworkSettings.IPAddress), " "),
|
||||
ContainerLabels: string(labels),
|
||||
})
|
||||
|
||||
if c.latestStats == nil {
|
||||
@@ -245,6 +248,13 @@ func (c *container) GetNodeMetadata() report.NodeMetadata {
|
||||
return result
|
||||
}
|
||||
|
||||
// ExtractContainerLabels returns the list of Docker container labels given a NodeMetadata from the Container topology.
|
||||
func ExtractContainerLabels(nmd report.NodeMetadata) map[string]string {
|
||||
result := make(map[string]string)
|
||||
json.Unmarshal([]byte(nmd.Metadata[ContainerLabels]), &result)
|
||||
return result
|
||||
}
|
||||
|
||||
// ExtractContainerIPs returns the list of container IPs given a NodeMetadata from the Container topology.
|
||||
func ExtractContainerIPs(nmd report.NodeMetadata) []string {
|
||||
return strings.Fields(nmd.Metadata[ContainerIPs])
|
||||
|
||||
@@ -106,12 +106,24 @@ var (
|
||||
NetworkSettings: &client.NetworkSettings{
|
||||
IPAddress: "1.2.3.4",
|
||||
},
|
||||
Config: &client.Config{
|
||||
Labels: map[string]string{
|
||||
"foo1": "bar1",
|
||||
"foo2": "bar2",
|
||||
},
|
||||
},
|
||||
}
|
||||
container2 = &client.Container{
|
||||
ID: "wiff",
|
||||
Name: "waff",
|
||||
Image: "baz",
|
||||
State: client.State{Pid: 1, Running: true},
|
||||
Config: &client.Config{
|
||||
Labels: map[string]string{
|
||||
"foo1": "bar1",
|
||||
"foo2": "bar2",
|
||||
},
|
||||
},
|
||||
}
|
||||
apiContainer1 = client.APIContainers{ID: "ping"}
|
||||
apiImage1 = client.APIImages{ID: "baz", RepoTags: []string{"bang", "not-chosen"}}
|
||||
|
||||
@@ -313,6 +313,18 @@ func containerOriginTable(nmd report.NodeMetadata, addHostTag bool) (Table, bool
|
||||
rows = append(rows, Row{Key: "IP Address", ValueMajor: ip, ValueMinor: ""})
|
||||
}
|
||||
|
||||
// Add labels in alphabetical order
|
||||
labels := docker.ExtractContainerLabels(nmd)
|
||||
labelKeys := make([]string, 0, len(labels))
|
||||
for k := range labels {
|
||||
labelKeys = append(labelKeys, k)
|
||||
}
|
||||
sort.Strings(labelKeys)
|
||||
for _, key := range labelKeys {
|
||||
rows = append(rows, Row{Key: fmt.Sprintf("Label %q", key), ValueMajor: labels[key]})
|
||||
|
||||
}
|
||||
|
||||
if val, ok := nmd.Metadata[docker.MemoryUsage]; ok {
|
||||
memory, err := strconv.ParseFloat(val, 64)
|
||||
if err == nil {
|
||||
|
||||
@@ -58,6 +58,8 @@ func TestOriginTable(t *testing.T) {
|
||||
{"Host", test.ServerHostID, "", false},
|
||||
{"ID", test.ServerContainerID, "", false},
|
||||
{"Image ID", test.ServerContainerImageID, "", false},
|
||||
{`Label "foo1"`, `bar1`, "", false},
|
||||
{`Label "foo2"`, `bar2`, "", false},
|
||||
},
|
||||
},
|
||||
} {
|
||||
@@ -146,6 +148,8 @@ func TestMakeDetailedContainerNode(t *testing.T) {
|
||||
Rows: []render.Row{
|
||||
{"ID", test.ServerContainerID, "", false},
|
||||
{"Image ID", test.ServerContainerImageID, "", false},
|
||||
{`Label "foo1"`, `bar1`, "", false},
|
||||
{`Label "foo2"`, `bar2`, "", false},
|
||||
},
|
||||
},
|
||||
{
|
||||
|
||||
@@ -174,10 +174,11 @@ var (
|
||||
report.HostNodeID: ClientHostNodeID,
|
||||
}),
|
||||
ServerContainerNodeID: report.MakeNodeMetadataWith(map[string]string{
|
||||
docker.ContainerID: ServerContainerID,
|
||||
docker.ContainerName: "server",
|
||||
docker.ImageID: ServerContainerImageID,
|
||||
report.HostNodeID: ServerHostNodeID,
|
||||
docker.ContainerID: ServerContainerID,
|
||||
docker.ContainerName: "server",
|
||||
docker.ImageID: ServerContainerImageID,
|
||||
report.HostNodeID: ServerHostNodeID,
|
||||
docker.ContainerLabels: `{"foo1": "bar1", "foo2": "bar2"}`,
|
||||
}),
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user