Skip to content

Commit 4e3b4a7

Browse files
committed
support cross resource group for azure file
fix test failure
1 parent c1e37a5 commit 4e3b4a7

File tree

6 files changed

+25
-19
lines changed

6 files changed

+25
-19
lines changed

pkg/cloudprovider/providers/azure/azure_blobDiskController.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func newBlobDiskController(common *controllerCommon) (*BlobDiskController, error
8080
// If no storage account is given, search all the storage accounts associated with the resource group and pick one that
8181
// fits storage type and location.
8282
func (c *BlobDiskController) CreateVolume(blobName, accountName, accountType, location string, requestGB int) (string, string, int, error) {
83-
account, key, err := c.common.cloud.ensureStorageAccount(accountName, accountType, location, dedicatedDiskAccountNamePrefix)
83+
account, key, err := c.common.cloud.ensureStorageAccount(accountName, accountType, c.common.resourceGroup, location, dedicatedDiskAccountNamePrefix)
8484
if err != nil {
8585
return "", "", 0, fmt.Errorf("could not get storage key for storage account %s: %v", accountName, err)
8686
}
@@ -108,7 +108,7 @@ func (c *BlobDiskController) DeleteVolume(diskURI string) error {
108108
if err != nil {
109109
return fmt.Errorf("failed to parse vhd URI %v", err)
110110
}
111-
key, err := c.common.cloud.getStorageAccesskey(accountName)
111+
key, err := c.common.cloud.getStorageAccesskey(accountName, c.common.resourceGroup)
112112
if err != nil {
113113
return fmt.Errorf("no key for storage account %s, err %v", accountName, err)
114114
}

pkg/cloudprovider/providers/azure/azure_storage.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@ const (
3131
)
3232

3333
// CreateFileShare creates a file share, using a matching storage account
34-
func (az *Cloud) CreateFileShare(shareName, accountName, accountType, location string, requestGiB int) (string, string, error) {
35-
account, key, err := az.ensureStorageAccount(accountName, accountType, location, fileShareAccountNamePrefix)
34+
func (az *Cloud) CreateFileShare(shareName, accountName, accountType, resourceGroup, location string, requestGiB int) (string, string, error) {
35+
if resourceGroup == "" {
36+
resourceGroup = az.resourceGroup
37+
}
38+
39+
account, key, err := az.ensureStorageAccount(accountName, accountType, resourceGroup, location, fileShareAccountNamePrefix)
3640
if err != nil {
3741
return "", "", fmt.Errorf("could not get storage key for storage account %s: %v", accountName, err)
3842
}

pkg/cloudprovider/providers/azure/azure_storage_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func TestCreateFileShare(t *testing.T) {
116116
fake.Keys = test.keys
117117
fake.Err = test.err
118118

119-
account, key, err := cloud.CreateFileShare(test.name, test.acct, test.acctType, test.loc, test.gb)
119+
account, key, err := cloud.CreateFileShare(test.name, test.acct, test.acctType, "rg", test.loc, test.gb)
120120
if test.expectErr && err == nil {
121121
t.Errorf("unexpected non-error")
122122
continue

pkg/cloudprovider/providers/azure/azure_storageaccount.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ type accountWithLocation struct {
3030
}
3131

3232
// getStorageAccounts gets name, type, location of all storage accounts in a resource group which matches matchingAccountType, matchingLocation
33-
func (az *Cloud) getStorageAccounts(matchingAccountType, matchingLocation string) ([]accountWithLocation, error) {
33+
func (az *Cloud) getStorageAccounts(matchingAccountType, resourceGroup, matchingLocation string) ([]accountWithLocation, error) {
3434
ctx, cancel := getContextWithCancel()
3535
defer cancel()
36-
result, err := az.StorageAccountClient.ListByResourceGroup(ctx, az.ResourceGroup)
36+
result, err := az.StorageAccountClient.ListByResourceGroup(ctx, resourceGroup)
3737
if err != nil {
3838
return nil, err
3939
}
4040
if result.Value == nil {
41-
return nil, fmt.Errorf("unexpected error when listing storage accounts from resource group %s", az.ResourceGroup)
41+
return nil, fmt.Errorf("unexpected error when listing storage accounts from resource group %s", resourceGroup)
4242
}
4343

4444
accounts := []accountWithLocation{}
@@ -61,11 +61,11 @@ func (az *Cloud) getStorageAccounts(matchingAccountType, matchingLocation string
6161
}
6262

6363
// getStorageAccesskey gets the storage account access key
64-
func (az *Cloud) getStorageAccesskey(account string) (string, error) {
64+
func (az *Cloud) getStorageAccesskey(account, resourceGroup string) (string, error) {
6565
ctx, cancel := getContextWithCancel()
6666
defer cancel()
6767

68-
result, err := az.StorageAccountClient.ListKeys(ctx, az.ResourceGroup, account)
68+
result, err := az.StorageAccountClient.ListKeys(ctx, resourceGroup, account)
6969
if err != nil {
7070
return "", err
7171
}
@@ -86,10 +86,10 @@ func (az *Cloud) getStorageAccesskey(account string) (string, error) {
8686
}
8787

8888
// ensureStorageAccount search storage account, create one storage account(with genAccountNamePrefix) if not found, return accountName, accountKey
89-
func (az *Cloud) ensureStorageAccount(accountName, accountType, location, genAccountNamePrefix string) (string, string, error) {
89+
func (az *Cloud) ensureStorageAccount(accountName, accountType, resourceGroup, location, genAccountNamePrefix string) (string, string, error) {
9090
if len(accountName) == 0 {
9191
// find a storage account that matches accountType
92-
accounts, err := az.getStorageAccounts(accountType, location)
92+
accounts, err := az.getStorageAccounts(accountType, resourceGroup, location)
9393
if err != nil {
9494
return "", "", fmt.Errorf("could not list storage accounts for account type %s: %v", accountType, err)
9595
}
@@ -110,7 +110,7 @@ func (az *Cloud) ensureStorageAccount(accountName, accountType, location, genAcc
110110
}
111111

112112
glog.V(2).Infof("azure - no matching account found, begin to create a new account %s in resource group %s, location: %s, accountType: %s",
113-
accountName, az.ResourceGroup, location, accountType)
113+
accountName, resourceGroup, location, accountType)
114114
cp := storage.AccountCreateParameters{
115115
Sku: &storage.Sku{Name: storage.SkuName(accountType)},
116116
// switch to use StorageV2 as it's recommended according to https://docs.microsoft.com/en-us/azure/storage/common/storage-account-options
@@ -121,15 +121,15 @@ func (az *Cloud) ensureStorageAccount(accountName, accountType, location, genAcc
121121

122122
ctx, cancel := getContextWithCancel()
123123
defer cancel()
124-
_, err := az.StorageAccountClient.Create(ctx, az.ResourceGroup, accountName, cp)
124+
_, err := az.StorageAccountClient.Create(ctx, resourceGroup, accountName, cp)
125125
if err != nil {
126126
return "", "", fmt.Errorf(fmt.Sprintf("Failed to create storage account %s, error: %s", accountName, err))
127127
}
128128
}
129129
}
130130

131131
// find the access key with this account
132-
accountKey, err := az.getStorageAccesskey(accountName)
132+
accountKey, err := az.getStorageAccesskey(accountName, resourceGroup)
133133
if err != nil {
134134
return "", "", fmt.Errorf("could not get storage key for storage account %s: %v", accountName, err)
135135
}

pkg/cloudprovider/providers/azure/azure_storageaccount_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func TestGetStorageAccessKeys(t *testing.T) {
6464
expectedKey := test.expectedKey
6565
fake.Keys = test.results
6666
fake.Err = test.err
67-
key, err := cloud.getStorageAccesskey("acct")
67+
key, err := cloud.getStorageAccesskey("acct", "rg")
6868
if test.expectErr && err == nil {
6969
t.Errorf("Unexpected non-error")
7070
continue

pkg/volume/azure_file/azure_provision.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ var _ volume.ProvisionableVolumePlugin = &azureFilePlugin{}
3838
// azure cloud provider should implement it
3939
type azureCloudProvider interface {
4040
// create a file share
41-
CreateFileShare(shareName, accountName, accountType, location string, requestGiB int) (string, string, error)
41+
CreateFileShare(shareName, accountName, accountType, resourceGroup, location string, requestGiB int) (string, string, error)
4242
// delete a file share
4343
DeleteFileShare(accountName, accountKey, shareName string) error
4444
// resize a file share
@@ -139,7 +139,7 @@ func (a *azureFileProvisioner) Provision(selectedNode *v1.Node, allowedTopologie
139139
return nil, fmt.Errorf("%s does not support block volume provisioning", a.plugin.GetPluginName())
140140
}
141141

142-
var sku, location, account string
142+
var sku, resourceGroup, location, account string
143143

144144
// File share name has a length limit of 63, and it cannot contain two consecutive '-'s.
145145
name := util.GenerateVolumeName(a.options.ClusterName, a.options.PVName, 63)
@@ -160,6 +160,8 @@ func (a *azureFileProvisioner) Provision(selectedNode *v1.Node, allowedTopologie
160160
account = v
161161
case "secretnamespace":
162162
secretNamespace = v
163+
case "resourcegroup":
164+
resourceGroup = v
163165
default:
164166
return nil, fmt.Errorf("invalid option %q for volume plugin %s", k, a.plugin.GetPluginName())
165167
}
@@ -169,7 +171,7 @@ func (a *azureFileProvisioner) Provision(selectedNode *v1.Node, allowedTopologie
169171
return nil, fmt.Errorf("claim.Spec.Selector is not supported for dynamic provisioning on Azure file")
170172
}
171173

172-
account, key, err := a.azureProvider.CreateFileShare(name, account, sku, location, requestGiB)
174+
account, key, err := a.azureProvider.CreateFileShare(name, account, sku, resourceGroup, location, requestGiB)
173175
if err != nil {
174176
return nil, err
175177
}

0 commit comments

Comments
 (0)