Skip to content

Commit

Permalink
report.Upgrade() add deployments to pods as parent
Browse files Browse the repository at this point in the history
This was previously done selectPodsWithDeployments.Render().
  • Loading branch information
Roberto Bruggemann committed Dec 8, 2017
1 parent ea20de5 commit 7618b36
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 28 deletions.
29 changes: 1 addition & 28 deletions render/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ var PodRenderer = Memoise(ConditionalRenderer(renderKubernetesTopologies,
),
),
),
// ConnectionJoin invokes the renderer twice, hence it
// helps to memoise it.
ConnectionJoin(MapPod2IP, Memoise(selectPodsWithDeployments{})),
ConnectionJoin(MapPod2IP, SelectPod),
),
),
))
Expand Down Expand Up @@ -113,31 +111,6 @@ func renderParents(childTopology string, parentTopologies []string, noParentsPse
)...)
}

// Renderer to return modified Pod nodes to elide replica sets and point directly
// to deployments where applicable.
type selectPodsWithDeployments struct{}

func (s selectPodsWithDeployments) Render(rpt report.Report) Nodes {
result := report.Nodes{}
// For each pod, we check for any replica sets, and merge any deployments they point to
// into a replacement Parents value.
for podID, pod := range rpt.Pod.Nodes {
if replicaSetIDs, ok := pod.Parents.Lookup(report.ReplicaSet); ok {
newParents := pod.Parents.Delete(report.ReplicaSet)
for _, replicaSetID := range replicaSetIDs {
if replicaSet, ok := rpt.ReplicaSet.Nodes[replicaSetID]; ok {
if deploymentIDs, ok := replicaSet.Parents.Lookup(report.Deployment); ok {
newParents = newParents.Add(report.Deployment, deploymentIDs)
}
}
}
pod = pod.WithParents(newParents)
}
result[podID] = pod
}
return Nodes{Nodes: result}
}

// MapPod2IP maps pod nodes to their IP address. This allows pods to
// be joined directly with the endpoint topology.
func MapPod2IP(m report.Node) []string {
Expand Down
43 changes: 43 additions & 0 deletions report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ func (r Report) Validate() error {
//
// This for now creates node's LatestControls from Controls.
func (r Report) Upgrade() Report {
return r.upgradeLatestControls().upgradePodNodes()
}

func (r Report) upgradeLatestControls() Report {
needUpgrade := false
r.WalkTopologies(func(topology *Topology) {
for _, node := range topology.Nodes {
Expand All @@ -349,6 +353,7 @@ func (r Report) Upgrade() Report {
}
}
})

if !needUpgrade {
return r
}
Expand All @@ -369,9 +374,47 @@ func (r Report) Upgrade() Report {
}
topology.Nodes = n
})

return cp
}

func (r Report) upgradePodNodes() Report {
needUpgrade := false
for _, pod := range r.Pod.Nodes {
_, hasReplicaSets := pod.Parents.Lookup(ReplicaSet)
// ReplicaSets are not reported since adding Deployments as pods' parent
if hasReplicaSets {
needUpgrade = true
break
}
}

if !needUpgrade {
return r
}

// For each pod, we check for any replica sets, and merge any deployments they point to
// into a replacement Parents value.
nodes := Nodes{}
for podID, pod := range r.Pod.Nodes {
if replicaSetIDs, ok := pod.Parents.Lookup(ReplicaSet); ok {
newParents := pod.Parents.Delete(ReplicaSet)
for _, replicaSetID := range replicaSetIDs {
if replicaSet, ok := r.ReplicaSet.Nodes[replicaSetID]; ok {
if deploymentIDs, ok := replicaSet.Parents.Lookup(Deployment); ok {
newParents = newParents.Add(Deployment, deploymentIDs)
}
}
}
pod = pod.WithParents(newParents)
}
nodes[podID] = pod
}
r.Pod.Nodes = nodes

return r
}

// BackwardCompatible returns a new backward-compatible report.
//
// This for now creates node's Controls from LatestControls.
Expand Down

0 comments on commit 7618b36

Please sign in to comment.