Skip to content

Commit 310168c

Browse files
committed
fix CreateVolume: search mode for Dedicated kind
1 parent 27b3681 commit 310168c

File tree

4 files changed

+58
-97
lines changed

4 files changed

+58
-97
lines changed

pkg/cloudprovider/providers/azure/azure_blobDiskController.go

Lines changed: 48 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -80,52 +80,55 @@ func newBlobDiskController(common *controllerCommon) (*BlobDiskController, error
8080
return &c, nil
8181
}
8282

83-
// CreateVolume creates a VHD blob in a given storage account, will create the given storage account if it does not exist in current resource group
83+
// CreateVolume creates a VHD blob in a storage account that has storageType and location using the given storage account.
84+
// If no storage account is given, search all the storage accounts associated with the resource group and pick one that
85+
// fits storage type and location.
8486
func (c *BlobDiskController) CreateVolume(name, storageAccount string, storageAccountType storage.SkuName, location string, requestGB int) (string, string, int, error) {
85-
key, err := c.common.cloud.getStorageAccesskey(storageAccount)
86-
if err != nil {
87-
glog.V(2).Infof("azureDisk - no key found for storage account %s in resource group %s, begin to create a new storage account", storageAccount, c.common.resourceGroup)
88-
89-
cp := storage.AccountCreateParameters{
90-
Sku: &storage.Sku{Name: storageAccountType},
91-
Tags: &map[string]*string{"created-by": to.StringPtr("azure-dd")},
92-
Location: &location}
93-
cancel := make(chan struct{})
94-
95-
_, errchan := c.common.cloud.StorageAccountClient.Create(c.common.resourceGroup, storageAccount, cp, cancel)
96-
err = <-errchan
97-
if err != nil {
98-
return "", "", 0, fmt.Errorf(fmt.Sprintf("Create Storage Account %s, error: %s", storageAccount, err))
99-
}
100-
101-
key, err = c.common.cloud.getStorageAccesskey(storageAccount)
87+
var err error
88+
accounts := []accountWithLocation{}
89+
if len(storageAccount) > 0 {
90+
accounts = append(accounts, accountWithLocation{Name: storageAccount})
91+
} else {
92+
// find a storage account
93+
accounts, err = c.common.cloud.getStorageAccounts()
10294
if err != nil {
103-
return "", "", 0, fmt.Errorf("no key found for storage account %s even after creating a new storage account", storageAccount)
95+
// TODO: create a storage account and container
96+
return "", "", 0, err
10497
}
105-
106-
glog.Errorf("no key found for storage account %s in resource group %s", storageAccount, c.common.resourceGroup)
107-
return "", "", 0, err
10898
}
99+
for _, account := range accounts {
100+
glog.V(4).Infof("account %s type %s location %s", account.Name, account.StorageType, account.Location)
101+
if (account.StorageType == string(storageAccountType)) && (location == "" || account.Location == location) {
102+
// find the access key with this account
103+
key, err := c.common.cloud.getStorageAccesskey(account.Name)
104+
if err != nil {
105+
glog.V(2).Infof("no key found for storage account %s", account.Name)
106+
continue
107+
}
109108

110-
client, err := azstorage.NewBasicClientOnSovereignCloud(storageAccount, key, c.common.cloud.Environment)
111-
if err != nil {
112-
return "", "", 0, err
113-
}
114-
blobClient := client.GetBlobService()
109+
client, err := azstorage.NewBasicClientOnSovereignCloud(account.Name, key, c.common.cloud.Environment)
110+
if err != nil {
111+
return "", "", 0, err
112+
}
113+
blobClient := client.GetBlobService()
115114

116-
container := blobClient.GetContainerReference(vhdContainerName)
117-
_, err = container.CreateIfNotExists(&azstorage.CreateContainerOptions{Access: azstorage.ContainerAccessTypePrivate})
118-
if err != nil {
119-
return "", "", 0, err
120-
}
115+
container := blobClient.GetContainerReference(vhdContainerName)
116+
_, err = container.CreateIfNotExists(&azstorage.CreateContainerOptions{Access: azstorage.ContainerAccessTypePrivate})
117+
if err != nil {
118+
return "", "", 0, err
119+
}
121120

122-
diskName, diskURI, err := c.createVHDBlobDisk(blobClient, storageAccount, name, vhdContainerName, int64(requestGB))
123-
if err != nil {
124-
return "", "", 0, err
125-
}
121+
// create a page blob in this account's vhd container
122+
diskName, diskURI, err := c.createVHDBlobDisk(blobClient, account.Name, name, vhdContainerName, int64(requestGB))
123+
if err != nil {
124+
return "", "", 0, err
125+
}
126126

127-
glog.V(4).Infof("azureDisk - created vhd blob uri: %s", diskURI)
128-
return diskName, diskURI, requestGB, err
127+
glog.V(4).Infof("azureDisk - created vhd blob uri: %s", diskURI)
128+
return diskName, diskURI, requestGB, err
129+
}
130+
}
131+
return "", "", 0, fmt.Errorf("failed to find a matching storage account")
129132
}
130133

131134
// DeleteVolume deletes a VHD blob
@@ -236,24 +239,12 @@ func (c *BlobDiskController) deleteVhdBlob(accountName, accountKey, blobName str
236239
}
237240

238241
//CreateBlobDisk : create a blob disk in a node
239-
func (c *BlobDiskController) CreateBlobDisk(dataDiskName string, storageAccountType storage.SkuName, sizeGB int, forceStandAlone bool) (string, error) {
240-
glog.V(4).Infof("azureDisk - creating blob data disk named:%s on StorageAccountType:%s StandAlone:%v", dataDiskName, storageAccountType, forceStandAlone)
242+
func (c *BlobDiskController) CreateBlobDisk(dataDiskName string, storageAccountType storage.SkuName, sizeGB int) (string, error) {
243+
glog.V(4).Infof("azureDisk - creating blob data disk named:%s on StorageAccountType:%s", dataDiskName, storageAccountType)
241244

242-
var storageAccountName = ""
243-
var err error
244-
245-
if forceStandAlone {
246-
// we have to wait until the storage account is is created
247-
storageAccountName = "p" + MakeCRC32(c.common.subscriptionID+c.common.resourceGroup+dataDiskName)
248-
err = c.createStorageAccount(storageAccountName, storageAccountType, c.common.location, false)
249-
if err != nil {
250-
return "", err
251-
}
252-
} else {
253-
storageAccountName, err = c.findSANameForDisk(storageAccountType)
254-
if err != nil {
255-
return "", err
256-
}
245+
storageAccountName, err := c.findSANameForDisk(storageAccountType)
246+
if err != nil {
247+
return "", err
257248
}
258249

259250
blobClient, err := c.getBlobSvcClient(storageAccountName)
@@ -266,15 +257,13 @@ func (c *BlobDiskController) CreateBlobDisk(dataDiskName string, storageAccountT
266257
return "", err
267258
}
268259

269-
if !forceStandAlone {
270-
atomic.AddInt32(&c.accounts[storageAccountName].diskCount, 1)
271-
}
260+
atomic.AddInt32(&c.accounts[storageAccountName].diskCount, 1)
272261

273262
return diskURI, nil
274263
}
275264

276265
//DeleteBlobDisk : delete a blob disk from a node
277-
func (c *BlobDiskController) DeleteBlobDisk(diskURI string, wasForced bool) error {
266+
func (c *BlobDiskController) DeleteBlobDisk(diskURI string) error {
278267
storageAccountName, vhdName, err := diskNameandSANameFromURI(diskURI)
279268
if err != nil {
280269
return err
@@ -286,11 +275,6 @@ func (c *BlobDiskController) DeleteBlobDisk(diskURI string, wasForced bool) erro
286275
glog.V(4).Infof("azureDisk - deleting volume %s", diskURI)
287276
return c.DeleteVolume(diskURI)
288277
}
289-
// if forced (as in one disk = one storage account)
290-
// delete the account completely
291-
if wasForced {
292-
return c.deleteStorageAccount(storageAccountName)
293-
}
294278

295279
blobSvc, err := c.getBlobSvcClient(storageAccountName)
296280
if err != nil {

pkg/volume/azure_dd/azure_dd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ import (
2828

2929
// interface exposed by the cloud provider implementing Disk functionlity
3030
type DiskController interface {
31-
CreateBlobDisk(dataDiskName string, storageAccountType storage.SkuName, sizeGB int, forceStandAlone bool) (string, error)
32-
DeleteBlobDisk(diskUri string, wasForced bool) error
31+
CreateBlobDisk(dataDiskName string, storageAccountType storage.SkuName, sizeGB int) (string, error)
32+
DeleteBlobDisk(diskUri string) error
3333

3434
CreateManagedDisk(diskName string, storageAccountType storage.SkuName, sizeGB int, tags map[string]string) (string, error)
3535
DeleteManagedDisk(diskURI string) error

pkg/volume/azure_dd/azure_provision.go

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,13 @@ func (d *azureDiskDeleter) Delete() error {
5555
return err
5656
}
5757

58-
wasStandAlone := (*volumeSource.Kind != v1.AzureSharedBlobDisk)
5958
managed := (*volumeSource.Kind == v1.AzureManagedDisk)
6059

6160
if managed {
6261
return diskController.DeleteManagedDisk(volumeSource.DataDiskURI)
6362
}
6463

65-
return diskController.DeleteBlobDisk(volumeSource.DataDiskURI, wasStandAlone)
64+
return diskController.DeleteBlobDisk(volumeSource.DataDiskURI)
6665
}
6766

6867
func (p *azureDiskProvisioner) Provision() (*v1.PersistentVolume, error) {
@@ -149,26 +148,13 @@ func (p *azureDiskProvisioner) Provision() (*v1.PersistentVolume, error) {
149148
return nil, err
150149
}
151150
} else {
152-
forceStandAlone := (kind == v1.AzureDedicatedBlobDisk)
153151
if kind == v1.AzureDedicatedBlobDisk {
154-
if location != "" && account != "" {
155-
// use dedicated kind (by default) for compatibility
156-
_, diskURI, _, err = diskController.CreateVolume(name, account, skuName, location, requestGB)
157-
if err != nil {
158-
return nil, err
159-
}
160-
} else {
161-
if location != "" || account != "" {
162-
return nil, fmt.Errorf("AzureDisk - location(%s) and account(%s) must be both empty or specified for dedicated kind, only one value specified is not allowed",
163-
location, account)
164-
}
165-
diskURI, err = diskController.CreateBlobDisk(name, skuName, requestGB, forceStandAlone)
166-
if err != nil {
167-
return nil, err
168-
}
152+
_, diskURI, _, err = diskController.CreateVolume(name, account, skuName, location, requestGB)
153+
if err != nil {
154+
return nil, err
169155
}
170156
} else {
171-
diskURI, err = diskController.CreateBlobDisk(name, skuName, requestGB, forceStandAlone)
157+
diskURI, err = diskController.CreateBlobDisk(name, skuName, requestGB)
172158
if err != nil {
173159
return nil, err
174160
}

test/e2e/framework/pv_util.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -718,16 +718,11 @@ func createPD(zone string) (string, error) {
718718
return "", err
719719
}
720720

721-
if azureCloud.BlobDiskController == nil {
722-
return "", fmt.Errorf("BlobDiskController is nil, it's not expected.")
723-
}
724-
725-
diskUri, err := azureCloud.BlobDiskController.CreateBlobDisk(pdName, "standard_lrs", 1, false)
721+
_, diskURI, _, err := azureCloud.CreateVolume(pdName, "" /* account */, "" /* sku */, "" /* location */, 1 /* sizeGb */)
726722
if err != nil {
727723
return "", err
728724
}
729-
730-
return diskUri, nil
725+
return diskURI, nil
731726
} else {
732727
return "", fmt.Errorf("provider does not support volume creation")
733728
}
@@ -772,11 +767,7 @@ func deletePD(pdName string) error {
772767
if err != nil {
773768
return err
774769
}
775-
if azureCloud.BlobDiskController == nil {
776-
return fmt.Errorf("BlobDiskController is nil, it's not expected.")
777-
}
778-
diskName := pdName[(strings.LastIndex(pdName, "/") + 1):]
779-
err = azureCloud.BlobDiskController.DeleteBlobDisk(diskName, false)
770+
err = azureCloud.DeleteVolume(pdName)
780771
if err != nil {
781772
Logf("failed to delete Azure volume %q: %v", pdName, err)
782773
return err

0 commit comments

Comments
 (0)