From faf25155a771ec7643ad4ccc9da3c7a1735fe8d0 Mon Sep 17 00:00:00 2001 From: Vishnu Kannan Date: Fri, 18 Sep 2015 11:31:13 -0700 Subject: [PATCH] Add container labels to the list of labels exported by heapster. This is useful for monitoring based on labels set to container images. --- sinks/api/decoder.go | 7 +++++++ sinks/cache/impl.go | 2 ++ 2 files changed, 9 insertions(+) diff --git a/sinks/api/decoder.go b/sinks/api/decoder.go index 27ffbae8df..36ead34b66 100644 --- a/sinks/api/decoder.go +++ b/sinks/api/decoder.go @@ -15,6 +15,7 @@ package v1 import ( + "fmt" "time" "k8s.io/heapster/sinks/cache" @@ -94,6 +95,12 @@ func (self *decoder) getContainerMetrics(container *cache.ContainerElement, labe } labels[LabelContainerName.Key] = container.Name labels[LabelContainerBaseImage.Key] = container.Image + // Add container specific labels along with existing labels. + containerLabels := util.LabelsToString(container.Labels, ",") + if labels[LabelLabels.Key] != "" { + containerLabels = fmt.Sprintf("%s,%s", labels[LabelLabels.Key], containerLabels) + } + labels[LabelLabels.Key] = containerLabels if _, exists := labels[LabelHostID.Key]; !exists { labels[LabelHostID.Key] = container.ExternalID diff --git a/sinks/cache/impl.go b/sinks/cache/impl.go index b93f536f84..93e2b67556 100644 --- a/sinks/cache/impl.go +++ b/sinks/cache/impl.go @@ -198,6 +198,7 @@ func (rc *realCache) StorePods(pods []source_api.Pod) error { ce.Metadata = Metadata{ Name: cont.Name, Hostname: cont.Hostname, + Labels: cont.Spec.Labels, } ce.Image = cont.Image ce.Metadata.LastUpdate = storeSpecAndStats(ce, cont) @@ -240,6 +241,7 @@ func (rc *realCache) StoreContainers(containers []source_api.Container) error { ne.freeContainers[cont.Name] = ce } } + ce.Metadata.Labels = cont.Spec.Labels ce.Image = cont.Image ce.Metadata.LastUpdate = storeSpecAndStats(ce, cont) ce.lastUpdated = now