mirror of
https://github.com/weaveworks/scope.git
synced 2026-07-28 17:51:21 +00:00
Merge pull request #400 from weaveworks/docker-labels
Report docker container labels and render them in the details pane
This commit is contained in:
@@ -216,6 +216,7 @@ func (c *container) GetNodeMetadata() report.NodeMetadata {
|
||||
ContainerIPs: strings.Join(append(c.container.NetworkSettings.SecondaryIPAddresses,
|
||||
c.container.NetworkSettings.IPAddress), " "),
|
||||
})
|
||||
AddLabels(result, c.container.Config.Labels)
|
||||
|
||||
if c.latestStats == nil {
|
||||
return result
|
||||
|
||||
31
probe/docker/labels.go
Normal file
31
probe/docker/labels.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/weaveworks/scope/report"
|
||||
)
|
||||
|
||||
// LabelPrefix is the key prefix used for Docker labels in NodeMetadata (e.g. a
|
||||
// Docker label "labelKey"="labelValue" will get encoded as
|
||||
// "docker_label_labelKey"="dockerValue" in the metadata)
|
||||
const LabelPrefix = "docker_label_"
|
||||
|
||||
// AddLabels appends Docker labels to the NodeMetadata from a topology.
|
||||
func AddLabels(nmd report.NodeMetadata, labels map[string]string) {
|
||||
for key, value := range labels {
|
||||
nmd.Metadata[LabelPrefix+key] = value
|
||||
}
|
||||
}
|
||||
|
||||
// ExtractLabels returns the list of Docker labels given a NodeMetadata from a topology.
|
||||
func ExtractLabels(nmd report.NodeMetadata) map[string]string {
|
||||
result := map[string]string{}
|
||||
for key, value := range nmd.Metadata {
|
||||
if strings.HasPrefix(key, LabelPrefix) {
|
||||
label := key[len(LabelPrefix):]
|
||||
result[label] = value
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
25
probe/docker/labels_test.go
Normal file
25
probe/docker/labels_test.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package docker_test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/weaveworks/scope/probe/docker"
|
||||
"github.com/weaveworks/scope/report"
|
||||
"github.com/weaveworks/scope/test"
|
||||
)
|
||||
|
||||
func TestLabels(t *testing.T) {
|
||||
want := map[string]string{
|
||||
"foo1": "bar1",
|
||||
"foo2": "bar2",
|
||||
}
|
||||
nmd := report.MakeNodeMetadata()
|
||||
|
||||
docker.AddLabels(nmd, want)
|
||||
have := docker.ExtractLabels(nmd)
|
||||
|
||||
if !reflect.DeepEqual(want, have) {
|
||||
t.Error(test.Diff(want, have))
|
||||
}
|
||||
}
|
||||
@@ -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"}}
|
||||
|
||||
@@ -52,6 +52,7 @@ func (r *Reporter) containerImageTopology() report.Topology {
|
||||
nmd := report.MakeNodeMetadataWith(map[string]string{
|
||||
ImageID: image.ID,
|
||||
})
|
||||
AddLabels(nmd, image.Labels)
|
||||
|
||||
if len(image.RepoTags) > 0 {
|
||||
nmd.Metadata[ImageName] = image.RepoTags[0]
|
||||
|
||||
Reference in New Issue
Block a user