Skip to content

Commit

Permalink
Merge pull request #1422 from weaveworks/1364-kuber-kuber-kuber-kuber…
Browse files Browse the repository at this point in the history
…-kuber-kuber-kuber-kuber-netes

Add a condition to rendering any kubernetes nodes at all
  • Loading branch information
paulbellamy committed May 4, 2016
2 parents 26b3c17 + 57618be commit d8e8365
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
16 changes: 10 additions & 6 deletions render/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ const (
UnmanagedMajor = "Unmanaged"
)

func renderKubernetesTopologies(rpt report.Report) bool {
return len(rpt.Pod.Nodes)+len(rpt.Service.Nodes) > 1
}

// PodRenderer is a Renderer which produces a renderable kubernetes
// graph by merging the container graph and the pods topology.
var PodRenderer = ApplyDecorators(
MakeFilter(
var PodRenderer = ConditionalRenderer(renderKubernetesTopologies,
ApplyDecorators(MakeFilter(
func(n report.Node) bool {
state, ok := n.Latest.Lookup(kubernetes.PodState)
return (!ok || state != kubernetes.StateDeleted)
Expand All @@ -36,21 +40,21 @@ var PodRenderer = ApplyDecorators(
),
SelectPod,
),
),
)),
)

// PodServiceRenderer is a Renderer which produces a renderable kubernetes services
// graph by merging the pods graph and the services topology.
var PodServiceRenderer = ApplyDecorators(
FilterEmpty(report.Pod,
var PodServiceRenderer = ConditionalRenderer(renderKubernetesTopologies,
ApplyDecorators(FilterEmpty(report.Pod,
MakeReduce(
MakeMap(
MapPod2Service,
PodRenderer,
),
SelectService,
),
),
)),
)

// MapContainer2Pod maps container Nodes to pod
Expand Down
27 changes: 27 additions & 0 deletions render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,30 @@ func propagateLatest(key string, from, to report.Node) report.Node {
}
return to
}

// Condition is a predecate over the entire report that can evaluate to true or false.
type Condition func(report.Report) bool

type conditionalRenderer struct {
Condition
Renderer
}

// ConditionalRenderer renders nothing if the condition is false, otherwise it defers
// to the wrapped Renderer.
func ConditionalRenderer(c Condition, r Renderer) Renderer {
return Memoise(conditionalRenderer{c, r})
}

func (cr conditionalRenderer) Render(rpt report.Report, dct Decorator) report.Nodes {
if cr.Condition(rpt) {
return cr.Renderer.Render(rpt, dct)
}
return report.Nodes{}
}
func (cr conditionalRenderer) Stats(rpt report.Report, dct Decorator) Stats {
if cr.Condition(rpt) {
return cr.Renderer.Stats(rpt, dct)
}
return Stats{}
}

0 comments on commit d8e8365

Please sign in to comment.