Skip to content

Commit

Permalink
When k8s present, allow filtering of containers by namespace
Browse files Browse the repository at this point in the history
To facilitate this, we replace the existing functionality of updateFilters which
sets k8s topologies to have the filters [namespace, managed], to instead append the namespace filter
to any existing. This lets it apply to both k8s and container topologies without overwriting existing
container filters. We instead set the managed filter in the static definition.

This however has the side effect that the ordering of the namespace filter and the managed filter
in k8s topologies has been reversed, so it reads:
	Show Unmanaged | Hide Unmanaged
	foo | bar | default | baz | All Namespaces
instead of:
	foo | bar | default | baz | All Namespaces
	Show Unmanaged | Hide Unmanaged
  • Loading branch information
ekimekim committed Feb 28, 2017
1 parent 107a933 commit 92621b4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
25 changes: 16 additions & 9 deletions app/api_topologies.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,24 @@ func updateFilters(rpt report.Report, topologies []APITopologyDesc) []APITopolog
ns = append(ns, namespace)
}
sort.Strings(ns)
for i, t := range topologies {
if t.id == podsID || t.id == servicesID || t.id == deploymentsID || t.id == replicaSetsID {
topologies[i] = updateTopologyFilters(t, []APITopologyOptionGroup{
kubernetesFilters(ns...), unmanagedFilter,
})
if len(ns) > 0 {
// We only want to apply k8s filters if any k8s info is present
for i, t := range topologies {
if t.id == containersID || t.id == containersByImageID || t.id == containersByHostnameID || t.id == podsID || t.id == servicesID || t.id == deploymentsID || t.id == replicaSetsID {
topologies[i] = mergeTopologyFilters(t, []APITopologyOptionGroup{
kubernetesFilters(ns...),
})
}
}
}
return topologies
}

// updateTopologyFilters recursively sets the options on a topology description
func updateTopologyFilters(t APITopologyDesc, options []APITopologyOptionGroup) APITopologyDesc {
t.Options = options
// mergeTopologyFilters recursively merges in new options on a topology description
func mergeTopologyFilters(t APITopologyDesc, options []APITopologyOptionGroup) APITopologyDesc {
t.Options = append(t.Options, options...)
for i, sub := range t.SubTopologies {
t.SubTopologies[i] = updateTopologyFilters(sub, options)
t.SubTopologies[i] = mergeTopologyFilters(sub, options)
}
return t
}
Expand Down Expand Up @@ -200,27 +203,31 @@ func MakeRegistry() *Registry {
renderer: render.PodRenderer,
Name: "Pods",
Rank: 3,
Options: []APITopologyOptionGroup{unmanagedFilter},
HideIfEmpty: true,
},
APITopologyDesc{
id: replicaSetsID,
parent: podsID,
renderer: render.ReplicaSetRenderer,
Name: "replica sets",
Options: []APITopologyOptionGroup{unmanagedFilter},
HideIfEmpty: true,
},
APITopologyDesc{
id: deploymentsID,
parent: podsID,
renderer: render.DeploymentRenderer,
Name: "deployments",
Options: []APITopologyOptionGroup{unmanagedFilter},
HideIfEmpty: true,
},
APITopologyDesc{
id: servicesID,
parent: podsID,
renderer: render.PodServiceRenderer,
Name: "services",
Options: []APITopologyOptionGroup{unmanagedFilter},
HideIfEmpty: true,
},
APITopologyDesc{
Expand Down
9 changes: 8 additions & 1 deletion render/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,14 @@ func IsNotPseudo(n report.Node) bool {
// IsNamespace checks if the node is a pod/service in the specified namespace
func IsNamespace(namespace string) FilterFunc {
return func(n report.Node) bool {
gotNamespace, _ := n.Latest.Lookup(kubernetes.Namespace)
try_keys := []string{kubernetes.Namespace, docker.LabelPrefix + "io.kubernetes.pod.namespace"}
gotNamespace := ""
for _, key := range try_keys {
if value, ok := n.Latest.Lookup(key); ok {
gotNamespace = value
break
}
}
return namespace == gotNamespace
}
}
Expand Down

0 comments on commit 92621b4

Please sign in to comment.