Skip to content

Commit

Permalink
Merge pull request #342 from flanksource/feat/link-kubernetes-resourc…
Browse files Browse the repository at this point in the history
…es-to-namespace

feat: link kubernetes resources to their namespace
  • Loading branch information
moshloop authored Dec 31, 2023
2 parents 7a62640 + afd5412 commit 52306bf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
15 changes: 15 additions & 0 deletions scrapers/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kubernetes

import (
"fmt"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -109,6 +110,20 @@ func (kubernetes KubernetesScraper) Scrape(ctx api.ScrapeContext) v1.ScrapeResul
}
}

if obj.GetNamespace() != "" {
relationships = append(relationships, v1.RelationshipResult{
ConfigExternalID: v1.ExternalID{
ExternalID: []string{string(obj.GetUID())},
ConfigType: ConfigTypePrefix + obj.GetKind(),
},
RelatedExternalID: v1.ExternalID{
ExternalID: []string{fmt.Sprintf("Kubernetes/Namespace//%s", obj.GetNamespace())},
ConfigType: ConfigTypePrefix + "Namespace",
},
Relationship: "Namespace" + obj.GetKind(),
})
}

for _, ownerRef := range obj.GetOwnerReferences() {
rel := v1.RelationshipResult{
ConfigExternalID: v1.ExternalID{
Expand Down
7 changes: 5 additions & 2 deletions scrapers/runscrapers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ var _ = Describe("Scrapers test", Ordered, func() {
Expect(err).NotTo(HaveOccurred(), "failed to create Secret")
})

It("should save second configMap", func() {
It("should save a second configMap", func() {
cm2 := &apiv1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "second-config",
Expand Down Expand Up @@ -124,7 +124,10 @@ var _ = Describe("Scrapers test", Ordered, func() {
err = gormDB.Find(&configRelationships).Error
Expect(err).To(BeNil())

Expect(len(configRelationships)).To(Equal(2))
// 2 relationships are coming from the relationship config above &
// the remaining 21 are coming from the relationship with the namespace.
// eg. Namespace->ConfigMap,Namespace->Endpoints, Namespace->RoleBinding, Namespace->Role ...
Expect(len(configRelationships)).To(Equal(2 + 21))
})
})

Expand Down

0 comments on commit 52306bf

Please sign in to comment.