From 6c906a7cfa6804856c7272a202e0169d560481bf Mon Sep 17 00:00:00 2001 From: Roberto Bruggemann Date: Thu, 14 Dec 2017 11:43:37 +0000 Subject: [PATCH] Extract namespaces from report.Namespace This avoids unnecessary loops. --- app/api_topologies.go | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/app/api_topologies.go b/app/api_topologies.go index 5aaff9d180..78c2ecb3f7 100644 --- a/app/api_topologies.go +++ b/app/api_topologies.go @@ -95,26 +95,14 @@ func updateSwarmFilters(rpt report.Report, topologies []APITopologyDesc) []APITo } func updateKubeFilters(rpt report.Report, topologies []APITopologyDesc) []APITopologyDesc { - namespaces := map[string]struct{}{} - // We exclude ReplicaSets since we don't show them anywhere. - for _, t := range []report.Topology{rpt.Pod, rpt.Service, rpt.Deployment, rpt.DaemonSet, rpt.StatefulSet, rpt.CronJob} { - for _, n := range t.Nodes { - if state, ok := n.Latest.Lookup(kubernetes.State); ok && state == kubernetes.StateDeleted { - continue - } - if namespace, ok := n.Latest.Lookup(kubernetes.Namespace); ok { - namespaces[namespace] = struct{}{} - } - } - } - if len(namespaces) == 0 { - // We only want to apply k8s filters when we have k8s-related nodes, - // so if we don't then return early - return topologies - } ns := []string{} - for namespace := range namespaces { - ns = append(ns, namespace) + for _, n := range rpt.Namespace.Nodes { + name, ok := n.Latest.Lookup(kubernetes.Name) + if !ok { + log.Errorf("Error while fetching the namespace's name: %v", n) + continue + } + ns = append(ns, name) } sort.Strings(ns) topologies = append([]APITopologyDesc{}, topologies...) // Make a copy so we can make changes safely