-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🧹 Rework azure snapshot connection to use device connection. Drop all…
… flags that were used as a temporary workaround. (#4085) Signed-off-by: Preslav <[email protected]>
- Loading branch information
1 parent
8f5e32b
commit 9714899
Showing
13 changed files
with
318 additions
and
637 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
providers/azure/connection/azureinstancesnapshot/instance.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright (c) Mondoo, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package azureinstancesnapshot | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azcore" | ||
compute "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute" | ||
"github.com/cockroachdb/errors" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
type instanceInfo struct { | ||
subscriptionId string | ||
resourceGroup string | ||
instanceName string | ||
location string | ||
bootDiskId string | ||
zones []*string | ||
// Attach the entire VM response as well | ||
vm compute.VirtualMachine | ||
} | ||
|
||
func (s *instanceInfo) getFirstAvailableLun() (int, error) { | ||
takenLuns := []int{} | ||
|
||
if s.vm.Properties.StorageProfile == nil { | ||
return 0, errors.New("instance storage profile not found") | ||
} | ||
if s.vm.Properties.StorageProfile.DataDisks == nil { | ||
return 0, errors.New("instance data disks not found") | ||
} | ||
|
||
for _, disk := range s.vm.Properties.StorageProfile.DataDisks { | ||
takenLuns = append(takenLuns, int(*disk.Lun)) | ||
} | ||
|
||
availableLuns := []int{} | ||
for i := 0; i < 64; i++ { | ||
// exclude the taken LUNs | ||
available := true | ||
for _, d := range takenLuns { | ||
if i == d { | ||
available = false | ||
break | ||
} | ||
} | ||
if available { | ||
availableLuns = append(availableLuns, i) | ||
} else { | ||
// log just for visibility | ||
log.Debug().Int("LUN", i).Msg("azure snapshot> LUN is taken, skipping") | ||
} | ||
} | ||
if len(availableLuns) == 0 { | ||
return 0, errors.New("no available LUNs") | ||
} | ||
return availableLuns[0], nil | ||
} | ||
|
||
func GetInstanceInfo(resourceGroup, instanceName, subId string, token azcore.TokenCredential) (instanceInfo, error) { | ||
ctx := context.Background() | ||
ii := instanceInfo{} | ||
|
||
computeSvc, err := computeClient(token, subId, nil) | ||
if err != nil { | ||
return ii, err | ||
} | ||
|
||
instance, err := computeSvc.Get(ctx, resourceGroup, instanceName, &compute.VirtualMachinesClientGetOptions{}) | ||
if err != nil { | ||
return ii, err | ||
} | ||
ii.resourceGroup = resourceGroup | ||
ii.instanceName = *instance.Name | ||
ii.bootDiskId = *instance.Properties.StorageProfile.OSDisk.ManagedDisk.ID | ||
ii.location = *instance.Location | ||
ii.subscriptionId = subId | ||
ii.zones = instance.Zones | ||
ii.vm = instance.VirtualMachine | ||
return ii, nil | ||
} |
71 changes: 71 additions & 0 deletions
71
providers/azure/connection/azureinstancesnapshot/instance_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (c) Mondoo, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package azureinstancesnapshot | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to" | ||
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetFirstAvailableLun(t *testing.T) { | ||
t.Run("no luns available", func(t *testing.T) { | ||
instanceInfo := &instanceInfo{ | ||
vm: armcompute.VirtualMachine{ | ||
Properties: &armcompute.VirtualMachineProperties{ | ||
StorageProfile: &armcompute.StorageProfile{ | ||
DataDisks: []*armcompute.DataDisk{}, | ||
}, | ||
}, | ||
}, | ||
} | ||
// fill in all available LUNs for the scanner | ||
for i := int32(0); i < 64; i++ { | ||
instanceInfo.vm.Properties.StorageProfile.DataDisks = append(instanceInfo.vm.Properties.StorageProfile.DataDisks, &armcompute.DataDisk{ | ||
Lun: to.Ptr(i), | ||
}) | ||
} | ||
|
||
_, err := instanceInfo.getFirstAvailableLun() | ||
require.Error(t, err) | ||
}) | ||
t.Run("first available lun on a scanner with no disks", func(t *testing.T) { | ||
instanceInfo := instanceInfo{ | ||
vm: armcompute.VirtualMachine{ | ||
Properties: &armcompute.VirtualMachineProperties{ | ||
StorageProfile: &armcompute.StorageProfile{ | ||
DataDisks: []*armcompute.DataDisk{}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
lun, err := instanceInfo.getFirstAvailableLun() | ||
require.NoError(t, err) | ||
require.Equal(t, 0, lun) | ||
}) | ||
t.Run("first available lun on a scanner with some disks", func(t *testing.T) { | ||
instanceInfo := instanceInfo{ | ||
vm: armcompute.VirtualMachine{ | ||
Properties: &armcompute.VirtualMachineProperties{ | ||
StorageProfile: &armcompute.StorageProfile{ | ||
DataDisks: []*armcompute.DataDisk{}, | ||
}, | ||
}, | ||
}, | ||
} | ||
// fill in 15 luns | ||
for i := int32(0); i < 16; i++ { | ||
instanceInfo.vm.Properties.StorageProfile.DataDisks = append(instanceInfo.vm.Properties.StorageProfile.DataDisks, &armcompute.DataDisk{ | ||
Lun: to.Ptr(i), | ||
}) | ||
} | ||
|
||
lun, err := instanceInfo.getFirstAvailableLun() | ||
require.NoError(t, err) | ||
require.Equal(t, 16, lun) | ||
}) | ||
} |
193 changes: 0 additions & 193 deletions
193
providers/azure/connection/azureinstancesnapshot/lun.go
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.