Skip to content

Commit

Permalink
fix: parent id of Namespace & Cluster in incremental k8s scraper
Browse files Browse the repository at this point in the history
In an incremental scraper, we don't have the namespace object. Hence, resourceIDMap would be empty for all the namespaces.
If any namespaced object, like a Deployment, were to be scraped by the incremental scraper, we wouldn't be able to set the parent of that deployment because we don't know the id of the parent namespace.

In the workload cluster, the incremental scraper wasn't able to set the parent for a newly created cronjob so the saving of CronJob config failed. Consequently, saving of the job and also the pod would fail. This would happen until a full scrape.
  • Loading branch information
adityathebe committed May 23, 2024
1 parent d1d7fb7 commit b09584c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
5 changes: 3 additions & 2 deletions db/models/config_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/flanksource/duty/types"
"github.com/google/uuid"
"github.com/lib/pq"
"github.com/samber/lo"
)

// ConfigItem represents the config item database table
Expand Down Expand Up @@ -48,10 +49,10 @@ type ConfigItem struct {

func (ci ConfigItem) String() string {
if len(ci.ExternalID) == 0 {
return fmt.Sprintf("id=%s name=%s type=%s", ci.ID, *ci.Type, *ci.Name)
return fmt.Sprintf("id=%s type=%s name=%s ", ci.ID, lo.FromPtr(ci.Type), lo.FromPtr(ci.Name))
}

return fmt.Sprintf("id=%s name=%s type=%s external_id=%s", ci.ID, *ci.Type, *ci.Name, ci.ExternalID[0])
return fmt.Sprintf("id=%s type=%s name=%s external_id=%s", ci.ID, lo.FromPtr(ci.Type), lo.FromPtr(ci.Name), ci.ExternalID[0])
}

func (ci ConfigItem) ConfigJSONStringMap() (map[string]interface{}, error) {
Expand Down
4 changes: 4 additions & 0 deletions db/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,10 @@ func extractConfigsAndChangesFromResults(ctx api.ScrapeContext, scrapeStartTime

func setConfigParents(ctx api.ScrapeContext, parentTypeToConfigMap map[configExternalKey]string, allConfigs []*models.ConfigItem) error {
for _, ci := range allConfigs {
if ci.ParentID != nil {
continue // existing item. Parent is already set.
}

if ci.ParentExternalID == "" || ci.ParentType == "" {
continue
}
Expand Down
22 changes: 16 additions & 6 deletions scrapers/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ func ExtractResults(ctx context.Context, config v1.Kubernetes, objs []*unstructu
return results.Errorf(err, "failed to clean kubernetes object")
}

parentType, parentExternalID := getKubernetesParent(obj, config.Exclusions, resourceIDMap)
parentType, parentExternalID := getKubernetesParent(obj, config, resourceIDMap)
results = append(results, v1.ScrapeResult{
BaseScraper: config.BaseScraper,
Name: obj.GetName(),
Expand All @@ -439,7 +439,7 @@ func ExtractResults(ctx context.Context, config v1.Kubernetes, objs []*unstructu
ID: string(obj.GetUID()),
Labels: stripLabels(labels, "-hash"),
Tags: tags,
Aliases: getKubernetesAlias(obj),
Aliases: []string{getKubernetesAlias(obj.GetKind(), obj.GetNamespace(), obj.GetName())},
ParentExternalID: parentExternalID,
ParentType: ConfigTypePrefix + parentType,
RelationshipResults: relationships,
Expand All @@ -466,14 +466,14 @@ func ExtractResults(ctx context.Context, config v1.Kubernetes, objs []*unstructu
return results
}

func getKubernetesParent(obj *unstructured.Unstructured, exclusions v1.KubernetesExclusionConfig, resourceIDMap map[string]map[string]map[string]string) (string, string) {
func getKubernetesParent(obj *unstructured.Unstructured, config v1.Kubernetes, 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" && lo.Contains(exclusions.Kinds, "ReplicaSet") {
if obj.GetKind() == "Pod" && lo.Contains(config.Exclusions.Kinds, "ReplicaSet") {
// If ReplicaSet is excluded then we want the pod's direct parent to
// be its Deployment
if ref.Kind == "ReplicaSet" {
Expand All @@ -492,17 +492,27 @@ func getKubernetesParent(obj *unstructured.Unstructured, exclusions v1.Kubernete
if obj.GetNamespace() != "" {
parentConfigType = "Namespace"
parentExternalID = resourceIDMap[""]["Namespace"][obj.GetNamespace()]

if obj.GetKind() == "CronJob" {
_ = obj
}

if parentExternalID == "" {
parentExternalID = getKubernetesAlias("Namespace", "", obj.GetNamespace())
}

return parentConfigType, parentExternalID
}

// Everything which is not namespaced should be mapped to cluster
parentConfigType = "Cluster"
parentExternalID = resourceIDMap[""]["Cluster"]["selfRef"]

return parentConfigType, parentExternalID
}

func getKubernetesAlias(obj *unstructured.Unstructured) []string {
return []string{strings.Join([]string{"Kubernetes", obj.GetKind(), obj.GetNamespace(), obj.GetName()}, "/")}
func getKubernetesAlias(kind, namespace, name string) string {
return strings.Join([]string{"Kubernetes", kind, namespace, name}, "/")
}

func updateOptions(ctx context.Context, opts *options.KetallOptions, config v1.Kubernetes) (*options.KetallOptions, error) {
Expand Down

0 comments on commit b09584c

Please sign in to comment.