Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set storage size to minimum volume size #441

Merged
merged 5 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Let's say "minimum volume size" to not give the impression that minimumVolumeSizeBytes is a known value from the CSI spec (which requiredBytes is only).

}

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