Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When k8s present, allow filtering of containers by namespace (take 2) #2362

Merged
merged 3 commits into from
Mar 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions app/api_topologies.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,35 @@ func updateFilters(rpt report.Report, topologies []APITopologyDesc) []APITopolog
}
}
}
var ns []string
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)
}
sort.Strings(ns)
topologies = append([]APITopologyDesc{}, topologies...) // Make a copy so we can make changes safely
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 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(append([]APITopologyOptionGroup{}, t.Options...), options...)
newSubTopologies := make([]APITopologyDesc, len(t.SubTopologies))
for i, sub := range t.SubTopologies {
t.SubTopologies[i] = updateTopologyFilters(sub, options)
newSubTopologies[i] = mergeTopologyFilters(sub, options)
}
t.SubTopologies = newSubTopologies
return t
}

Expand Down Expand Up @@ -200,27 +208,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
15 changes: 13 additions & 2 deletions render/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
"github.com/weaveworks/scope/report"
)

const (
k8sNamespaceLabel = "io.kubernetes.pod.namespace"
)

// PreciousNodeRenderer ensures a node is never filtered out by decorators
type PreciousNodeRenderer struct {
PreciousNodeID string
Expand Down Expand Up @@ -281,7 +285,7 @@ func IsApplication(n report.Node) bool {
if roleLabel == "system" {
return false
}
namespace, _ := n.Latest.Lookup(docker.LabelPrefix + "io.kubernetes.pod.namespace")
namespace, _ := n.Latest.Lookup(docker.LabelPrefix + k8sNamespaceLabel)
if namespace == "kube-system" {
return false
}
Expand Down Expand Up @@ -320,7 +324,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)
tryKeys := []string{kubernetes.Namespace, docker.LabelPrefix + k8sNamespaceLabel}
gotNamespace := ""
for _, key := range tryKeys {
if value, ok := n.Latest.Lookup(key); ok {
gotNamespace = value
break
}
}
return namespace == gotNamespace
}
}
Expand Down