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

🧹 coordinator v2 #1104

Closed
wants to merge 2 commits into from
Closed
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
73 changes: 40 additions & 33 deletions policy/scan/local_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
// allows setting the upstream credentials from a job
allowJobCredentials bool
disableProgressBar bool
scanMutex sync.Mutex
// scanMutex sync.Mutex
}

type ScannerOption func(*LocalScanner)
Expand Down Expand Up @@ -82,7 +82,7 @@
}

func NewLocalScanner(opts ...ScannerOption) *LocalScanner {
runtime := providers.Coordinator.NewRuntime()
runtime := providers.GlobalCoordinator.NewRuntime()

Check failure on line 85 in policy/scan/local_scanner.go

View workflow job for this annotation

GitHub Actions / go-bench

undefined: providers.GlobalCoordinator

Check failure on line 85 in policy/scan/local_scanner.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: providers.GlobalCoordinator

Check failure on line 85 in policy/scan/local_scanner.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: providers.GlobalCoordinator

Check failure on line 85 in policy/scan/local_scanner.go

View workflow job for this annotation

GitHub Actions / go-test

undefined: providers.GlobalCoordinator

Check failure on line 85 in policy/scan/local_scanner.go

View workflow job for this annotation

GitHub Actions / go-test

undefined: providers.GlobalCoordinator

ls := &LocalScanner{
resolvedPolicyCache: inmemory.NewResolvedPolicyCache(ResolvedPolicyCacheSize),
Expand Down Expand Up @@ -134,8 +134,8 @@
// scan that is in progress, which will result in errors. We can address this later
// by attaching a provider to a scan and killing only the providers that are not
// not actively used.
s.scanMutex.Lock()
defer s.scanMutex.Unlock()
// s.scanMutex.Lock()
// defer s.scanMutex.Unlock()
if job == nil {
return nil, status.Errorf(codes.InvalidArgument, "missing scan job")
}
Expand Down Expand Up @@ -245,7 +245,7 @@

func (s *LocalScanner) distributeJob(job *Job, ctx context.Context, upstream *upstream.UpstreamConfig) (*ScanResult, error) {
// Always shut down the coordinator, to make sure providers are killed
defer providers.Coordinator.Shutdown()
// defer providers.Coordinator.Shutdown()

reporter, err := createReporter(ctx, job, upstream)
if err != nil {
Expand All @@ -267,40 +267,28 @@
// Within this process, we set up a catch-all deferred function, that shuts
// down all runtimes, in case we exit early.
defer func() {
for _, asset := range discoveredAssets.Assets {
for _, asset := range discoveredAssets.RootAssets {

Check failure on line 270 in policy/scan/local_scanner.go

View workflow job for this annotation

GitHub Actions / go-bench

discoveredAssets.RootAssets undefined (type *"go.mondoo.com/cnquery/v10/explorer/scan".DiscoveredAssets has no field or method RootAssets)

Check failure on line 270 in policy/scan/local_scanner.go

View workflow job for this annotation

GitHub Actions / golangci-lint

discoveredAssets.RootAssets undefined (type *"go.mondoo.com/cnquery/v10/explorer/scan".DiscoveredAssets has no field or method RootAssets)

Check failure on line 270 in policy/scan/local_scanner.go

View workflow job for this annotation

GitHub Actions / golangci-lint

discoveredAssets.RootAssets undefined (type *"go.mondoo.com/cnquery/v10/explorer/scan".DiscoveredAssets has no field or method RootAssets)

Check failure on line 270 in policy/scan/local_scanner.go

View workflow job for this annotation

GitHub Actions / go-test

discoveredAssets.RootAssets undefined (type *"go.mondoo.com/cnquery/v10/explorer/scan".DiscoveredAssets has no field or method RootAssets)

Check failure on line 270 in policy/scan/local_scanner.go

View workflow job for this annotation

GitHub Actions / go-test

discoveredAssets.RootAssets undefined (type *"go.mondoo.com/cnquery/v10/explorer/scan".DiscoveredAssets has no field or method RootAssets)
// we can call close multiple times and it will only execute once
if asset.Runtime != nil {
asset.Runtime.Close()
if asset.Coordinator != nil {
asset.Coordinator.Shutdown()
}
}
}()

if len(discoveredAssets.Assets) == 0 {
assets := discoveredAssets.GetFlattenedChildren()

Check failure on line 278 in policy/scan/local_scanner.go

View workflow job for this annotation

GitHub Actions / go-bench

discoveredAssets.GetFlattenedChildren undefined (type *"go.mondoo.com/cnquery/v10/explorer/scan".DiscoveredAssets has no field or method GetFlattenedChildren)

Check failure on line 278 in policy/scan/local_scanner.go

View workflow job for this annotation

GitHub Actions / golangci-lint

discoveredAssets.GetFlattenedChildren undefined (type *"go.mondoo.com/cnquery/v10/explorer/scan".DiscoveredAssets has no field or method GetFlattenedChildren)

Check failure on line 278 in policy/scan/local_scanner.go

View workflow job for this annotation

GitHub Actions / go-test

discoveredAssets.GetFlattenedChildren undefined (type *"go.mondoo.com/cnquery/v10/explorer/scan".DiscoveredAssets has no field or method GetFlattenedChildren)

Check failure on line 278 in policy/scan/local_scanner.go

View workflow job for this annotation

GitHub Actions / go-test

discoveredAssets.GetFlattenedChildren undefined (type *"go.mondoo.com/cnquery/v10/explorer/scan".DiscoveredAssets has no field or method GetFlattenedChildren)
if len(assets) == 0 {
return reporter.Reports(), nil
}

multiprogress, err := scan.CreateProgressBar(discoveredAssets, s.disableProgressBar)
if err != nil {
return nil, err
}

// start the progress bar
scanGroups := sync.WaitGroup{}
scanGroups.Add(1)
go func() {
defer scanGroups.Done()
if err := multiprogress.Open(); err != nil {
log.Error().Err(err).Msg("failed to open progress bar")
}
}()
log.Info().Msgf("discovered %d assets", len(assets))

assetBatches := slicesx.Batch(discoveredAssets.Assets, 100)
assetBatches := slicesx.Batch(assets, 100)

Check failure on line 285 in policy/scan/local_scanner.go

View workflow job for this annotation

GitHub Actions / go-bench

cannot infer T (/home/runner/go/pkg/mod/go.mondoo.com/cnquery/[email protected]/utils/slicesx/batch.go:6:12)

Check failure on line 285 in policy/scan/local_scanner.go

View workflow job for this annotation

GitHub Actions / golangci-lint

cannot infer T (/home/runner/go/pkg/mod/go.mondoo.com/cnquery/[email protected]/utils/slicesx/batch.go:6:12)

Check failure on line 285 in policy/scan/local_scanner.go

View workflow job for this annotation

GitHub Actions / go-test

cannot infer T (/home/runner/go/pkg/mod/go.mondoo.com/cnquery/[email protected]/utils/slicesx/batch.go:6:12)

Check failure on line 285 in policy/scan/local_scanner.go

View workflow job for this annotation

GitHub Actions / go-test

cannot infer T (/home/runner/go/pkg/mod/go.mondoo.com/cnquery/[email protected]/utils/slicesx/batch.go:6:12)
for i := range assetBatches {
batch := assetBatches[i]

// sync assets
if upstream != nil && upstream.ApiEndpoint != "" && !upstream.Incognito {
log.Info().Msg("synchronize assets")
log.Info().Msgf("synchronize %d assets", len(batch))
client, err := upstream.InitClient()
if err != nil {
return nil, err
Expand Down Expand Up @@ -353,20 +341,38 @@
}
}
}
}

multiprogress, err := scan.CreateProgressBar(discoveredAssets, s.disableProgressBar)
if err != nil {
return nil, err
}

// start the progress bar
scanGroups := sync.WaitGroup{}
scanGroups.Add(1)
go func() {
defer scanGroups.Done()
if err := multiprogress.Open(); err != nil {
log.Error().Err(err).Msg("failed to open progress bar")
}
}()

// // if a bundle was provided check that it matches the filter, bundles can also be downloaded
// // later therefore we do not want to stop execution here
// if job.Bundle != nil && job.Bundle.FilterPolicies(job.PolicyFilters) {
// return nil, false, errors.New("all available packs filtered out. nothing to do.")
// }
// // if a bundle was provided check that it matches the filter, bundles can also be downloaded
// // later therefore we do not want to stop execution here
// if job.Bundle != nil && job.Bundle.FilterPolicies(job.PolicyFilters) {
// return nil, false, errors.New("all available packs filtered out. nothing to do.")
// }

for k := range discoveredAssets.RootAssets {

Check failure on line 367 in policy/scan/local_scanner.go

View workflow job for this annotation

GitHub Actions / go-bench

discoveredAssets.RootAssets undefined (type *"go.mondoo.com/cnquery/v10/explorer/scan".DiscoveredAssets has no field or method RootAssets)

Check failure on line 367 in policy/scan/local_scanner.go

View workflow job for this annotation

GitHub Actions / golangci-lint

discoveredAssets.RootAssets undefined (type *"go.mondoo.com/cnquery/v10/explorer/scan".DiscoveredAssets has no field or method RootAssets)

Check failure on line 367 in policy/scan/local_scanner.go

View workflow job for this annotation

GitHub Actions / go-test

discoveredAssets.RootAssets undefined (type *"go.mondoo.com/cnquery/v10/explorer/scan".DiscoveredAssets has no field or method RootAssets)
root := discoveredAssets.RootAssets[k]

Check failure on line 368 in policy/scan/local_scanner.go

View workflow job for this annotation

GitHub Actions / go-bench

discoveredAssets.RootAssets undefined (type *"go.mondoo.com/cnquery/v10/explorer/scan".DiscoveredAssets has no field or method RootAssets)

Check failure on line 368 in policy/scan/local_scanner.go

View workflow job for this annotation

GitHub Actions / golangci-lint

discoveredAssets.RootAssets undefined (type *"go.mondoo.com/cnquery/v10/explorer/scan".DiscoveredAssets has no field or method RootAssets)) (typecheck)

Check failure on line 368 in policy/scan/local_scanner.go

View workflow job for this annotation

GitHub Actions / go-test

discoveredAssets.RootAssets undefined (type *"go.mondoo.com/cnquery/v10/explorer/scan".DiscoveredAssets has no field or method RootAssets)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
for i := range batch {
asset := batch[i].Asset
runtime := batch[i].Runtime
for i := range root.Children {
asset := root.Children[i].Asset
runtime := root.Children[i].Runtime

log.Debug().Interface("asset", asset).Msg("start scan")

Expand Down Expand Up @@ -398,6 +404,7 @@
// shut down all ephemeral runtimes
runtime.Close()
}
root.Coordinator.Shutdown()
}()
wg.Wait()
}
Expand Down
Loading