Skip to content

Commit

Permalink
CSI: Implement Expand Volume
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre-Emmanuel Jacquier <[email protected]>
  • Loading branch information
pierre-emmanuelJ committed Jan 19, 2024
1 parent fd62922 commit afb159c
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
50 changes: 49 additions & 1 deletion driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ var (
)

const (
// TODO: The API should return it.
MinimalVolumeSizeBytes = 100 * 1024 * 1024 * 1024
MaximumVolumeSizeBytes = 1000 * 1024 * 1024 * 1024
)

type controllerService struct {
Expand Down Expand Up @@ -605,8 +607,54 @@ func (d *controllerService) ListSnapshots(ctx context.Context, req *csi.ListSnap
// ControllerExpandVolume resizes/updates the volume (not supported yet on Exoscale Public API)
func (d *controllerService) ControllerExpandVolume(ctx context.Context, req *csi.ControllerExpandVolumeRequest) (*csi.ControllerExpandVolumeResponse, error) {
klog.V(4).Infof("ControllerExpandVolume")
zone, volumeID, err := getExoscaleID(req.GetVolumeId())
if err != nil {
return nil, err
}
client := d.client.WithURL(zone)

volume, err := client.GetBlockStorageVolume(ctx, volumeID)
if err != nil {
if errors.Is(err, v3.ErrNotFound) {
return nil, status.Errorf(codes.NotFound, "volume %s not found", volumeID)
}

return nil, err
}

newSize, err := getNewVolumeSize(req.GetCapacityRange())
if err != nil {
return nil, status.Errorf(codes.OutOfRange, "invalid capacity range: %v", err)
}

if newSize < volume.Size {
return nil, status.Error(codes.InvalidArgument, "new size must be bigger than actual volume size")
}

_, err = client.ResizeBlockStorageVolume(ctx, volumeID, v3.ResizeBlockStorageVolumeRequest{
Size: newSize,
})
if err != nil {
return nil, err
}

return nil, status.Error(codes.Unimplemented, "")
nodeExpansionRequired := true
volumeCapability := req.GetVolumeCapability()
if volumeCapability != nil {
err := validateVolumeCapability(volumeCapability)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "volumeCapabilities not supported: %s", err)
}

if _, ok := volumeCapability.GetAccessType().(*csi.VolumeCapability_Block); ok {
nodeExpansionRequired = false
}
}

return &csi.ControllerExpandVolumeResponse{
CapacityBytes: newSize,
NodeExpansionRequired: nodeExpansionRequired,
}, nil
}

// ControllerGetVolume gets a volume and return it.
Expand Down
51 changes: 51 additions & 0 deletions driver/helpers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package driver

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -94,3 +95,53 @@ func createMountPoint(path string, file bool) error {
func convertBytesToGibiBytes(nBytes int64) int64 {
return nBytes / (1024 * 1024 * 1024)
}

func getNewVolumeSize(capacityRange *csi.CapacityRange) (int64, error) {
if capacityRange == nil {
return MinimalVolumeSizeBytes, nil
}

requiredBytes := capacityRange.GetRequiredBytes()
requiredSet := requiredBytes > 0

limitBytes := capacityRange.GetLimitBytes()
limitSet := limitBytes > 0

if !requiredSet && !limitSet {
return MinimalVolumeSizeBytes, nil
}

if requiredSet && limitSet && limitBytes < requiredBytes {
return 0, errors.New("limit size is less than required size")
}

if requiredSet && !limitSet && requiredBytes < MinimalVolumeSizeBytes {
return 0, errors.New("required size is less than the minimun size")
}

if limitSet && limitBytes < MinimalVolumeSizeBytes {
return 0, errors.New("limit size is less than the minimun size")
}

if requiredSet && requiredBytes > MaximumVolumeSizeBytes {
return 0, errors.New("required size is greater than the maximum size")
}

if !requiredSet && limitSet && limitBytes > MaximumVolumeSizeBytes {
return 0, errors.New("limit size is greater than the maximum size")
}

if requiredSet && limitSet && requiredBytes == limitBytes {
return requiredBytes, nil
}

if requiredSet {
return requiredBytes, nil
}

if limitSet {
return limitBytes, nil
}

return MinimalVolumeSizeBytes, nil
}

0 comments on commit afb159c

Please sign in to comment.