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

feat: add PickRandomMatchingAccount in account search #3811

Merged
merged 3 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/provider/azure_managedDiskController.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ type ManagedDiskOptions struct {
LogicalSectorSize int32
// SkipGetDiskOperation indicates whether skip GetDisk operation(mainly due to throttling)
SkipGetDiskOperation bool
// PublicNetworkAccess - Possible values include: 'Enabled', 'Disabled'
PublicNetworkAccess compute.PublicNetworkAccess
// NetworkAccessPolicy - Possible values include: 'AllowAll', 'AllowPrivate', 'DenyAll'
NetworkAccessPolicy compute.NetworkAccessPolicy
// DiskAccessID - ARM id of the DiskAccess resource for using private endpoints on disks.
Expand Down Expand Up @@ -139,6 +141,10 @@ func (c *ManagedDiskController) CreateManagedDisk(ctx context.Context, options *
BurstingEnabled: options.BurstingEnabled,
}

if options.PublicNetworkAccess != "" {
diskProperties.PublicNetworkAccess = options.PublicNetworkAccess
}

if options.NetworkAccessPolicy != "" {
diskProperties.NetworkAccessPolicy = options.NetworkAccessPolicy
if options.NetworkAccessPolicy == compute.AllowPrivate {
Expand Down
4 changes: 4 additions & 0 deletions pkg/provider/azure_managedDiskController_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func TestCreateManagedDisk(t *testing.T) {
diskEncryptionType string
subscriptionID string
resouceGroup string
publicNetworkAccess compute.PublicNetworkAccess
networkAccessPolicy compute.NetworkAccessPolicy
diskAccessID *string
expectedDiskID string
Expand Down Expand Up @@ -204,6 +205,7 @@ func TestCreateManagedDisk(t *testing.T) {
storageAccountType: compute.StandardLRS,
diskEncryptionSetID: goodDiskEncryptionSetID,
networkAccessPolicy: compute.DenyAll,
publicNetworkAccess: compute.Disabled,
expectedDiskID: disk1ID,
existedDisk: compute.Disk{ID: pointer.String(disk1ID), Name: pointer.String(disk1Name), DiskProperties: &compute.DiskProperties{Encryption: &compute.Encryption{DiskEncryptionSetID: &goodDiskEncryptionSetID, Type: compute.EncryptionTypeEncryptionAtRestWithCustomerKey}, ProvisioningState: pointer.String("Succeeded")}, Tags: testTags},
expectedErr: false,
Expand All @@ -216,6 +218,7 @@ func TestCreateManagedDisk(t *testing.T) {
diskEncryptionSetID: goodDiskEncryptionSetID,
diskEncryptionType: "EncryptionAtRestWithCustomerKey",
networkAccessPolicy: compute.AllowAll,
publicNetworkAccess: compute.Enabled,
expectedDiskID: disk1ID,
existedDisk: compute.Disk{ID: pointer.String(disk1ID), Name: pointer.String(disk1Name), DiskProperties: &compute.DiskProperties{Encryption: &compute.Encryption{DiskEncryptionSetID: &goodDiskEncryptionSetID, Type: compute.EncryptionTypeEncryptionAtRestWithCustomerKey}, ProvisioningState: pointer.String("Succeeded")}, Tags: testTags},
expectedErr: false,
Expand Down Expand Up @@ -274,6 +277,7 @@ func TestCreateManagedDisk(t *testing.T) {
DiskEncryptionType: test.diskEncryptionType,
MaxShares: maxShare,
NetworkAccessPolicy: test.networkAccessPolicy,
PublicNetworkAccess: test.publicNetworkAccess,
DiskAccessID: test.diskAccessID,
SubscriptionID: test.subscriptionID,
}
Expand Down
16 changes: 15 additions & 1 deletion pkg/provider/azure_storageaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package provider

import (
"context"
"crypto/rand"
"fmt"
"math/big"
"strings"

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2022-07-01/network"
Expand Down Expand Up @@ -75,6 +77,8 @@ type AccountOptions struct {
EnableBlobVersioning *bool
SoftDeleteBlobs int32
SoftDeleteContainers int32
// indicate whether to get a random matching account, if false, will get the first matching account
PickRandomMatchingAccount bool
}

type accountWithLocation struct {
Expand Down Expand Up @@ -212,7 +216,17 @@ func (az *Cloud) EnsureStorageAccount(ctx context.Context, accountOptions *Accou
}

if len(accounts) > 0 {
accountName = accounts[0].Name
index := 0
if accountOptions.PickRandomMatchingAccount {
// randomly pick one matching account
n, err := rand.Int(rand.Reader, big.NewInt(int64(len(accounts))))
andyzhangx marked this conversation as resolved.
Show resolved Hide resolved
if err != nil || n == nil {
return "", "", err
}
index = int(n.Int64())
klog.V(4).Infof("randomly pick one matching account, index: %d", index)
}
accountName = accounts[index].Name
createNewAccount = false
klog.V(4).Infof("found a matching account %s type %s location %s", accounts[0].Name, accounts[0].StorageType, accounts[0].Location)
}
Expand Down
23 changes: 13 additions & 10 deletions pkg/provider/azure_storageaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ func TestEnsureStorageAccount(t *testing.T) {
SubnetPropertiesFormatNil bool
mockStorageAccountsClient bool
setAccountOptions bool
pickRandomMatchingAccount bool
accessTier string
storageType StorageType
requireInfrastructureEncryption *bool
Expand All @@ -393,6 +394,7 @@ func TestEnsureStorageAccount(t *testing.T) {
createPrivateEndpoint: true,
mockStorageAccountsClient: true,
setAccountOptions: true,
pickRandomMatchingAccount: true,
storageType: StorageTypeBlob,
requireInfrastructureEncryption: pointer.Bool(true),
keyVaultURL: pointer.String("keyVaultURL"),
Expand Down Expand Up @@ -502,16 +504,17 @@ func TestEnsureStorageAccount(t *testing.T) {
var testAccountOptions *AccountOptions
if test.setAccountOptions {
testAccountOptions = &AccountOptions{
ResourceGroup: test.resourceGroup,
CreatePrivateEndpoint: test.createPrivateEndpoint,
Name: test.accountName,
CreateAccount: test.createAccount,
SubscriptionID: test.subscriptionID,
AccessTier: test.accessTier,
StorageType: test.storageType,
EnableBlobVersioning: pointer.Bool(true),
SoftDeleteBlobs: 7,
SoftDeleteContainers: 7,
ResourceGroup: test.resourceGroup,
CreatePrivateEndpoint: test.createPrivateEndpoint,
Name: test.accountName,
CreateAccount: test.createAccount,
SubscriptionID: test.subscriptionID,
AccessTier: test.accessTier,
StorageType: test.storageType,
EnableBlobVersioning: pointer.Bool(true),
SoftDeleteBlobs: 7,
SoftDeleteContainers: 7,
PickRandomMatchingAccount: test.pickRandomMatchingAccount,
}
}

Expand Down