Skip to content

Commit

Permalink
Set storage size to minimum volume size (#441)
Browse files Browse the repository at this point in the history
Set storage size to minimum 1Gi if volume size specified is less than the minimum.
  • Loading branch information
srikiz authored Jul 5, 2022
1 parent 2b4244c commit e39d38a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
16 changes: 10 additions & 6 deletions driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
34 changes: 21 additions & 13 deletions driver/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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),
Expand All @@ -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)
}

})
Expand Down

0 comments on commit e39d38a

Please sign in to comment.