Skip to content

Commit

Permalink
got filtering services by namespace working
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbellamy committed Apr 29, 2016
1 parent b4cb02a commit 603aa7b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 35 deletions.
2 changes: 1 addition & 1 deletion app/api_topologies.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func updateFilters(rpt report.Report, topologies []APITopologyDesc) []APITopolog
}
sort.Strings(ns)
for i, t := range topologies {
if t.id == "pods" {
if t.id == "pods" || t.id == "pods-by-service" {
topologies[i] = updateTopologyFilters(t, []APITopologyOptionGroup{kubernetesFilters(ns...)})
}
}
Expand Down
35 changes: 17 additions & 18 deletions probe/kubernetes/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Pod interface {
ID() string
Name() string
Namespace() string
ContainerIDs() []string
ContainerIDs() report.StringSet
Created() string
AddServiceID(id string)
Labels() labels.Labels
Expand All @@ -38,13 +38,13 @@ type Pod interface {

type pod struct {
*api.Pod
serviceIDs []string
serviceIDs report.StringSet
Node *api.Node
}

// NewPod creates a new Pod
func NewPod(p *api.Pod) Pod {
return &pod{Pod: p}
return &pod{Pod: p, serviceIDs: report.MakeStringSet()}
}

func (p *pod) ID() string {
Expand All @@ -63,10 +63,10 @@ func (p *pod) Created() string {
return p.ObjectMeta.CreationTimestamp.Format(time.RFC822)
}

func (p *pod) ContainerIDs() []string {
ids := []string{}
func (p *pod) ContainerIDs() report.StringSet {
ids := report.MakeStringSet()
for _, container := range p.Status.ContainerStatuses {
ids = append(ids, strings.TrimPrefix(container.ContainerID, "docker://"))
ids = ids.Add(strings.TrimPrefix(container.ContainerID, "docker://"))
}
return ids
}
Expand All @@ -76,7 +76,7 @@ func (p *pod) Labels() labels.Labels {
}

func (p *pod) AddServiceID(id string) {
p.serviceIDs = append(p.serviceIDs, id)
p.serviceIDs = p.serviceIDs.Add(id)
}

func (p *pod) State() string {
Expand All @@ -89,18 +89,17 @@ func (p *pod) NodeName() string {

func (p *pod) GetNode(probeID string) report.Node {
n := report.MakeNodeWith(report.MakePodNodeID(p.Namespace(), p.Name()), map[string]string{
PodID: p.ID(),
PodName: p.Name(),
Namespace: p.Namespace(),
PodCreated: p.Created(),
PodContainerIDs: strings.Join(p.ContainerIDs(), " "),
PodState: p.State(),
PodIP: p.Status.PodIP,
PodID: p.ID(),
PodName: p.Name(),
Namespace: p.Namespace(),
PodCreated: p.Created(),
PodState: p.State(),
PodIP: p.Status.PodIP,
report.ControlProbeID: probeID,
})
if len(p.serviceIDs) > 0 {
n = n.WithLatests(map[string]string{ServiceIDs: strings.Join(p.serviceIDs, " ")})
}
}).WithSets(report.EmptySets.
Add(PodContainerIDs, p.ContainerIDs()).
Add(ServiceIDs, p.serviceIDs),
)
for _, serviceID := range p.serviceIDs {
segments := strings.SplitN(serviceID, "/", 2)
if len(segments) != 2 {
Expand Down
15 changes: 1 addition & 14 deletions render/filters.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package render

import (
"fmt"
"strings"

"github.com/weaveworks/scope/common/mtime"
Expand Down Expand Up @@ -279,20 +278,8 @@ func HasChildren(topology string) FilterFunc {
// IsNamespace checks if the node is a pod/service in the specified namespace
func IsNamespace(namespace string) FilterFunc {
return func(n report.Node) bool {
if n.Topology == Pseudo {
return true
}
gotNamespace, ok := n.Latest.Lookup(kubernetes.Namespace)
if !ok {
fmt.Printf("[DEBUG] Missing namespace %s for %s %s\n", namespace, n.Topology, n.ID)
return true
}
if namespace == gotNamespace {
fmt.Printf("[DEBUG] Keeping namespace %s for %s %s\n", gotNamespace, n.Topology, n.ID)
} else {
fmt.Printf("[DEBUG] Droppng namespace %s for %s %s\n", gotNamespace, n.Topology, n.ID)
}
return namespace == gotNamespace
return !ok || namespace == gotNamespace
}
}

Expand Down
4 changes: 2 additions & 2 deletions render/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,13 @@ func MapPod2Service(pod report.Node, _ report.Networks) report.Nodes {
if !ok {
return report.Nodes{}
}
ids, ok := pod.Latest.Lookup(kubernetes.ServiceIDs)
serviceIDs, ok := pod.Sets.Lookup(kubernetes.ServiceIDs)
if !ok {
return report.Nodes{}
}

result := report.Nodes{}
for _, serviceID := range strings.Fields(ids) {
for _, serviceID := range serviceIDs {
serviceName := strings.TrimPrefix(serviceID, namespace+"/")
id := report.MakeServiceNodeID(namespace, serviceName)
node := NewDerivedNode(id, pod).WithTopology(report.Service)
Expand Down

0 comments on commit 603aa7b

Please sign in to comment.