Skip to content

Commit

Permalink
report.Upgrade: reconstruct namespaces
Browse files Browse the repository at this point in the history
Old probes do not report namespace topologies.
`report.upgradeNamespaces()` recontructs namespace topologies using the data available from other kubernetes resources.

Also, add a test.
  • Loading branch information
Roberto Bruggemann committed Jan 3, 2018
1 parent 2c60d50 commit 4233b75
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions report/map_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const (
KubernetesSuspended = "kubernetes_suspended"
KubernetesLastScheduled = "kubernetes_last_scheduled"
KubernetesActiveJobs = "kubernetes_active_jobs"
KubernetesStateDeleted = "deleted"
// probe/awsecs
ECSCluster = "ecs_cluster"
ECSCreatedAt = "ecs_created_at"
Expand Down
32 changes: 31 additions & 1 deletion report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func (r Report) Validate() error {
//
// This for now creates node's LatestControls from Controls.
func (r Report) Upgrade() Report {
return r.upgradeLatestControls().upgradePodNodes()
return r.upgradeLatestControls().upgradePodNodes().upgradeNamespaces()
}

func (r Report) upgradeLatestControls() Report {
Expand Down Expand Up @@ -439,6 +439,36 @@ func (r Report) upgradePodNodes() Report {
return r
}

func (r Report) upgradeNamespaces() Report {
if len(r.Namespace.Nodes) > 0 {
return r
}

namespaces := map[string]struct{}{}
for _, t := range []Topology{r.Pod, r.Service, r.Deployment, r.DaemonSet, r.StatefulSet, r.CronJob} {
for _, n := range t.Nodes {
if state, ok := n.Latest.Lookup(KubernetesState); ok && state == KubernetesStateDeleted {
continue
}
if namespace, ok := n.Latest.Lookup(KubernetesNamespace); ok {
namespaces[namespace] = struct{}{}
}
}
}

nodes := make(Nodes, len(namespaces))
for ns := range namespaces {
// Namespace ID:
// Probes did not use to report namespace ids, but since creating a report node requires an id,
// the namespace name, which is unique, is passed to `MakeNamespaceNodeID`
namespaceID := MakeNamespaceNodeID(ns)
nodes[namespaceID] = MakeNodeWith(namespaceID, map[string]string{KubernetesName: ns})
}
r.Namespace.Nodes = nodes

return r
}

// BackwardCompatible returns a new backward-compatible report.
//
// This for now creates node's Controls from LatestControls.
Expand Down
13 changes: 11 additions & 2 deletions report/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,14 @@ func TestReportUpgrade(t *testing.T) {
mtime.NowForce(time.Now())
defer mtime.NowReset()
parentsWithDeployment := report.MakeSets().Add(report.Deployment, report.MakeStringSet("id"))
rsNode := report.MakeNode("bar").WithParents(parentsWithDeployment)
podNode := report.MakeNode("foo").WithControls("alive").WithParents(report.MakeSets().Add(report.ReplicaSet, report.MakeStringSet("bar")))
rsNode := report.MakeNode("bar").
WithParents(parentsWithDeployment)
namespaceName := "ns"
namespaceID := report.MakeNamespaceNodeID(namespaceName)
podNode := report.MakeNode("foo").
WithLatests(map[string]string{report.KubernetesNamespace: namespaceName}).
WithControls("alive").
WithParents(report.MakeSets().Add(report.ReplicaSet, report.MakeStringSet("bar")))
controls := map[string]report.NodeControlData{
"alive": {
Dead: false,
Expand All @@ -110,9 +116,12 @@ func TestReportUpgrade(t *testing.T) {
rpt := report.MakeReport()
rpt.ReplicaSet.AddNode(rsNode)
rpt.Pod.AddNode(podNode)
namespaceNode := report.MakeNode(namespaceID).
WithLatests(map[string]string{report.KubernetesName: namespaceName})
expected := report.MakeReport()
expected.ReplicaSet.AddNode(rsNode)
expected.Pod.AddNode(expectedPodNode)
expected.Namespace.AddNode(namespaceNode)
got := rpt.Upgrade()
if !s_reflect.DeepEqual(expected, got) {
t.Error(test.Diff(expected, got))
Expand Down

0 comments on commit 4233b75

Please sign in to comment.