Skip to content

Commit

Permalink
Merge pull request #68183 from andyzhangx/automated-cherry-pick-of-#6…
Browse files Browse the repository at this point in the history
…8117-upstream-release-1.10

Automatic merge from submit-queue.

Automated cherry pick of #68117: support cross resource group for azure file

Cherry pick of #68117 on release-1.10.

#68117: support cross resource group for azure file
  • Loading branch information
Kubernetes Submit Queue committed Sep 6, 2018
2 parents cb1d872 + 98cb2cd commit 5351fb2
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 19 deletions.
4 changes: 2 additions & 2 deletions pkg/cloudprovider/providers/azure/azure_blobDiskController.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func newBlobDiskController(common *controllerCommon) (*BlobDiskController, error
// If no storage account is given, search all the storage accounts associated with the resource group and pick one that
// fits storage type and location.
func (c *BlobDiskController) CreateVolume(blobName, accountName, accountType, location string, requestGB int) (string, string, int, error) {
account, key, err := c.common.cloud.ensureStorageAccount(accountName, accountType, location, dedicatedDiskAccountNamePrefix)
account, key, err := c.common.cloud.ensureStorageAccount(accountName, accountType, c.common.resourceGroup, location, dedicatedDiskAccountNamePrefix)
if err != nil {
return "", "", 0, fmt.Errorf("could not get storage key for storage account %s: %v", accountName, err)
}
Expand Down Expand Up @@ -107,7 +107,7 @@ func (c *BlobDiskController) DeleteVolume(diskURI string) error {
if err != nil {
return fmt.Errorf("failed to parse vhd URI %v", err)
}
key, err := c.common.cloud.getStorageAccesskey(accountName)
key, err := c.common.cloud.getStorageAccesskey(accountName, c.common.resourceGroup)
if err != nil {
return fmt.Errorf("no key for storage account %s, err %v", accountName, err)
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/cloudprovider/providers/azure/azure_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ const (
)

// CreateFileShare creates a file share, using a matching storage account
func (az *Cloud) CreateFileShare(shareName, accountName, accountType, location string, requestGiB int) (string, string, error) {
account, key, err := az.ensureStorageAccount(accountName, accountType, location, fileShareAccountNamePrefix)
func (az *Cloud) CreateFileShare(shareName, accountName, accountType, resourceGroup, location string, requestGiB int) (string, string, error) {
if resourceGroup == "" {
resourceGroup = az.resourceGroup
}

account, key, err := az.ensureStorageAccount(accountName, accountType, resourceGroup, location, fileShareAccountNamePrefix)
if err != nil {
return "", "", fmt.Errorf("could not get storage key for storage account %s: %v", accountName, err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloudprovider/providers/azure/azure_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func TestCreateFileShare(t *testing.T) {
fake.Keys = test.keys
fake.Err = test.err

account, key, err := cloud.CreateFileShare(test.name, test.acct, test.acctType, test.loc, test.gb)
account, key, err := cloud.CreateFileShare(test.name, test.acct, test.acctType, "rg", test.loc, test.gb)
if test.expectErr && err == nil {
t.Errorf("unexpected non-error")
continue
Expand Down
20 changes: 10 additions & 10 deletions pkg/cloudprovider/providers/azure/azure_storageaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ type accountWithLocation struct {
}

// getStorageAccounts gets name, type, location of all storage accounts in a resource group which matches matchingAccountType, matchingLocation
func (az *Cloud) getStorageAccounts(matchingAccountType, matchingLocation string) ([]accountWithLocation, error) {
result, err := az.StorageAccountClient.ListByResourceGroup(az.ResourceGroup)
func (az *Cloud) getStorageAccounts(matchingAccountType, resourceGroup, matchingLocation string) ([]accountWithLocation, error) {
result, err := az.StorageAccountClient.ListByResourceGroup(resourceGroup)
if err != nil {
return nil, err
}
if result.Value == nil {
return nil, fmt.Errorf("unexpected error when listing storage accounts from resource group %s", az.ResourceGroup)
return nil, fmt.Errorf("unexpected error when listing storage accounts from resource group %s", resourceGroup)
}

accounts := []accountWithLocation{}
Expand All @@ -59,8 +59,8 @@ func (az *Cloud) getStorageAccounts(matchingAccountType, matchingLocation string
}

// getStorageAccesskey gets the storage account access key
func (az *Cloud) getStorageAccesskey(account string) (string, error) {
result, err := az.StorageAccountClient.ListKeys(az.ResourceGroup, account)
func (az *Cloud) getStorageAccesskey(account, resourceGroup string) (string, error) {
result, err := az.StorageAccountClient.ListKeys(resourceGroup, account)
if err != nil {
return "", err
}
Expand All @@ -81,10 +81,10 @@ func (az *Cloud) getStorageAccesskey(account string) (string, error) {
}

// ensureStorageAccount search storage account, create one storage account(with genAccountNamePrefix) if not found, return accountName, accountKey
func (az *Cloud) ensureStorageAccount(accountName, accountType, location, genAccountNamePrefix string) (string, string, error) {
func (az *Cloud) ensureStorageAccount(accountName, accountType, resourceGroup, location, genAccountNamePrefix string) (string, string, error) {
if len(accountName) == 0 {
// find a storage account that matches accountType
accounts, err := az.getStorageAccounts(accountType, location)
accounts, err := az.getStorageAccounts(accountType, resourceGroup, location)
if err != nil {
return "", "", fmt.Errorf("could not list storage accounts for account type %s: %v", accountType, err)
}
Expand All @@ -105,15 +105,15 @@ func (az *Cloud) ensureStorageAccount(accountName, accountType, location, genAcc
}

glog.V(2).Infof("azure - no matching account found, begin to create a new account %s in resource group %s, location: %s, accountType: %s",
accountName, az.ResourceGroup, location, accountType)
accountName, resourceGroup, location, accountType)
cp := storage.AccountCreateParameters{
Sku: &storage.Sku{Name: storage.SkuName(accountType)},
Tags: &map[string]*string{"created-by": to.StringPtr("azure")},
AccountPropertiesCreateParameters: &storage.AccountPropertiesCreateParameters{EnableHTTPSTrafficOnly: to.BoolPtr(true)},
Location: &location}
cancel := make(chan struct{})

_, errchan := az.StorageAccountClient.Create(az.ResourceGroup, accountName, cp, cancel)
_, errchan := az.StorageAccountClient.Create(resourceGroup, accountName, cp, cancel)
err := <-errchan
if err != nil {
return "", "", fmt.Errorf(fmt.Sprintf("Failed to create storage account %s, error: %s", accountName, err))
Expand All @@ -122,7 +122,7 @@ func (az *Cloud) ensureStorageAccount(accountName, accountType, location, genAcc
}

// find the access key with this account
accountKey, err := az.getStorageAccesskey(accountName)
accountKey, err := az.getStorageAccesskey(accountName, resourceGroup)
if err != nil {
return "", "", fmt.Errorf("could not get storage key for storage account %s: %v", accountName, err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestGetStorageAccessKeys(t *testing.T) {
expectedKey := test.expectedKey
fake.Keys = test.results
fake.Err = test.err
key, err := cloud.getStorageAccesskey("acct")
key, err := cloud.getStorageAccesskey("acct", "rg")
if test.expectErr && err == nil {
t.Errorf("Unexpected non-error")
continue
Expand Down
8 changes: 5 additions & 3 deletions pkg/volume/azure_file/azure_provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var _ volume.ProvisionableVolumePlugin = &azureFilePlugin{}
// azure cloud provider should implement it
type azureCloudProvider interface {
// create a file share
CreateFileShare(shareName, accountName, accountType, location string, requestGiB int) (string, string, error)
CreateFileShare(shareName, accountName, accountType, resourceGroup, location string, requestGiB int) (string, string, error)
// delete a file share
DeleteFileShare(accountName, accountKey, shareName string) error
// resize a file share
Expand Down Expand Up @@ -136,7 +136,7 @@ func (a *azureFileProvisioner) Provision() (*v1.PersistentVolume, error) {
return nil, fmt.Errorf("invalid AccessModes %v: only AccessModes %v are supported", a.options.PVC.Spec.AccessModes, a.plugin.GetAccessModes())
}

var sku, location, account string
var sku, resourceGroup, location, account string

// File share name has a length limit of 63, and it cannot contain two consecutive '-'s.
name := util.GenerateVolumeName(a.options.ClusterName, a.options.PVName, 63)
Expand All @@ -157,6 +157,8 @@ func (a *azureFileProvisioner) Provision() (*v1.PersistentVolume, error) {
account = v
case "secretnamespace":
secretNamespace = v
case "resourcegroup":
resourceGroup = v
default:
return nil, fmt.Errorf("invalid option %q for volume plugin %s", k, a.plugin.GetPluginName())
}
Expand All @@ -166,7 +168,7 @@ func (a *azureFileProvisioner) Provision() (*v1.PersistentVolume, error) {
return nil, fmt.Errorf("claim.Spec.Selector is not supported for dynamic provisioning on Azure file")
}

account, key, err := a.azureProvider.CreateFileShare(name, account, sku, location, requestGiB)
account, key, err := a.azureProvider.CreateFileShare(name, account, sku, resourceGroup, location, requestGiB)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 5351fb2

Please sign in to comment.