From e39d38ae6baeabef472f9945209b577f3283805b Mon Sep 17 00:00:00 2001 From: Srikanth Rao Date: Tue, 5 Jul 2022 14:39:35 +0530 Subject: [PATCH] Set storage size to minimum volume size (#441) Set storage size to minimum 1Gi if volume size specified is less than the minimum. --- driver/controller.go | 16 ++++++++++------ driver/controller_test.go | 34 +++++++++++++++++++++------------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/driver/controller.go b/driver/controller.go index f003d1a9..35997ead 100644 --- a/driver/controller.go +++ b/driver/controller.go @@ -93,7 +93,7 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("volume capabilities cannot be satisified: %s", strings.Join(violations, "; "))) } - size, err := extractStorage(req.CapacityRange) + size, err := d.extractStorage(req.CapacityRange) if err != nil { return nil, status.Errorf(codes.OutOfRange, "invalid capacity range: %v", err) } @@ -873,7 +873,7 @@ func (d *Driver) ControllerExpandVolume(ctx context.Context, req *csi.Controller return nil, status.Errorf(codes.Internal, "ControllerExpandVolume could not retrieve existing volume: %v", err) } - resizeBytes, err := extractStorage(req.GetCapacityRange()) + resizeBytes, err := d.extractStorage(req.GetCapacityRange()) if err != nil { return nil, status.Errorf(codes.OutOfRange, "ControllerExpandVolume invalid capacity range: %v", err) } @@ -934,9 +934,9 @@ func (d *Driver) ControllerGetVolume(ctx context.Context, req *csi.ControllerGet // extractStorage extracts the storage size in bytes from the given capacity // range. If the capacity range is not satisfied it returns the default volume -// size. If the capacity range is below or above supported sizes, it returns an -// error. -func extractStorage(capRange *csi.CapacityRange) (int64, error) { +// size. If the capacity range is above supported sizes, it returns an +// error. If the capacity range is below supported size, it returns the minimum supported size +func (d *Driver) extractStorage(capRange *csi.CapacityRange) (int64, error) { if capRange == nil { return defaultVolumeSizeInBytes, nil } @@ -955,7 +955,11 @@ func extractStorage(capRange *csi.CapacityRange) (int64, error) { } if requiredSet && !limitSet && requiredBytes < minimumVolumeSizeInBytes { - return 0, fmt.Errorf("required (%v) can not be less than minimum supported volume size (%v)", formatBytes(requiredBytes), formatBytes(minimumVolumeSizeInBytes)) + d.log.WithFields(logrus.Fields{ + "required_bytes": formatBytes(requiredBytes), + "minimum_volume_size": formatBytes(minimumVolumeSizeInBytes), + }).Warn("requiredBytes is less than minimum volume size, setting requiredBytes default to minimumVolumeSizeBytes") + return minimumVolumeSizeInBytes, nil } if limitSet && limitBytes < minimumVolumeSizeInBytes { diff --git a/driver/controller_test.go b/driver/controller_test.go index 1e2e3b76..b1b464df 100644 --- a/driver/controller_test.go +++ b/driver/controller_test.go @@ -181,11 +181,16 @@ func (*fakeTagsDriver) UntagResources(context.Context, string, *godo.UntagResour } func TestControllerExpandVolume(t *testing.T) { + defaultVolume := &godo.Volume{ + ID: "volume-id", + SizeGigaBytes: (defaultVolumeSizeInBytes / giB), + } tcs := []struct { - name string - req *csi.ControllerExpandVolumeRequest - resp *csi.ControllerExpandVolumeResponse - err error + name string + req *csi.ControllerExpandVolumeRequest + resp *csi.ControllerExpandVolumeResponse + err error + volume *godo.Volume }{ { name: "request exceeds maximum supported size", @@ -199,15 +204,19 @@ func TestControllerExpandVolume(t *testing.T) { err: status.Error(codes.OutOfRange, "ControllerExpandVolume invalid capacity range: required (20Ti) can not exceed maximum supported volume size (16Ti)"), }, { - name: "requested size less than minimum supported size", + name: "requested size less than minimum supported size returns the default minimum volume size", + volume: &godo.Volume{ + ID: "volume-id", + SizeGigaBytes: 1, + }, req: &csi.ControllerExpandVolumeRequest{ VolumeId: "volume-id", CapacityRange: &csi.CapacityRange{ RequiredBytes: 0.5 * giB, }, }, - resp: nil, - err: status.Error(codes.OutOfRange, "ControllerExpandVolume invalid capacity range: required (512Mi) can not be less than minimum supported volume size (1Gi)"), + resp: &csi.ControllerExpandVolumeResponse{CapacityBytes: minimumVolumeSizeInBytes, NodeExpansionRequired: true}, + err: nil, }, { name: "volume for corresponding volume id does not exist", @@ -245,20 +254,19 @@ func TestControllerExpandVolume(t *testing.T) { } for _, tc := range tcs { t.Run(tc.name, func(t *testing.T) { - volume := &godo.Volume{ - ID: "volume-id", - SizeGigaBytes: (defaultVolumeSizeInBytes / giB), + if tc.volume == nil { + tc.volume = defaultVolume } driver := &Driver{ region: "foo", storage: &fakeStorageDriver{ volumes: map[string]*godo.Volume{ - "volume-id": volume, + "volume-id": tc.volume, }, }, storageActions: &fakeStorageActionsDriver{ volumes: map[string]*godo.Volume{ - "volume-id": volume, + "volume-id": tc.volume, }, }, log: logrus.New().WithField("test_enabed", true), @@ -268,7 +276,7 @@ func TestControllerExpandVolume(t *testing.T) { assert.Equal(t, err, tc.err) } else { assert.Equal(t, tc.resp, resp) - assert.Equal(t, (volume.SizeGigaBytes * giB), resp.CapacityBytes) + assert.Equal(t, (tc.volume.SizeGigaBytes * giB), resp.CapacityBytes) } })