Skip to content

Commit

Permalink
chore: find config data in scrapers
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra committed Nov 4, 2024
1 parent 26540c8 commit 57cd8bf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 32 deletions.
14 changes: 12 additions & 2 deletions db/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,21 @@ func updateCI(ctx api.ScrapeContext, summary *v1.ScrapeSummary, result v1.Scrape
return true, changes, nil
}

func shouldExcludeChange(result *v1.ScrapeResult, changeResult v1.ChangeResult) (bool, error) {
func shouldExcludeChange(ctx api.ScrapeContext, result *v1.ScrapeResult, changeResult v1.ChangeResult) (bool, error) {
exclusions := result.BaseScraper.Transform.Change.Exclude

env := changeResult.AsMap()
env["config"] = result.Config
// In some cases, we might just get the
if env["config"] == nil {
ciID := lo.CoalesceOrEmpty(lo.FromPtr(result.ConfigID), changeResult.ConfigID, changeResult.ExternalID)
confObj, err := ctx.TempCache().Get(ctx, ciID)
if err != nil && ctx.PropertyOn(true, "log.changes.unmatched") {
ctx.Errorf("error finding config object with id[%s] for change exclusion: %v", ciID, err)
} else if confObj != nil && confObj.Config != nil {
env["config"] = lo.FromPtr(confObj.Config)
}
}

for _, expr := range exclusions {
if res, err := gomplate.RunTemplate(env, gomplate.Template{Expression: expr}); err != nil {
Expand Down Expand Up @@ -330,7 +340,7 @@ func extractChanges(ctx api.ScrapeContext, result *v1.ScrapeResult, ci *models.C
}
}

if exclude, err := shouldExcludeChange(result, changeResult); err != nil {
if exclude, err := shouldExcludeChange(ctx, result, changeResult); err != nil {
ctx.JobHistory().AddError(fmt.Sprintf("error running change exclusion: %v", err))
} else if exclude {
changeSummary.AddIgnored(changeResult.ChangeType)
Expand Down
1 change: 1 addition & 0 deletions scrapers/kubernetes/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,6 @@ func getChangeFromEvent(event v1.KubernetesEvent, severityKeywords v1.SeverityKe
Severity: severity,
Source: getSourceFromEvent(event),
Summary: event.Message,
ConfigID: string(event.InvolvedObject.UID),
}
}
43 changes: 13 additions & 30 deletions scrapers/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/Jeffail/gabs/v2"
"github.com/flanksource/commons/collections"
"github.com/flanksource/commons/logger"
"github.com/flanksource/duty/models"
"github.com/google/uuid"
"github.com/samber/lo"
Expand Down Expand Up @@ -244,7 +243,6 @@ func ExtractResults(ctx *KubernetesContext, objs []*unstructured.Unstructured) v
continue
}

var configData map[string]any
uid, err := ctx.FindInvolvedConfigID(event)
if err != nil {
results.Errorf(err, "")
Expand All @@ -262,36 +260,21 @@ func ExtractResults(ctx *KubernetesContext, objs []*unstructured.Unstructured) v
event.InvolvedObject.UID = types.UID(uid.String())

change := getChangeFromEvent(event, ctx.config.Event.SeverityKeywords)
if change != nil {
if ignore, err := ctx.IgnoreChange(*change, event); err != nil {
results.Errorf(err, "Failed to determine if change should be ignored: %v", err)
continue
} else if ignore {
continue
}

res := v1.ScrapeResult{
BaseScraper: ctx.config.BaseScraper,
Changes: []v1.ChangeResult{*change},
}

confObj, err := ctx.TempCache().Get(ctx.ScrapeContext, uid.String())
if err != nil {
// TODO: Should this gointo results.Errorf
logger.Warnf("unable to find config[%s] related to event: %v", uid, err)
}
if confObj != nil && confObj.Config != nil {
// Also, this won't work for resources we ignore
configData, _ = confObj.ConfigJSONStringMap()
res.ID = uid.String()
res.Config = configData
res.Type = confObj.Type
res.Name = lo.FromPtr(confObj.Name)
}

changeResults = append(changeResults, res)
if change == nil {
continue
}
if ignore, err := ctx.IgnoreChange(*change, event); err != nil {
results.Errorf(err, "Failed to determine if change should be ignored: %v", err)
continue
} else if ignore {
continue
}

changeResults = append(changeResults, v1.ScrapeResult{
BaseScraper: ctx.config.BaseScraper,
Changes: []v1.ChangeResult{*change},
})

// this is all we need from an event object
continue
}
Expand Down

0 comments on commit 57cd8bf

Please sign in to comment.