From 97c133e5c8b750bf98219f584357aeb6a4fa4cb5 Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Mon, 5 Feb 2024 11:26:54 +0545 Subject: [PATCH] fix: duty changes --- Makefile | 6 ++++++ api/context.go | 2 +- db/config.go | 13 ++++--------- jobs/sync_upstream.go | 10 +++++----- scrapers/processors/json.go | 6 +++--- scrapers/runscrapers_test.go | 3 ++- 6 files changed, 21 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index f322061f..7a03b14d 100644 --- a/Makefile +++ b/Makefile @@ -19,6 +19,12 @@ else GOBIN=$(shell go env GOBIN) endif +.PHONY: tidy +tidy: + go mod tidy + git add go.mod go.sum + cd hack/generate-schemas && go mod tidy && git add go.mod go.sum + docker: docker build . -f build/Dockerfile -t ${IMG} diff --git a/api/context.go b/api/context.go index 4cef0279..8f253032 100644 --- a/api/context.go +++ b/api/context.go @@ -17,7 +17,7 @@ import ( ) type ScrapeContext interface { - duty.DBContext + context.Context DutyContext() dutyCtx.Context IsTrace() bool diff --git a/db/config.go b/db/config.go index 40045b9d..21652ea8 100644 --- a/db/config.go +++ b/db/config.go @@ -10,15 +10,14 @@ import ( "github.com/flanksource/commons/utils" v1 "github.com/flanksource/config-db/api/v1" "github.com/flanksource/config-db/db/models" - "github.com/flanksource/duty" "github.com/flanksource/duty/context" dutyModels "github.com/flanksource/duty/models" + "github.com/flanksource/duty/query" "github.com/flanksource/duty/types" "github.com/google/uuid" "github.com/lib/pq" "github.com/ohler55/ojg/oj" "github.com/patrickmn/go-cache" - "github.com/samber/lo" "gorm.io/gorm/clause" ) @@ -102,12 +101,12 @@ func UpdateConfigItem(ci *models.ConfigItem) error { return nil } -func FindConfigsByRelationshipSelector(ctx context.Context, selector v1.RelationshipSelector) ([]dutyModels.ConfigItem, error) { +func FindConfigIDsByRelationshipSelector(ctx context.Context, selector v1.RelationshipSelector) ([]uuid.UUID, error) { if selector.IsEmpty() { return nil, nil } - return duty.FindConfigs(ctx, []types.ResourceSelector{selector.ToResourceSelector()}, duty.PickColumns("id")) + return query.FindConfigIDsByResourceSelector(ctx, selector.ToResourceSelector()) } // FindConfigIDsByNamespaceNameClass returns the uuid of config items which matches the given type, name & namespace @@ -117,12 +116,8 @@ func FindConfigIDsByNamespaceNameClass(ctx context.Context, namespace, name, con Namespace: namespace, FieldSelector: fmt.Sprintf("config_class=%s", configClass), } - items, err := duty.FindConfigs(ctx, []types.ResourceSelector{rs}, duty.PickColumns("id")) - if err != nil { - return nil, err - } - return lo.Map(items, func(c dutyModels.ConfigItem, _ int) uuid.UUID { return c.ID }), nil + return query.FindConfigIDsByResourceSelector(ctx, rs) } // QueryConfigItems ... diff --git a/jobs/sync_upstream.go b/jobs/sync_upstream.go index 352f78fe..b9cffcc9 100644 --- a/jobs/sync_upstream.go +++ b/jobs/sync_upstream.go @@ -27,7 +27,7 @@ const ( eventQueueUpdateChannel = "event_queue_updates" ) -var SyncConfigChanges = job.Job{ +var SyncConfigChanges = &job.Job{ Name: "SyncConfigChanges", JobHistory: true, Singleton: true, @@ -43,7 +43,7 @@ var SyncConfigChanges = job.Job{ }, } -var SyncConfigAnalyses = job.Job{ +var SyncConfigAnalyses = &job.Job{ Name: "SyncConfigAnalyses", JobHistory: true, Singleton: true, @@ -59,7 +59,7 @@ var SyncConfigAnalyses = job.Job{ }, } -var ReconcileConfigScrapersAndItems = job.Job{ +var ReconcileConfigScrapersAndItems = &job.Job{ Name: "ReconcileConfigScrapersAndItems", JobHistory: true, Singleton: true, @@ -87,7 +87,7 @@ var ReconcileConfigScrapersAndItems = job.Job{ }, } -var PullUpstreamConfigScrapers = job.Job{ +var PullUpstreamConfigScrapers = &job.Job{ Name: "PullUpstreamConfigScrapers", JobHistory: true, Singleton: true, @@ -102,7 +102,7 @@ var PullUpstreamConfigScrapers = job.Job{ }, } -var UpstreamJobs = []job.Job{ +var UpstreamJobs = []*job.Job{ SyncConfigChanges, SyncConfigAnalyses, PullUpstreamConfigScrapers, diff --git a/scrapers/processors/json.go b/scrapers/processors/json.go index 113e6eea..dc202b1b 100644 --- a/scrapers/processors/json.go +++ b/scrapers/processors/json.go @@ -234,15 +234,15 @@ func getRelationshipsFromRelationshipConfigs(ctx context.Context, input v1.Scrap } for _, selector := range relationshipSelectors { - linkedConfigItems, err := db.FindConfigsByRelationshipSelector(ctx, selector) + linkedConfigIDs, err := db.FindConfigIDsByRelationshipSelector(ctx, selector) if err != nil { return nil, fmt.Errorf("failed to find config items by relationship selector: %w", err) } - for _, itemToLink := range linkedConfigItems { + for _, id := range linkedConfigIDs { rel := v1.RelationshipResult{ ConfigExternalID: v1.ExternalID{ExternalID: []string{input.ID}, ConfigType: input.Type}, - RelatedConfigID: itemToLink.ID.String(), + RelatedConfigID: id.String(), } relationships = append(relationships, rel) diff --git a/scrapers/runscrapers_test.go b/scrapers/runscrapers_test.go index b107bb96..c50b4d93 100644 --- a/scrapers/runscrapers_test.go +++ b/scrapers/runscrapers_test.go @@ -15,6 +15,7 @@ import ( "github.com/flanksource/duty" "github.com/flanksource/duty/context" dutymodels "github.com/flanksource/duty/models" + "github.com/flanksource/duty/query" "github.com/flanksource/duty/types" "github.com/google/uuid" . "github.com/onsi/ginkgo/v2" @@ -117,7 +118,7 @@ var _ = Describe("Scrapers test", Ordered, func() { }) It("should correctly setup kubernetes relationship", func() { - duty.CleanCache() + query.FlushGettersCache() scraperCtx := api.NewScrapeContext(gocontext.TODO(), gormDB, nil).WithScrapeConfig(&scrapeConfig) _, err := RunScraper(scraperCtx)