From 88bbd8ea573c34d047499f4f4da5a1b7017f5575 Mon Sep 17 00:00:00 2001 From: Basit Hasan Date: Mon, 3 Apr 2023 18:55:44 +0530 Subject: [PATCH 1/2] controller get capacity method added Signed-off-by: Basit Hasan --- driver/controller.go | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/driver/controller.go b/driver/controller.go index 13244d8f..8ec355b0 100644 --- a/driver/controller.go +++ b/driver/controller.go @@ -590,12 +590,28 @@ func (d *Driver) ListVolumes(ctx context.Context, req *csi.ListVolumesRequest) ( // GetCapacity returns the capacity of the storage pool func (d *Driver) GetCapacity(ctx context.Context, req *csi.GetCapacityRequest) (*csi.GetCapacityResponse, error) { - // TODO(arslan): check if we can provide this information somehow + volumes, _, err := d.storage.ListVolumes(ctx, &godo.ListVolumeParams{}) + if err != nil { + return nil, err + } + + totalCapacity := int64(0) + for _, volume := range volumes { + if volume.Region.Slug == d.region { + totalCapacity += int64(volume.SizeGigaBytes) * 1024 * 1024 * 1024 + } + } + + // Create a new response object + resp := &csi.GetCapacityResponse{ + AvailableCapacity: totalCapacity, + } d.log.WithFields(logrus.Fields{ - "params": req.Parameters, - "method": "get_capacity", - }).Warn("get capacity is not implemented") - return nil, status.Error(codes.Unimplemented, "") + "response": resp, + "method": "controller_get_capacity", + }).Info("controller get capacity called") + return resp, nil + } // ControllerGetCapabilities returns the capabilities of the controller service. From cdc6c2f819968c15490ddba063f771f51a0d50a9 Mon Sep 17 00:00:00 2001 From: Basit Hasan Date: Fri, 14 Apr 2023 14:35:50 +0530 Subject: [PATCH 2/2] updated method Signed-off-by: Basit Hasan --- driver/controller.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/driver/controller.go b/driver/controller.go index 8ec355b0..b6731c04 100644 --- a/driver/controller.go +++ b/driver/controller.go @@ -588,23 +588,34 @@ func (d *Driver) ListVolumes(ctx context.Context, req *csi.ListVolumesRequest) ( return resp, nil } -// GetCapacity returns the capacity of the storage pool +// GetCapacity returns the capacity of the storage pool and return it in GigaBytes func (d *Driver) GetCapacity(ctx context.Context, req *csi.GetCapacityRequest) (*csi.GetCapacityResponse, error) { volumes, _, err := d.storage.ListVolumes(ctx, &godo.ListVolumeParams{}) if err != nil { return nil, err } - totalCapacity := int64(0) + usedCapacity := int64(0) for _, volume := range volumes { if volume.Region.Slug == d.region { - totalCapacity += int64(volume.SizeGigaBytes) * 1024 * 1024 * 1024 + if volume.DropletIDs != nil && len(volume.DropletIDs) > 0 { + usedCapacity += volume.SizeGigaBytes + } } } + // Get the limit on the total storage capacity that can be used + limitdetails, err := d.checkLimit(ctx) + if err != nil { + return nil, err + } + + limit := int64(limitdetails.limit) + availableCapacity := limit - usedCapacity + // Create a new response object resp := &csi.GetCapacityResponse{ - AvailableCapacity: totalCapacity, + AvailableCapacity: availableCapacity, } d.log.WithFields(logrus.Fields{ "response": resp,