diff --git a/db/update.go b/db/update.go index 3ae3f1f2..597b8013 100644 --- a/db/update.go +++ b/db/update.go @@ -276,11 +276,22 @@ 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 change result but not the config + // so we fetch config here + 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 { @@ -330,7 +341,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) diff --git a/scrapers/kubernetes/events.go b/scrapers/kubernetes/events.go index 0ac991cc..4612f321 100644 --- a/scrapers/kubernetes/events.go +++ b/scrapers/kubernetes/events.go @@ -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), } } diff --git a/scrapers/kubernetes/kubernetes.go b/scrapers/kubernetes/kubernetes.go index 7cdecaa8..154144be 100644 --- a/scrapers/kubernetes/kubernetes.go +++ b/scrapers/kubernetes/kubernetes.go @@ -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" @@ -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, "") @@ -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 }