Skip to content

Commit

Permalink
fix kubernetes#38362: create blob vhds container if not exists
Browse files Browse the repository at this point in the history
Signed-off-by: Huamin Chen <hchen@redhat.com>
  • Loading branch information
rootfs committed Jan 6, 2017
1 parent 307de20 commit 3a2b972
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
12 changes: 12 additions & 0 deletions pkg/cloudprovider/providers/azure/azure_blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package azure
import (
"fmt"
"regexp"
"strings"

azs "github.com/Azure/azure-sdk-for-go/storage"
)
Expand All @@ -40,9 +41,20 @@ func (az *Cloud) createVhdBlob(accountName, accountKey, name string, sizeGB int6
// Blob name in URL must end with '.vhd' extension.
name = name + ".vhd"
err = blobClient.PutPageBlob(vhdContainerName, name, vhdSize, tags)
if err != nil {
// if container doesn't exist, create one and retry PutPageBlob
detail := err.Error()
if strings.Contains(detail, errContainerNotFound) {
err = blobClient.CreateContainer(vhdContainerName, azs.ContainerAccessTypeContainer)
if err == nil {
err = blobClient.PutPageBlob(vhdContainerName, name, vhdSize, tags)
}
}
}
if err != nil {
return "", "", fmt.Errorf("failed to put page blob: %v", err)
}

// add VHD signature to the blob
h, err := createVHDHeader(uint64(size))
if err != nil {
Expand Down
9 changes: 6 additions & 3 deletions pkg/cloudprovider/providers/azure/azure_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ import (
)

const (
maxLUN = 64 // max number of LUNs per VM
maxLUN = 64 // max number of LUNs per VM
errLeaseFailed = "AcquireDiskLeaseFailed"
errLeaseIDMissing = "LeaseIdMissing"
errContainerNotFound = "ContainerNotFound"
)

// AttachDisk attaches a vhd to vm
Expand Down Expand Up @@ -65,7 +68,7 @@ func (az *Cloud) AttachDisk(diskName, diskURI string, nodeName types.NodeName, l
if err != nil {
glog.Errorf("azure attach failed, err: %v", err)
detail := err.Error()
if strings.Contains(detail, "Code=\"AcquireDiskLeaseFailed\"") {
if strings.Contains(detail, errLeaseFailed) {
// if lease cannot be acquired, immediately detach the disk and return the original error
glog.Infof("failed to acquire disk lease, try detach")
az.DetachDiskByName(diskName, diskURI, nodeName)
Expand Down Expand Up @@ -237,7 +240,7 @@ func (az *Cloud) DeleteVolume(name, uri string) error {
if err != nil {
glog.Warningf("failed to delete blob %s err: %v", uri, err)
detail := err.Error()
if strings.Contains(detail, "LeaseIdMissing") {
if strings.Contains(detail, errLeaseIDMissing) {
// disk is still being used
// see https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.blob.protocol.bloberrorcodestrings.leaseidmissing.aspx
return volume.NewDeletedVolumeInUseError(fmt.Sprintf("disk %q is still in use while being deleted", name))
Expand Down

0 comments on commit 3a2b972

Please sign in to comment.