Skip to content

Set storage size to minimum volume size #441

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

Merged
merged 5 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
43 changes: 26 additions & 17 deletions driver/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,20 @@ func (*fakeTagsDriver) UntagResources(context.Context, string, *godo.UntagResour
}

func TestControllerExpandVolume(t *testing.T) {
default_volume := &godo.Volume{
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use snake case here, i.e., defaultVolume.

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
godo_volume *godo.Volume
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd name this just volume since the type is embedded in the *godo.Volume type. This would also keep the diff in this PR slightly smaller.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, done.

}{
{
name: "request exceeds maximum supported size",
name: "request exceeds maximum supported size",
godo_volume: default_volume,
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of requiring to define the volume for every current and future test case, could we make it optional and let the test body pick defaultVolume if the volume from the test case struct is nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, updated.

req: &csi.ControllerExpandVolumeRequest{
VolumeId: "volume-id",
CapacityRange: &csi.CapacityRange{
Expand All @@ -200,17 +206,22 @@ func TestControllerExpandVolume(t *testing.T) {
},
{
name: "requested size less than minimum supported size",
godo_volume: &godo.Volume{
ID: "volume-id",
SizeGigaBytes: (minimumVolumeSizeInBytes / giB),
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we subtract a bit to make sure we're really below the minimum size (and not hitting it exactly)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because this is size in gb I don't find a way to go less than 1 Gi. I did change that value to 1 to make it simpler. Please suggest if I am missing anything here.

},
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",
name: "volume for corresponding volume id does not exist",
godo_volume: default_volume,
req: &csi.ControllerExpandVolumeRequest{
VolumeId: "non-existent-id",
CapacityRange: &csi.CapacityRange{
Expand All @@ -221,7 +232,8 @@ func TestControllerExpandVolume(t *testing.T) {
err: status.Error(codes.Internal, "ControllerExpandVolume could not retrieve existing volume: volume not found"),
},
{
name: "new volume size is less than old volume size",
name: "new volume size is less than old volume size",
godo_volume: default_volume,
req: &csi.ControllerExpandVolumeRequest{
VolumeId: "volume-id",
CapacityRange: &csi.CapacityRange{
Expand All @@ -232,7 +244,8 @@ func TestControllerExpandVolume(t *testing.T) {
err: nil,
},
{
name: "new volume size is equal to old volume size",
name: "new volume size is equal to old volume size",
godo_volume: default_volume,
req: &csi.ControllerExpandVolumeRequest{
VolumeId: "volume-id",
CapacityRange: &csi.CapacityRange{
Expand All @@ -245,20 +258,16 @@ 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),
}
driver := &Driver{
region: "foo",
storage: &fakeStorageDriver{
volumes: map[string]*godo.Volume{
"volume-id": volume,
"volume-id": tc.godo_volume,
},
},
storageActions: &fakeStorageActionsDriver{
volumes: map[string]*godo.Volume{
"volume-id": volume,
"volume-id": tc.godo_volume,
},
},
log: logrus.New().WithField("test_enabed", true),
Expand All @@ -268,7 +277,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.godo_volume.SizeGigaBytes * giB), resp.CapacityBytes)
}

})
Expand Down