Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: config external lookup references for aws #1084

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions api/v1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
"github.com/flanksource/gomplate/v3"
)

// List of types which should not have scraper_id
var ScraperLessTypes = []string{AWSRegion, AWSAvailabilityZone}

// ConfigFieldExclusion defines fields with JSONPath that needs to
// be removed from the config.
type ConfigFieldExclusion struct {
Expand Down
2 changes: 1 addition & 1 deletion api/v1/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ type ScrapeResult struct {
LastScrapedTime *time.Time `json:"last_scraped_time"`

// ScraperLess when true indicates that this config item
// do not belong to any scraper. Example: AWS region & availability zone.
// does not belong to any scraper. Example: AWS region & availability zone.
ScraperLess bool `json:"scraper_less,omitempty"`

// List of candidate parents in order of precision.
Expand Down
3 changes: 2 additions & 1 deletion api/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v1

import (
"fmt"
"slices"
"strings"

"github.com/flanksource/config-db/utils"
Expand Down Expand Up @@ -118,7 +119,7 @@ func (e ExternalID) Find(db *gorm.DB) *gorm.DB {
if e.ConfigType != "" {
query = query.Where("type = ?", e.ConfigType)
}
if e.ScraperID != "all" && e.ScraperID != "" {
if e.ScraperID != "all" && e.ScraperID != "" && !slices.Contains(ScraperLessTypes, e.ConfigType) {
query = query.Where("scraper_id = ?", e.ScraperID)
}
return query
Expand Down
6 changes: 4 additions & 2 deletions db/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package db
import (
"encoding/json"
"fmt"
"slices"

"github.com/flanksource/commons/hash"
"github.com/flanksource/commons/logger"
Expand Down Expand Up @@ -145,10 +146,11 @@ func NewConfigItemFromResult(ctx api.ScrapeContext, result v1.ScrapeResult) (*mo
Parents: result.Parents,
Health: lo.ToPtr(dutyModels.HealthUnknown),
Children: result.Children,
ScraperID: ctx.ScrapeConfig().GetPersistedID(),
}

if !result.ScraperLess {
ci.ScraperID = ctx.ScrapeConfig().GetPersistedID()
if result.ScraperLess || slices.Contains(v1.ScraperLessTypes, ci.Type) {
ci.ScraperID = nil
}

if parsed, err := result.Tags.AsMap(); err != nil {
Expand Down
6 changes: 2 additions & 4 deletions scrapers/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,6 @@ func (aws Scraper) availabilityZones(ctx *AWSContext, config v1.AWS, results *v1
ConfigClass: "AvailabilityZone",
Aliases: nil,
Name: lo.FromPtr(az.ZoneId),
ScraperLess: true,
Parents: []v1.ConfigExternalKey{{Type: v1.AWSRegion, ExternalID: lo.FromPtr(az.RegionName)}},
})

Expand Down Expand Up @@ -874,7 +873,6 @@ func (aws Scraper) account(ctx *AWSContext, config v1.AWS, results *v1.ScrapeRes
BaseScraper: config.BaseScraper,
Name: *region.RegionName,
ID: *region.RegionName,
ScraperLess: true,
}

if *region.OptInStatus != "not-opted-in" {
Expand Down Expand Up @@ -919,7 +917,7 @@ func (aws Scraper) users(ctx *AWSContext, config v1.AWS, results *v1.ScrapeResul
ConfigClass: "User",
Labels: labels,
Name: *user.UserName,
Aliases: []string{*user.UserId, *user.Arn},
Aliases: []string{*user.UserId, *user.UserName},
Ignore: []string{"arn", "userId", "createDate", "userName"},
ID: *user.Arn, // UserId is not often referenced
Parents: []v1.ConfigExternalKey{{Type: v1.AWSAccount, ExternalID: lo.FromPtr(ctx.Caller.Account)}},
Expand Down Expand Up @@ -1435,7 +1433,7 @@ func (aws Scraper) loadBalancers(ctx *AWSContext, config v1.AWS, results *v1.Scr
Name: *lb.LoadBalancerName,
Labels: labels,
Tags: tags,
Aliases: []string{"AWSELB/" + arn, lo.FromPtr(lb.CanonicalHostedZoneName)},
Aliases: []string{"AWSELB/" + arn, lo.FromPtr(lb.CanonicalHostedZoneName), *lb.LoadBalancerName},
ID: arn,
Parents: []v1.ConfigExternalKey{{Type: v1.AWSEC2VPC, ExternalID: lo.FromPtr(lb.VPCId)}},
RelationshipResults: relationships,
Expand Down
2 changes: 1 addition & 1 deletion scrapers/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ func ExtractResults(ctx *KubernetesContext, objs []*unstructured.Unstructured) v
if strings.HasSuffix(hostname, "elb.amazonaws.com") {
relationships = append(relationships, v1.RelationshipResult{
ConfigID: string(obj.GetUID()),
RelatedExternalID: v1.ExternalID{ExternalID: hostname, ConfigType: v1.AWSLoadBalancer},
RelatedExternalID: v1.ExternalID{ExternalID: hostname, ConfigType: v1.AWSLoadBalancer, ScraperID: "all"},
})
}
}
Expand Down
Loading