Skip to content

Commit

Permalink
fix: config_relationship & kubernetes pod parent (#538)
Browse files Browse the repository at this point in the history
* fix: config_relationship & kubernetes pod parent

* bump duty
  • Loading branch information
adityathebe authored May 14, 2024
1 parent 5d04b53 commit 81a5c2e
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion db/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,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
1 change: 1 addition & 0 deletions db/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ func SaveResults(ctx api.ScrapeContext, results []v1.ScrapeResult) error {
// once the all the scraped results are saved.
resultsWithRelationshipSelectors []v1.ScrapeResult
)

for _, result := range results {
if result.AnalysisResult != nil {
if err := upsertAnalysis(ctx, &result); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ require (
github.com/aws/aws-sdk-go-v2/service/sts v1.19.0
github.com/aws/aws-sdk-go-v2/service/support v1.14.7
github.com/aws/smithy-go v1.13.5
github.com/dominikbraun/graph v0.23.0
github.com/evanphx/json-patch v5.6.0+incompatible
github.com/fergusstrange/embedded-postgres v1.25.0
github.com/flanksource/commons v1.22.1
github.com/flanksource/duty v1.0.452
github.com/flanksource/duty v1.0.459
github.com/flanksource/is-healthy v1.0.4
github.com/flanksource/ketall v1.1.6
github.com/flanksource/mapstructure v1.6.0
Expand Down Expand Up @@ -105,7 +106,6 @@ require (
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/dominikbraun/graph v0.23.0 // indirect
github.com/eko/gocache/lib/v4 v4.1.5 // indirect
github.com/eko/gocache/store/go_cache/v4 v4.2.1 // indirect
github.com/evanphx/json-patch/v5 v5.7.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -858,8 +858,8 @@ github.com/fergusstrange/embedded-postgres v1.25.0 h1:sa+k2Ycrtz40eCRPOzI7Ry7Ttk
github.com/fergusstrange/embedded-postgres v1.25.0/go.mod h1:t/MLs0h9ukYM6FSt99R7InCHs1nW0ordoVCcnzmpTYw=
github.com/flanksource/commons v1.22.1 h1:Ycg8r26bx537UTdAEFgngDW1r2j5bX6Lr3NGxLICpiw=
github.com/flanksource/commons v1.22.1/go.mod h1:GD5+yGvmYFPIW3WMNN+y1JkeDMJY74e05pQAsRbrvwY=
github.com/flanksource/duty v1.0.452 h1:LCnKLk5e4XKI9QX5Myr+K/kO5NKgZsRiHAwveZzAztg=
github.com/flanksource/duty v1.0.452/go.mod h1:eF0Z6vZ2vSO1vy8HDYMc+OkhGWx4Pg5+P16gUuL8+oI=
github.com/flanksource/duty v1.0.459 h1:OZVwMMsBaN8CUqXoqYI8kl42B4pqEc/gVpkvfxM8CT4=
github.com/flanksource/duty v1.0.459/go.mod h1:eF0Z6vZ2vSO1vy8HDYMc+OkhGWx4Pg5+P16gUuL8+oI=
github.com/flanksource/gomplate/v3 v3.20.4/go.mod h1:27BNWhzzSjDed1z8YShO6W+z6G9oZXuxfNFGd/iGSdc=
github.com/flanksource/gomplate/v3 v3.24.2 h1:WZSriw1MaBhzrDV1IOP9eNsupIPxIHy0yTaMOVhCvsk=
github.com/flanksource/gomplate/v3 v3.24.2/go.mod h1:94BxYobZqouGdVezuz6LNto5C+yLMG0LnNnM9CUPyoo=
Expand Down
2 changes: 1 addition & 1 deletion hack/generate-schemas/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/flanksource/duty v1.0.452 // indirect
github.com/flanksource/duty v1.0.459 // indirect
github.com/flanksource/gomplate/v3 v3.24.2 // indirect
github.com/flanksource/is-healthy v1.0.4 // indirect
github.com/flanksource/kubectl-neat v1.0.4 // indirect
Expand Down
4 changes: 2 additions & 2 deletions hack/generate-schemas/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/flanksource/commons v1.22.1 h1:Ycg8r26bx537UTdAEFgngDW1r2j5bX6Lr3NGxLICpiw=
github.com/flanksource/commons v1.22.1/go.mod h1:GD5+yGvmYFPIW3WMNN+y1JkeDMJY74e05pQAsRbrvwY=
github.com/flanksource/duty v1.0.452 h1:LCnKLk5e4XKI9QX5Myr+K/kO5NKgZsRiHAwveZzAztg=
github.com/flanksource/duty v1.0.452/go.mod h1:eF0Z6vZ2vSO1vy8HDYMc+OkhGWx4Pg5+P16gUuL8+oI=
github.com/flanksource/duty v1.0.459 h1:OZVwMMsBaN8CUqXoqYI8kl42B4pqEc/gVpkvfxM8CT4=
github.com/flanksource/duty v1.0.459/go.mod h1:eF0Z6vZ2vSO1vy8HDYMc+OkhGWx4Pg5+P16gUuL8+oI=
github.com/flanksource/gomplate/v3 v3.24.2 h1:WZSriw1MaBhzrDV1IOP9eNsupIPxIHy0yTaMOVhCvsk=
github.com/flanksource/gomplate/v3 v3.24.2/go.mod h1:94BxYobZqouGdVezuz6LNto5C+yLMG0LnNnM9CUPyoo=
github.com/flanksource/is-healthy v1.0.4 h1:r/dVsi7keR5NGLCTpgO/M6Hah2jRtYNfMh/XJNnxacc=
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 81a5c2e

Please sign in to comment.