Skip to content

Commit

Permalink
🧹 Do not fetch azure instance info if not required. (#3311)
Browse files Browse the repository at this point in the history
Signed-off-by: Preslav <[email protected]>
  • Loading branch information
preslavgerchev authored Feb 14, 2024
1 parent a0a0eb6 commit 011a45a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
21 changes: 14 additions & 7 deletions providers/azure/connection/azureinstancesnapshot/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ const (

// the instance from which we're performing the scan
type azureScannerInstance struct {
instanceInfo
subscriptionId string
resourceGroup string
name string
// holds extra information about the instance, fetched via the Azure API
instanceInfo *instanceInfo
}

type assetInfo struct {
Expand Down Expand Up @@ -94,12 +98,10 @@ func determineScannerInstanceInfo(localConn *local.LocalConnection, token azcore
return nil, err
}

instanceInfo, err := InstanceInfo(resourceGrp, instanceName, subId, token)
if err != nil {
return nil, err
}
return &azureScannerInstance{
instanceInfo: instanceInfo,
subscriptionId: subId,
resourceGroup: resourceGrp,
name: instanceName,
}, nil
}

Expand Down Expand Up @@ -167,6 +169,11 @@ func NewAzureSnapshotConnection(id uint32, conf *inventory.Config, asset *invent
// 3. we either clone the target disk/snapshot and mount it
// or we skip the setup and expect the disk to be already attached
if !c.skipSetup() {
instanceInfo, err := InstanceInfo(scanner.resourceGroup, scanner.name, scanner.subscriptionId, token)
if err != nil {
return nil, err
}
c.scanner.instanceInfo = &instanceInfo
scsiDevices, err := c.listScsiDevices()
if err != nil {
c.Close()
Expand Down Expand Up @@ -327,4 +334,4 @@ func (c *AzureSnapshotConnection) Type() shared.ConnectionType {

func (c *AzureSnapshotConnection) Config() *inventory.Config {
return c.FileSystemConnection.Conf
}
}
9 changes: 6 additions & 3 deletions providers/azure/connection/azureinstancesnapshot/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func (c *AzureSnapshotConnection) setupDiskAndMount(target scanTarget, lun int32
}

func (c *AzureSnapshotConnection) setupDisk(target scanTarget) (mountedDiskInfo, assetInfo, error) {
if c.scanner.instanceInfo == nil {
return mountedDiskInfo{}, assetInfo{}, errors.New("cannot setup disk, instance info not found")
}
mi := mountedDiskInfo{}
ai := assetInfo{}
h := sha256.New()
Expand All @@ -79,7 +82,7 @@ func (c *AzureSnapshotConnection) setupDisk(target scanTarget) (mountedDiskInfo,
}

log.Debug().Str("boot disk", instanceInfo.bootDiskId).Msg("found boot disk for instance, cloning")
disk, err := c.snapshotCreator.cloneDisk(instanceInfo.bootDiskId, c.scanner.resourceGroup, diskName, c.scanner.location, c.scanner.vm.Zones)
disk, err := c.snapshotCreator.cloneDisk(instanceInfo.bootDiskId, c.scanner.resourceGroup, diskName, c.scanner.instanceInfo.location, c.scanner.instanceInfo.vm.Zones)
if err != nil {
log.Error().Err(err).Msg("could not complete disk cloning")
return mountedDiskInfo{}, assetInfo{}, errors.Wrap(err, "could not complete disk cloning")
Expand All @@ -95,7 +98,7 @@ func (c *AzureSnapshotConnection) setupDisk(target scanTarget) (mountedDiskInfo,
return mountedDiskInfo{}, assetInfo{}, err
}

disk, err := c.snapshotCreator.createSnapshotDisk(snapshotInfo.snapshotId, c.scanner.resourceGroup, diskName, c.scanner.location, c.scanner.vm.Zones)
disk, err := c.snapshotCreator.createSnapshotDisk(snapshotInfo.snapshotId, c.scanner.resourceGroup, diskName, c.scanner.instanceInfo.location, c.scanner.instanceInfo.vm.Zones)
if err != nil {
log.Error().Err(err).Msg("could not complete snapshot disk creation")
return mountedDiskInfo{}, assetInfo{}, errors.Wrap(err, "could not create disk from snapshot")
Expand All @@ -111,7 +114,7 @@ func (c *AzureSnapshotConnection) setupDisk(target scanTarget) (mountedDiskInfo,
return mountedDiskInfo{}, assetInfo{}, err
}

disk, err := c.snapshotCreator.cloneDisk(diskInfo.diskId, c.scanner.resourceGroup, diskName, c.scanner.location, c.scanner.vm.Zones)
disk, err := c.snapshotCreator.cloneDisk(diskInfo.diskId, c.scanner.resourceGroup, diskName, c.scanner.instanceInfo.location, c.scanner.instanceInfo.vm.Zones)
if err != nil {
log.Error().Err(err).Msg("could not complete disk cloning")
return mountedDiskInfo{}, assetInfo{}, errors.Wrap(err, "could not complete disk cloning")
Expand Down
11 changes: 9 additions & 2 deletions providers/azure/connection/azureinstancesnapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package azureinstancesnapshot

import (
"context"
"errors"
"time"

"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -207,7 +208,10 @@ func (sc *snapshotCreator) cloneDisk(sourceDiskId, resourceGroupName, diskName s
}

// attachDisk attaches a disk to an instance
func (sc *snapshotCreator) attachDisk(targetInstance instanceInfo, diskName, diskId string, lun int32) error {
func (sc *snapshotCreator) attachDisk(targetInstance *instanceInfo, diskName, diskId string, lun int32) error {
if targetInstance == nil {
return errors.New("targetInstance is nil, cannot attach disk")
}
ctx := context.Background()
log.Debug().Str("disk-name", diskName).Int32("LUN", lun).Msg("attach disk")
computeSvc, err := sc.computeClient()
Expand Down Expand Up @@ -257,7 +261,10 @@ func (sc *snapshotCreator) attachDisk(targetInstance instanceInfo, diskName, dis
return err
}

func (sc *snapshotCreator) detachDisk(diskName string, targetInstance instanceInfo) error {
func (sc *snapshotCreator) detachDisk(diskName string, targetInstance *instanceInfo) error {
if targetInstance == nil {
return errors.New("targetInstance is nil, cannot detach disk")
}
ctx := context.Background()
log.Debug().Str("instance-name", targetInstance.instanceName).Msg("detach disk from instance")
computeSvc, err := sc.computeClient()
Expand Down

0 comments on commit 011a45a

Please sign in to comment.