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

chore: add cluster object to scrape results & refactor temp cache #1056

Merged
merged 1 commit into from
Oct 9, 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
46 changes: 14 additions & 32 deletions api/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ type TempCache struct {
notFound map[string]struct{}
}

func NewTempCache() *TempCache {
return &TempCache{
items: make(map[string]models.ConfigItem),
aliases: make(map[string]string),
notFound: make(map[string]struct{}),
}
}

func (t *TempCache) FindExternalID(ctx ScrapeContext, ext v1.ExternalID) (string, error) {
if item, err := t.Find(ctx, ext); err != nil {
return "", err
Expand All @@ -46,10 +54,6 @@ func (t *TempCache) Find(ctx ScrapeContext, lookup v1.ExternalID) (*models.Confi
return t.Get(ctx, uid)
}

if t.aliases == nil {
t.aliases = make(map[string]string)
}

if alias, ok := t.aliases[lookup.Key()]; ok {
return t.Get(ctx, alias)
}
Expand All @@ -59,28 +63,16 @@ func (t *TempCache) Find(ctx ScrapeContext, lookup v1.ExternalID) (*models.Confi
return nil, err
}

if result.ID != "" {
t.Insert(result)
return &result, nil
} else {

if t.notFound == nil {
t.notFound = make(map[string]struct{})
}
if result.ID == "" {
t.notFound[lookup.Key()] = struct{}{}
return nil, nil
}

return nil, nil
t.Insert(result)
return &result, nil
}

func (t *TempCache) Insert(item models.ConfigItem) {
if t.aliases == nil {
t.aliases = make(map[string]string)
}
if t.items == nil {
t.items = make(map[string]models.ConfigItem)
}

scraperID := lo.FromPtr(item.ScraperID).String()
for _, extID := range item.ExternalID {
key := v1.ExternalID{ConfigType: item.Type, ExternalID: extID, ScraperID: scraperID}.Key()
Expand All @@ -104,10 +96,6 @@ func (t *TempCache) Get(ctx ScrapeContext, id string) (*models.ConfigItem, error
return nil, nil
}

if t.items == nil {
t.items = make(map[string]models.ConfigItem)
}

if item, ok := t.items[id]; ok {
return &item, nil
}
Expand All @@ -121,9 +109,6 @@ func (t *TempCache) Get(ctx ScrapeContext, id string) (*models.ConfigItem, error
t.Insert(result)
return &result, nil
} else {
if t.notFound == nil {
t.notFound = make(map[string]struct{})
}
t.notFound[id] = struct{}{}
}

Expand All @@ -134,16 +119,13 @@ func QueryCache(ctx context.Context, query string, args ...interface{}) (*TempCa
if ctx.DB() == nil {
return nil, fmt.Errorf("no db configured")
}
t := TempCache{
items: make(map[string]models.ConfigItem),
aliases: make(map[string]string),
}
t := NewTempCache()
items := []models.ConfigItem{}
if err := ctx.DB().Table("config_items").Where("deleted_at IS NULL").Where(query, args...).Find(&items).Error; err != nil {
return nil, err
}
for _, item := range items {
t.Insert(item)
}
return &t, nil
return t, nil
}
6 changes: 1 addition & 5 deletions api/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/flanksource/commons/logger"
v1 "github.com/flanksource/config-db/api/v1"
dbModel "github.com/flanksource/config-db/db/models"
dutyCtx "github.com/flanksource/duty/context"
"github.com/flanksource/duty/models"
)
Expand All @@ -26,10 +25,7 @@ type ScrapeContext struct {
func NewScrapeContext(ctx dutyCtx.Context) ScrapeContext {
return ScrapeContext{
Context: ctx,
temp: &TempCache{
items: make(map[string]dbModel.ConfigItem),
notFound: make(map[string]struct{}),
},
temp: NewTempCache(),
}
}

Expand Down
14 changes: 14 additions & 0 deletions scrapers/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,20 @@ func ExtractResults(ctx *KubernetesContext, objs []*unstructured.Unstructured) v
changeResults v1.ScrapeResults
)

clusterName := ctx.config.ClusterName
cluster := v1.ScrapeResult{
BaseScraper: ctx.config.BaseScraper,
Name: clusterName,
ConfigClass: "Cluster",
Type: ConfigTypePrefix + "Cluster",
Config: make(map[string]any),
Labels: make(v1.JSONStringMap),
ID: "Kubernetes/Cluster/" + clusterName,
Tags: v1.Tags{{Name: "cluster", Value: clusterName}},
}

results = append(results, cluster)

ctx.Load(objs)
if ctx.isIncremental {
// On incremental scrape, we do not have all the data in the resource ID map.
Expand Down
Loading