Skip to content

Commit

Permalink
fix: config_relationship & kubernetes pod parent
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed May 10, 2024
1 parent 0a37faa commit 38a8ec3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion db/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func GetJSON(ci models.ConfigItem) []byte {

func UpdateConfigRelatonships(ctx api.ScrapeContext, relationships []models.ConfigRelationship) error {
return ctx.DB().Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "config_id"}, {Name: "related_id"}, {Name: "selector_id"}},
Columns: []clause.Column{{Name: "config_id"}, {Name: "related_id"}, {Name: "relation"}},
DoNothing: true,
}).CreateInBatches(relationships, 200).Error
}
Expand Down
2 changes: 2 additions & 0 deletions db/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,8 @@ func SaveResults(ctx api.ScrapeContext, results []v1.ScrapeResult) error {
resultsWithRelationshipSelectors []v1.ScrapeResult
)

// TODO:: Sort the results so that parents are inserted first

var toTouch []string
for _, result := range results {
result.LastScrapedTime = &startTime
Expand Down
11 changes: 7 additions & 4 deletions scrapers/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ func ExtractResults(ctx context.Context, config v1.Kubernetes, objs []*unstructu
return results.Errorf(err, "failed to clean kubernetes object")
}

parentType, parentExternalID := getKubernetesParent(obj, resourceIDMap)
parentType, parentExternalID := getKubernetesParent(obj, config.Exclusions, resourceIDMap)
results = append(results, v1.ScrapeResult{
BaseScraper: config.BaseScraper,
Name: obj.GetName(),
Expand Down Expand Up @@ -472,21 +472,24 @@ func ExtractResults(ctx context.Context, config v1.Kubernetes, objs []*unstructu
return results
}

func getKubernetesParent(obj *unstructured.Unstructured, resourceIDMap map[string]map[string]map[string]string) (string, string) {
func getKubernetesParent(obj *unstructured.Unstructured, exclusions v1.KubernetesExclusionConfig, resourceIDMap map[string]map[string]map[string]string) (string, string) {
var parentExternalID, parentConfigType string

// This will work for pods and replicasets
if len(obj.GetOwnerReferences()) > 0 {
ref := obj.GetOwnerReferences()[0]
if obj.GetKind() == "Pod" {
// We want pod's parents as Deployments

if obj.GetKind() == "Pod" && lo.Contains(exclusions.Kinds, "ReplicaSet") {
// If ReplicaSet is excluded then we want the pod's direct parent to
// be its Deployment
if ref.Kind == "ReplicaSet" {
deployName := extractDeployNameFromReplicaSet(ref.Name)
parentConfigType = "Deployment"
parentExternalID = resourceIDMap[obj.GetNamespace()]["Deployment"][deployName]
return parentConfigType, parentExternalID
}
}

parentConfigType = ref.Kind
parentExternalID = string(ref.UID)
return parentConfigType, parentExternalID
Expand Down

0 comments on commit 38a8ec3

Please sign in to comment.