Skip to content

Commit 760cc6f

Browse files
committed
search by accounttype in CreateVolume func
fix review comments
1 parent 310168c commit 760cc6f

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

pkg/cloudprovider/providers/azure/azure_blobDiskController.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@ type BlobDiskController struct {
5959
accounts map[string]*storageAccountState
6060
}
6161

62-
var defaultContainerName = ""
63-
var storageAccountNamePrefix = ""
64-
var storageAccountNameMatch = ""
65-
66-
var accountsLock = &sync.Mutex{}
62+
var (
63+
defaultContainerName = ""
64+
storageAccountNamePrefix = ""
65+
storageAccountNameMatch = ""
66+
accountsLock = &sync.Mutex{}
67+
)
6768

6869
func newBlobDiskController(common *controllerCommon) (*BlobDiskController, error) {
6970
c := BlobDiskController{common: common}
@@ -83,7 +84,7 @@ func newBlobDiskController(common *controllerCommon) (*BlobDiskController, error
8384
// CreateVolume creates a VHD blob in a storage account that has storageType and location using the given storage account.
8485
// If no storage account is given, search all the storage accounts associated with the resource group and pick one that
8586
// fits storage type and location.
86-
func (c *BlobDiskController) CreateVolume(name, storageAccount string, storageAccountType storage.SkuName, location string, requestGB int) (string, string, int, error) {
87+
func (c *BlobDiskController) CreateVolume(name, storageAccount, storageAccountType, location string, requestGB int) (string, string, int, error) {
8788
var err error
8889
accounts := []accountWithLocation{}
8990
if len(storageAccount) > 0 {
@@ -98,7 +99,7 @@ func (c *BlobDiskController) CreateVolume(name, storageAccount string, storageAc
9899
}
99100
for _, account := range accounts {
100101
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+
if (storageAccountType == "" || account.StorageType == storageAccountType) && (location == "" || account.Location == location) || len(storageAccount) > 0 {
102103
// find the access key with this account
103104
key, err := c.common.cloud.getStorageAccesskey(account.Name)
104105
if err != nil {
@@ -112,12 +113,6 @@ func (c *BlobDiskController) CreateVolume(name, storageAccount string, storageAc
112113
}
113114
blobClient := client.GetBlobService()
114115

115-
container := blobClient.GetContainerReference(vhdContainerName)
116-
_, err = container.CreateIfNotExists(&azstorage.CreateContainerOptions{Access: azstorage.ContainerAccessTypePrivate})
117-
if err != nil {
118-
return "", "", 0, err
119-
}
120-
121116
// create a page blob in this account's vhd container
122117
diskName, diskURI, err := c.createVHDBlobDisk(blobClient, account.Name, name, vhdContainerName, int64(requestGB))
123118
if err != nil {
@@ -176,11 +171,6 @@ func (c *BlobDiskController) getBlobNameAndAccountFromURI(diskURI string) (strin
176171

177172
func (c *BlobDiskController) createVHDBlobDisk(blobClient azstorage.BlobStorageClient, accountName, vhdName, containerName string, sizeGB int64) (string, string, error) {
178173
container := blobClient.GetContainerReference(containerName)
179-
_, err := container.CreateIfNotExists(&azstorage.CreateContainerOptions{Access: azstorage.ContainerAccessTypePrivate})
180-
if err != nil {
181-
return "", "", err
182-
}
183-
184174
size := 1024 * 1024 * 1024 * sizeGB
185175
vhdSize := size + vhd.VHD_HEADER_SIZE /* header size */
186176
// Blob name in URL must end with '.vhd' extension.
@@ -193,7 +183,17 @@ func (c *BlobDiskController) createVHDBlobDisk(blobClient azstorage.BlobStorageC
193183
blob := container.GetBlobReference(vhdName)
194184
blob.Properties.ContentLength = vhdSize
195185
blob.Metadata = tags
196-
err = blob.PutPageBlob(nil)
186+
err := blob.PutPageBlob(nil)
187+
if err != nil {
188+
// if container doesn't exist, create one and retry PutPageBlob
189+
detail := err.Error()
190+
if strings.Contains(detail, errContainerNotFound) {
191+
err = container.Create(&azstorage.CreateContainerOptions{Access: azstorage.ContainerAccessTypePrivate})
192+
if err == nil {
193+
err = blob.PutPageBlob(nil)
194+
}
195+
}
196+
}
197197
if err != nil {
198198
return "", "", fmt.Errorf("failed to put page blob %s in container %s: %v", vhdName, containerName, err)
199199
}

pkg/volume/azure_dd/azure_dd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type DiskController interface {
4848
GetNextDiskLun(nodeName types.NodeName) (int32, error)
4949

5050
// Create a VHD blob
51-
CreateVolume(name, storageAccount string, storageAccountType storage.SkuName, location string, requestGB int) (string, string, int, error)
51+
CreateVolume(name, storageAccount, storageAccountType, location string, requestGB int) (string, string, int, error)
5252
// Delete a VHD blob
5353
DeleteVolume(diskURI string) error
5454
}

pkg/volume/azure_dd/azure_provision.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func (p *azureDiskProvisioner) Provision() (*v1.PersistentVolume, error) {
149149
}
150150
} else {
151151
if kind == v1.AzureDedicatedBlobDisk {
152-
_, diskURI, _, err = diskController.CreateVolume(name, account, skuName, location, requestGB)
152+
_, diskURI, _, err = diskController.CreateVolume(name, account, storageAccountType, location, requestGB)
153153
if err != nil {
154154
return nil, err
155155
}

0 commit comments

Comments
 (0)