From 9367d95cb0a63a2a2b880e53fcfbc61ab706385e Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Wed, 19 Oct 2016 16:48:20 +0000 Subject: [PATCH] Allow providing fixed entries in tables --- probe/docker/container.go | 4 ++-- probe/docker/reporter.go | 2 +- probe/kubernetes/meta.go | 2 +- report/table.go | 47 ++++++++++++++++++++++++++++++--------- 4 files changed, 40 insertions(+), 15 deletions(-) diff --git a/probe/docker/container.go b/probe/docker/container.go index ca11c3883..a93604431 100644 --- a/probe/docker/container.go +++ b/probe/docker/container.go @@ -379,8 +379,8 @@ func (c *container) getBaseNode() report.Node { }).WithParents(report.EmptySets. Add(report.ContainerImage, report.MakeStringSet(report.MakeContainerImageNodeID(c.Image()))), ) - result = result.AddTable(LabelPrefix, c.container.Config.Labels) - result = result.AddTable(EnvPrefix, c.env()) + result = result.AddPrefixTable(LabelPrefix, c.container.Config.Labels) + result = result.AddPrefixTable(EnvPrefix, c.env()) return result } diff --git a/probe/docker/reporter.go b/probe/docker/reporter.go index 48971061c..fd0ef6cf4 100644 --- a/probe/docker/reporter.go +++ b/probe/docker/reporter.go @@ -240,7 +240,7 @@ func (r *Reporter) containerImageTopology() report.Topology { node := report.MakeNodeWith(nodeID, map[string]string{ ImageID: imageID, }) - node = node.AddTable(ImageLabelPrefix, image.Labels) + node = node.AddPrefixTable(ImageLabelPrefix, image.Labels) if len(image.RepoTags) > 0 { node = node.WithLatests(map[string]string{ImageName: image.RepoTags[0]}) diff --git a/probe/kubernetes/meta.go b/probe/kubernetes/meta.go index d8109867e..c7db23d0c 100644 --- a/probe/kubernetes/meta.go +++ b/probe/kubernetes/meta.go @@ -56,5 +56,5 @@ func (m meta) MetaNode(id string) report.Node { Name: m.Name(), Namespace: m.Namespace(), Created: m.Created(), - }).AddTable(LabelPrefix, m.Labels()) + }).AddPrefixTable(LabelPrefix, m.Labels()) } diff --git a/report/table.go b/report/table.go index feaec789d..e77c51ad0 100644 --- a/report/table.go +++ b/report/table.go @@ -17,8 +17,8 @@ const ( TruncationCountPrefix = "table_truncation_count_" ) -// AddTable appends arbirary key-value pairs to the Node, returning a new node. -func (node Node) AddTable(prefix string, labels map[string]string) Node { +// AddPrefixTable appends arbirary key-value pairs to the Node, returning a new node. +func (node Node) AddPrefixTable(prefix string, labels map[string]string) Node { count := 0 for key, value := range labels { if count >= MaxTableRows { @@ -34,17 +34,20 @@ func (node Node) AddTable(prefix string, labels map[string]string) Node { return node } -// ExtractTable returns the key-value pairs with the given prefix from this Node, -func (node Node) ExtractTable(prefix string) (rows map[string]string, truncationCount int) { +// extractTable returns the key-value pairs to build a table from this node +func (node Node) extractTable(template TableTemplate) (rows map[string]string, truncationCount int) { rows = map[string]string{} truncationCount = 0 node.Latest.ForEach(func(key string, _ time.Time, value string) { - if strings.HasPrefix(key, prefix) { - label := key[len(prefix):] + if label, ok := template.FixedRows[key]; ok { + rows[label] = value + } + if strings.HasPrefix(key, template.Prefix) { + label := key[len(template.Prefix):] rows[label] = value } }) - if str, ok := node.Latest.Lookup(TruncationCountPrefix + prefix); ok { + if str, ok := node.Latest.Lookup(TruncationCountPrefix + template.Prefix); ok { if n, err := fmt.Sscanf(str, "%d", &truncationCount); n != 1 || err != nil { log.Warn("Unexpected truncation count format %q", str) } @@ -79,15 +82,31 @@ func (t Table) Copy() Table { return result } +// FixedRow describes a row which is part of a TableTemplate and whose value is extracted +// from a predetermined key +type FixedRow struct { + Key string `json:"key"` + Label string `json:"label"` +} + // TableTemplate describes how to render a table for the UI. type TableTemplate struct { ID string `json:"id"` Label string `json:"label"` Prefix string `json:"prefix"` + // FixedRows indicates what predetermined rows to render each entry is + // indexed by the key to extract the row value is mapped to the row + // label + FixedRows map[string]string `json:"fixedRows"` } // Copy returns a value-copy of the TableTemplate func (t TableTemplate) Copy() TableTemplate { + fixedRowsCopy := make(map[string]string, len(t.FixedRows)) + for key, value := range t.FixedRows { + fixedRowsCopy[key] = value + } + t.FixedRows = fixedRowsCopy return t } @@ -102,10 +121,16 @@ func (t TableTemplate) Merge(other TableTemplate) TableTemplate { return s2 } + fixedRows := t.FixedRows + if len(other.FixedRows) > len(fixedRows) { + fixedRows = other.FixedRows + } + return TableTemplate{ - ID: max(t.ID, other.ID), - Label: max(t.Label, other.Label), - Prefix: max(t.Prefix, other.Prefix), + ID: max(t.ID, other.ID), + Label: max(t.Label, other.Label), + Prefix: max(t.Prefix, other.Prefix), + FixedRows: fixedRows, } } @@ -116,7 +141,7 @@ type TableTemplates map[string]TableTemplate func (t TableTemplates) Tables(node Node) []Table { var result []Table for _, template := range t { - rows, truncationCount := node.ExtractTable(template.Prefix) + rows, truncationCount := node.extractTable(template) table := Table{ ID: template.ID, Label: template.Label,