From a3ae12c29900b5f82803b37b5e1f2fc63cbab375 Mon Sep 17 00:00:00 2001 From: Ivan Milchev Date: Wed, 6 Mar 2024 14:48:03 +0100 Subject: [PATCH] cleanup delayed discovery handling Signed-off-by: Ivan Milchev --- explorer/scan/local_scanner.go | 47 +++++++++++-------- .../connection/container/image_connection.go | 3 +- .../connection/docker/container_connection.go | 1 - 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/explorer/scan/local_scanner.go b/explorer/scan/local_scanner.go index 9e708e0234..814ad0a974 100644 --- a/explorer/scan/local_scanner.go +++ b/explorer/scan/local_scanner.go @@ -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} @@ -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) diff --git a/providers/os/connection/container/image_connection.go b/providers/os/connection/container/image_connection.go index 6d449d055a..e90df9c062 100644 --- a/providers/os/connection/container/image_connection.go +++ b/providers/os/connection/container/image_connection.go @@ -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()) @@ -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()) }), ) diff --git a/providers/os/connection/docker/container_connection.go b/providers/os/connection/docker/container_connection.go index 1b9a459f17..a0d5940188 100644 --- a/providers/os/connection/docker/container_connection.go +++ b/providers/os/connection/docker/container_connection.go @@ -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