Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#115 from ZeroMagic/capacity
Browse files Browse the repository at this point in the history
fix: check the disk capacity
  • Loading branch information
k8s-ci-robot authored Jul 10, 2019
2 parents a978653 + 047d67b commit 3db8e33
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
19 changes: 19 additions & 0 deletions pkg/azuredisk/azuredisk.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package azuredisk
import (
"context"
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
Expand All @@ -36,6 +37,9 @@ import (
"k8s.io/kubernetes/pkg/util/mount"
"k8s.io/kubernetes/pkg/volume/util"
"k8s.io/legacy-cloud-providers/azure"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

const (
Expand All @@ -49,6 +53,9 @@ const (
// see https://docs.microsoft.com/en-us/rest/api/compute/disks/createorupdate#uri-parameters
diskNameMinLength = 1
diskNameMaxLength = 80

//default disk size is 1 GiB
defaultDiskSize = 1
)

var (
Expand Down Expand Up @@ -174,6 +181,18 @@ func (d *Driver) checkDiskExists(ctx context.Context, diskURI string) error {
return nil
}

func (d *Driver) checkDiskCapacity(ctx context.Context, resourceGroup, diskName string, requestGiB int) (bool, error) {
disk, err := d.cloud.DisksClient.Get(ctx, resourceGroup, diskName)
// Because we can not judge the reason of the error. Maybe the disk does not exist.
// So here we do not handle the error.
if err == nil {
if !reflect.DeepEqual(disk, compute.Disk{}) && disk.DiskSizeGB != nil && int(*disk.DiskSizeGB) != requestGiB {
return false, status.Errorf(codes.AlreadyExists, "the request volume already exists, but its capacity(%v) is different from (%v)", *disk.DiskProperties.DiskSizeGB, requestGiB)
}
}
return true, nil
}

func isValidDiskURI(diskURI string) error {
if isManagedDisk(diskURI) {
if strings.Index(diskURI, "/subscriptions/") != 0 {
Expand Down
17 changes: 14 additions & 3 deletions pkg/azuredisk/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,12 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
if !isValidVolumeCapabilities(volumeCapabilities) {
return nil, status.Error(codes.InvalidArgument, "Volume capabilities not supported")
}

volSizeBytes := int64(req.GetCapacityRange().GetRequiredBytes())
capacityBytes := req.GetCapacityRange().GetRequiredBytes()
volSizeBytes := int64(capacityBytes)
requestGiB := int(volumehelper.RoundUpGiB(volSizeBytes))
if requestGiB == 0 {
requestGiB = defaultDiskSize
}

maxVolSize := int(req.GetCapacityRange().GetLimitBytes())
if (maxVolSize > 0) && (maxVolSize < requestGiB) {
Expand Down Expand Up @@ -135,6 +138,10 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
diskName = getValidDiskName(name)
}

if resourceGroup == "" {
resourceGroup = d.cloud.ResourceGroup
}

// normalize values
skuName, err := normalizeStorageAccountType(storageAccountType)
if err != nil {
Expand All @@ -158,6 +165,10 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)

selectedAvailabilityZone := pickAvailabilityZone(req.GetAccessibilityRequirements())

if ok, err := d.checkDiskCapacity(ctx, resourceGroup, diskName, requestGiB); !ok {
return nil, err
}

klog.V(2).Infof("begin to create azure disk(%s) account type(%s) rg(%s) location(%s) size(%d)", diskName, skuName, resourceGroup, location, requestGiB)

diskURI := ""
Expand Down Expand Up @@ -228,7 +239,7 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
return &csi.CreateVolumeResponse{
Volume: &csi.Volume{
VolumeId: diskURI,
CapacityBytes: req.GetCapacityRange().GetRequiredBytes(),
CapacityBytes: capacityBytes,
VolumeContext: parameters,
AccessibleTopology: []*csi.Topology{
{
Expand Down

0 comments on commit 3db8e33

Please sign in to comment.