Skip to content

Commit

Permalink
feat: multiple candidate parents for a single config
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe authored and moshloop committed Jun 10, 2024
1 parent eecc4b5 commit 32e8c95
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 209 deletions.
10 changes: 8 additions & 2 deletions api/v1/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,11 @@ func (t Tags) Eval(labels map[string]string, config string) (Tags, error) {
return t, nil
}

type ConfigExternalKey struct {
ExternalID string
Type string
}

// ScrapeResult ...
// +kubebuilder:object:generate=false
type ScrapeResult struct {
Expand Down Expand Up @@ -384,11 +389,12 @@ type ScrapeResult struct {
RelationshipResults RelationshipResults `json:"-"`
Ignore []string `json:"-"`
Action string `json:",omitempty"`
ParentExternalID string `json:"-"`
ParentType string `json:"-"`
Properties types.Properties `json:"properties,omitempty"`
LastScrapedTime *time.Time `json:"last_scraped_time"`

// List of candidate parents in order of precision.
Parents []ConfigExternalKey `json:"-"`

// RelationshipSelectors are used to form relationship of this scraped item with other items.
// Unlike `RelationshipResults`, selectors give you the flexibility to form relationship without
// knowing the external ids of the item to be linked.
Expand Down
4 changes: 1 addition & 3 deletions db/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,7 @@ func NewConfigItemFromResult(ctx api.ScrapeContext, result v1.ScrapeResult) (*mo
Config: &dataStr,
Ready: result.Ready,
LastScrapedTime: result.LastScrapedTime,

ParentExternalID: result.ParentExternalID,
ParentType: result.ParentType,
Parents: result.Parents,
}

if parsed, err := result.Tags.AsMap(); err != nil {
Expand Down
3 changes: 1 addition & 2 deletions db/models/config_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ type ConfigItem struct {
LastScrapedTime *time.Time `gorm:"column:last_scraped_time" json:"last_scraped_time"`
DeleteReason v1.ConfigDeleteReason `gorm:"column:delete_reason" json:"delete_reason"`

ParentExternalID string `gorm:"-" json:"-"`
ParentType string `gorm:"-" json:"-"`
Parents []v1.ConfigExternalKey `gorm:"-" json:"parents,omitempty"`
}

func (ci ConfigItem) String() string {
Expand Down
37 changes: 23 additions & 14 deletions db/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,24 +721,33 @@ func setConfigParents(ctx api.ScrapeContext, parentTypeToConfigMap map[configExt
continue // existing item. Parent is already set.
}

if ci.ParentExternalID == "" || ci.ParentType == "" {
continue
if len(ci.Parents) == 0 {
continue // these are root items.
}

if parentID, found := parentTypeToConfigMap[configExternalKey{
externalID: ci.ParentExternalID,
parentType: ci.ParentType,
}]; found {
ci.ParentID = &parentID
continue
for _, parent := range ci.Parents {
if parent.ExternalID == "" || parent.Type == "" {
continue
}

if parentID, found := parentTypeToConfigMap[configExternalKey{
externalID: parent.ExternalID,
parentType: parent.Type,
}]; found {
ci.ParentID = &parentID
break
}

if found, err := ctx.TempCache().Find(parent.Type, parent.ExternalID); err != nil {
return err
} else if found != nil {
ci.ParentID = &found.ID
break
}
}

if found, err := ctx.TempCache().Find(ci.ParentType, ci.ParentExternalID); err != nil {
return err
} else if found != nil {
ci.ParentID = &found.ID
} else {
ctx.Logger.V(0).Infof("[%s] parent %s/%s not found", ci, ci.ParentType, ci.ParentExternalID)
if ci.ParentID == nil {
ctx.Logger.V(0).Infof("parent not found for config [%s]", ci)
}
}

Expand Down
Loading

0 comments on commit 32e8c95

Please sign in to comment.