@@ -59,11 +59,12 @@ type BlobDiskController struct {
59
59
accounts map [string ]* storageAccountState
60
60
}
61
61
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
+ )
67
68
68
69
func newBlobDiskController (common * controllerCommon ) (* BlobDiskController , error ) {
69
70
c := BlobDiskController {common : common }
@@ -83,7 +84,7 @@ func newBlobDiskController(common *controllerCommon) (*BlobDiskController, error
83
84
// CreateVolume creates a VHD blob in a storage account that has storageType and location using the given storage account.
84
85
// If no storage account is given, search all the storage accounts associated with the resource group and pick one that
85
86
// 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 ) {
87
88
var err error
88
89
accounts := []accountWithLocation {}
89
90
if len (storageAccount ) > 0 {
@@ -98,7 +99,7 @@ func (c *BlobDiskController) CreateVolume(name, storageAccount string, storageAc
98
99
}
99
100
for _ , account := range accounts {
100
101
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 {
102
103
// find the access key with this account
103
104
key , err := c .common .cloud .getStorageAccesskey (account .Name )
104
105
if err != nil {
@@ -112,12 +113,6 @@ func (c *BlobDiskController) CreateVolume(name, storageAccount string, storageAc
112
113
}
113
114
blobClient := client .GetBlobService ()
114
115
115
- container := blobClient .GetContainerReference (vhdContainerName )
116
- _ , err = container .CreateIfNotExists (& azstorage.CreateContainerOptions {Access : azstorage .ContainerAccessTypePrivate })
117
- if err != nil {
118
- return "" , "" , 0 , err
119
- }
120
-
121
116
// create a page blob in this account's vhd container
122
117
diskName , diskURI , err := c .createVHDBlobDisk (blobClient , account .Name , name , vhdContainerName , int64 (requestGB ))
123
118
if err != nil {
@@ -176,11 +171,6 @@ func (c *BlobDiskController) getBlobNameAndAccountFromURI(diskURI string) (strin
176
171
177
172
func (c * BlobDiskController ) createVHDBlobDisk (blobClient azstorage.BlobStorageClient , accountName , vhdName , containerName string , sizeGB int64 ) (string , string , error ) {
178
173
container := blobClient .GetContainerReference (containerName )
179
- _ , err := container .CreateIfNotExists (& azstorage.CreateContainerOptions {Access : azstorage .ContainerAccessTypePrivate })
180
- if err != nil {
181
- return "" , "" , err
182
- }
183
-
184
174
size := 1024 * 1024 * 1024 * sizeGB
185
175
vhdSize := size + vhd .VHD_HEADER_SIZE /* header size */
186
176
// Blob name in URL must end with '.vhd' extension.
@@ -193,7 +183,17 @@ func (c *BlobDiskController) createVHDBlobDisk(blobClient azstorage.BlobStorageC
193
183
blob := container .GetBlobReference (vhdName )
194
184
blob .Properties .ContentLength = vhdSize
195
185
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
+ }
197
197
if err != nil {
198
198
return "" , "" , fmt .Errorf ("failed to put page blob %s in container %s: %v" , vhdName , containerName , err )
199
199
}
0 commit comments