diff --git a/api/cache.go b/api/cache.go index 311f89ed3..c50e53062 100644 --- a/api/cache.go +++ b/api/cache.go @@ -18,12 +18,8 @@ type TempCache struct { aliases map[string]string } -func (t TempCache) FindExternal(ext v1.ExternalID) (*models.ConfigItem, error) { - return t.Find(ext.ConfigType, ext.ExternalID[0]) -} - -func (t TempCache) FindExternalID(ext v1.ExternalID) (string, error) { - if item, err := t.Find(ext.ConfigType, ext.ExternalID[0]); err != nil { +func (t TempCache) FindExternalID(scraperID string, ext v1.ExternalID) (string, error) { + if item, err := t.Find(scraperID, ext.ConfigType, ext.ExternalID[0]); err != nil { return "", err } else if item != nil { return item.ID, nil @@ -31,13 +27,14 @@ func (t TempCache) FindExternalID(ext v1.ExternalID) (string, error) { return "", nil } -func (t TempCache) Find(typ, id string) (*models.ConfigItem, error) { +func (t TempCache) Find(scraperID, typ, id string) (*models.ConfigItem, error) { typ = strings.ToLower(typ) if strings.HasPrefix(typ, "kubernetes::") && uuid.Validate(id) == nil { // kubernetes external ids are stored are the same as the config ids return t.Get(id) } + if t.aliases == nil { t.aliases = make(map[string]string) } @@ -47,13 +44,19 @@ func (t TempCache) Find(typ, id string) (*models.ConfigItem, error) { } result := models.ConfigItem{} - if err := t.ctx.DB().Limit(1).Find(&result, "lower(type) = ? and external_id @> ?", typ, pq.StringArray{id}).Error; err != nil { + query := t.ctx.DB().Limit(1) + if id != "" { + query = query.Where("scraper_id = ?", scraperID) + } + if err := query.Find(&result, "lower(type) = ? and external_id @> ?", typ, pq.StringArray{id}).Error; err != nil { return nil, err } + if result.ID != "" { t.Insert(result) return &result, nil } + return nil, nil } diff --git a/db/update.go b/db/update.go index 6d9dc6b48..10b8834f2 100644 --- a/db/update.go +++ b/db/update.go @@ -258,7 +258,7 @@ func extractChanges(ctx api.ScrapeContext, result *v1.ScrapeResult, ci *models.C if change.ConfigID == "" && change.GetExternalID().IsEmpty() && ci != nil { change.ConfigID = ci.ID } else if !change.GetExternalID().IsEmpty() { - if ci, err := ctx.TempCache().FindExternalID(change.GetExternalID()); err != nil { + if ci, err := ctx.TempCache().FindExternalID(string(ctx.ScrapeConfig().GetUID()), change.GetExternalID()); err != nil { return nil, nil, changeSummary, fmt.Errorf("failed to get config from change (externalID=%s): %w", change.GetExternalID(), err) } else if ci != "" { change.ConfigID = ci @@ -289,7 +289,7 @@ func extractChanges(ctx api.ScrapeContext, result *v1.ScrapeResult, ci *models.C func upsertAnalysis(ctx api.ScrapeContext, result *v1.ScrapeResult) error { analysis := result.AnalysisResult.ToConfigAnalysis() - ciID, err := ctx.TempCache().Find(analysis.ConfigType, analysis.ExternalID) + ciID, err := ctx.TempCache().Find(string(ctx.ScrapeConfig().GetUID()), analysis.ConfigType, analysis.ExternalID) if err != nil { return err } @@ -578,7 +578,7 @@ func relationshipResultHandler(ctx api.ScrapeContext, relationships v1.Relations if relationship.ConfigID != "" { configID = relationship.ConfigID } else { - configID, err = ctx.TempCache().FindExternalID(relationship.ConfigExternalID) + configID, err = ctx.TempCache().FindExternalID(string(ctx.ScrapeConfig().GetUID()), relationship.ConfigExternalID) if err != nil { logger.Errorf("error fetching config item(id=%s): %v", relationship.ConfigExternalID, err) continue @@ -593,7 +593,7 @@ func relationshipResultHandler(ctx api.ScrapeContext, relationships v1.Relations if relationship.RelatedConfigID != "" { relatedID = relationship.RelatedConfigID } else { - relatedID, err = ctx.TempCache().FindExternalID(relationship.RelatedExternalID) + relatedID, err = ctx.TempCache().FindExternalID(string(ctx.ScrapeConfig().GetUID()), relationship.RelatedExternalID) if err != nil { logger.Errorf("error fetching external config item(id=%s): %v", relationship.RelatedExternalID, err) continue @@ -676,7 +676,7 @@ func extractConfigsAndChangesFromResults(ctx api.ScrapeContext, scrapeStartTime return nil, nil, nil, nil, allChangeSummary, fmt.Errorf("unable to lookup existing config(%s): %w", ci, err) } } else { - if existing, err = ctx.TempCache().Find(*ci.Type, ci.ExternalID[0]); err != nil { + if existing, err = ctx.TempCache().Find(string(ctx.ScrapeConfig().GetUID()), *ci.Type, ci.ExternalID[0]); err != nil { return nil, nil, nil, nil, allChangeSummary, fmt.Errorf("unable to lookup external id(%s): %w", ci, err) } } @@ -776,7 +776,7 @@ func setConfigParents(ctx api.ScrapeContext, parentTypeToConfigMap map[configExt break } - if found, err := ctx.TempCache().Find(parent.Type, parent.ExternalID); err != nil { + if found, err := ctx.TempCache().Find(string(ctx.ScrapeConfig().GetUID()), parent.Type, parent.ExternalID); err != nil { return err } else if found != nil { ci.ParentID = &found.ID diff --git a/scrapers/aws/aws.go b/scrapers/aws/aws.go index 1b4ab7f47..6fc68924e 100644 --- a/scrapers/aws/aws.go +++ b/scrapers/aws/aws.go @@ -1264,7 +1264,7 @@ func (aws Scraper) loadBalancers(ctx *AWSContext, config v1.AWS, results *v1.Scr Name: *lb.LoadBalancerName, Labels: labels, Tags: tags, - Aliases: []string{"AWSELB/" + arn, arn}, + Aliases: []string{"AWSELB/" + arn, arn, lo.FromPtr(lb.CanonicalHostedZoneName)}, ID: *lb.LoadBalancerName, Parents: []v1.ConfigExternalKey{{Type: v1.AWSEC2VPC, ExternalID: lo.FromPtr(lb.VPCId)}}, RelationshipResults: relationships, diff --git a/scrapers/kubernetes/kubernetes.go b/scrapers/kubernetes/kubernetes.go index e2fa2f192..23c32db8b 100644 --- a/scrapers/kubernetes/kubernetes.go +++ b/scrapers/kubernetes/kubernetes.go @@ -516,6 +516,13 @@ func ExtractResults(ctx api.ScrapeContext, config v1.Kubernetes, objs []*unstruc if ingress, ok := ing.(map[string]any); ok { if hostname, ok := ingress["hostname"].(string); ok && hostname != "" { labels["hostname"] = hostname + + if strings.HasSuffix(hostname, "elb.amazonaws.com") { + relationships = append(relationships, v1.RelationshipResult{ + ConfigID: string(obj.GetUID()), + RelatedExternalID: v1.ExternalID{ExternalID: []string{hostname}, ConfigType: v1.AWSLoadBalancer}, + }) + } } if ip, ok := ingress["ip"].(string); ok && ip != "" {