Skip to content

Commit

Permalink
cleanup delayed discovery handling
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Milchev <[email protected]>
  • Loading branch information
imilchev committed Mar 6, 2024
1 parent d3be245 commit a3ae12c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
47 changes: 27 additions & 20 deletions explorer/scan/local_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,30 +342,13 @@ func (s *LocalScanner) distributeJob(job *Job, ctx context.Context, upstream *up
}

if asset.Connections[0].DelayDiscovery {
asset.Connections[0].DelayDiscovery = false
if err := runtime.Connect(&plugin.ConnectReq{Asset: asset}); err != nil {
log.Error().Err(err).Str("asset", asset.Name).Msg("failed to connect to asset")
discoveredAsset, err := handleDelayedDiscovery(ctx, asset, runtime, services, spaceMrn)
if err != nil {
reporter.AddScanError(asset, err)
multiprogress.Errored(asset.PlatformIds[0])
continue
}
if services != nil {
resp, err := services.SynchronizeAssets(ctx, &explorer.SynchronizeAssetsReq{
SpaceMrn: spaceMrn,
List: []*inventory.Asset{asset},
})
if err != nil {
reporter.AddScanError(asset, err)
multiprogress.Errored(asset.PlatformIds[0])
continue
}

asset = runtime.Provider.Connection.Asset
slices.Sort(asset.PlatformIds)
details := resp.Details[asset.PlatformIds[0]]
asset.Mrn = details.AssetMrn
asset.Url = details.Url
}
asset = discoveredAsset
}

p := &progress.MultiProgressAdapter{Key: asset.PlatformIds[0], Multi: multiprogress}
Expand All @@ -392,6 +375,30 @@ func (s *LocalScanner) distributeJob(job *Job, ctx context.Context, upstream *up
return reporter.Reports(), nil
}

func handleDelayedDiscovery(ctx context.Context, asset *inventory.Asset, runtime *providers.Runtime, services *explorer.Services, spaceMrn string) (*inventory.Asset, error) {
asset.Connections[0].DelayDiscovery = false
if err := runtime.Connect(&plugin.ConnectReq{Asset: asset}); err != nil {
return nil, err
}
if services != nil {
resp, err := services.SynchronizeAssets(ctx, &explorer.SynchronizeAssetsReq{
SpaceMrn: spaceMrn,
List: []*inventory.Asset{asset},
})
if err != nil {
return nil, err
}

asset = runtime.Provider.Connection.Asset
slices.Sort(asset.PlatformIds)
details := resp.Details[asset.PlatformIds[0]]
asset.Mrn = details.AssetMrn
asset.Url = details.Url
asset.KindString = asset.GetPlatform().Kind
}
return asset, nil
}

func (s *LocalScanner) RunAssetJob(job *AssetJob) {
log.Debug().Msgf("connecting to asset %s", job.Asset.HumanName())
results, err := s.runMotorizedAsset(job)
Expand Down
3 changes: 1 addition & 2 deletions providers/os/connection/container/image_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func NewImageConnection(id uint32, conf *inventory.Config, asset *inventory.Asse

return tar.NewConnection(id, conf, asset,
tar.WithFetchFn(func() (string, error) {
log.Warn().Msgf("fetching image %s", conf.Host)
err = tar.StreamToTmpFile(mutate.Extract(img), f)
if err != nil {
_ = os.Remove(f.Name())
Expand All @@ -47,7 +46,7 @@ func NewImageConnection(id uint32, conf *inventory.Config, asset *inventory.Asse
return f.Name(), nil
}),
tar.WithCloseFn(func() {
log.Warn().Str("tar", f.Name()).Msg("tar> remove temporary tar file on connection close")
log.Debug().Str("tar", f.Name()).Msg("tar> remove temporary tar file on connection close")
_ = os.Remove(f.Name())
}),
)
Expand Down
1 change: 0 additions & 1 deletion providers/os/connection/docker/container_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@ func NewContainerImageConnection(id uint32, conf *inventory.Config, asset *inven
conf,
asset,
tar.WithFetchFn(func() (string, error) {
log.Warn().Msgf("pulling image %s", ii.ID)
img, err := image.LoadImageFromDockerEngine(ii.ID, disableInmemoryCache)
if err != nil {
return filename, err
Expand Down

0 comments on commit a3ae12c

Please sign in to comment.