Skip to content

Commit 8c95830

Browse files
committed
feat: csi-cinder storage capacity
Available capacity of disk storage Signed-off-by: Serge Logvinov <[email protected]>
1 parent 2f186d6 commit 8c95830

File tree

6 files changed

+36
-1
lines changed

6 files changed

+36
-1
lines changed

Diff for: pkg/csi/cinder/controllerserver.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,16 @@ func (cs *controllerServer) ValidateVolumeCapabilities(ctx context.Context, req
772772
}
773773

774774
func (cs *controllerServer) GetCapacity(ctx context.Context, req *csi.GetCapacityRequest) (*csi.GetCapacityResponse, error) {
775-
return nil, status.Error(codes.Unimplemented, "GetCapacity is not yet implemented")
775+
klog.V(4).Infof("GetCapacity: called with args %+v", protosanitizer.StripSecrets(*req))
776+
777+
availableCapacity, err := cs.Cloud.GetFreeCapacity()
778+
if err != nil {
779+
return nil, status.Errorf(codes.Internal, "GetCapacity: failed with error %v", err)
780+
}
781+
782+
return &csi.GetCapacityResponse{
783+
AvailableCapacity: int64(availableCapacity * 1024 * 1024 * 1024),
784+
}, nil
776785
}
777786

778787
func (cs *controllerServer) ControllerGetVolume(ctx context.Context, req *csi.ControllerGetVolumeRequest) (*csi.ControllerGetVolumeResponse, error) {

Diff for: pkg/csi/cinder/driver.go

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func NewDriver(o *DriverOpts) *Driver {
9393
csi.ControllerServiceCapability_RPC_PUBLISH_UNPUBLISH_VOLUME,
9494
csi.ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT,
9595
csi.ControllerServiceCapability_RPC_LIST_SNAPSHOTS,
96+
csi.ControllerServiceCapability_RPC_GET_CAPACITY,
9697
csi.ControllerServiceCapability_RPC_EXPAND_VOLUME,
9798
csi.ControllerServiceCapability_RPC_CLONE_VOLUME,
9899
csi.ControllerServiceCapability_RPC_LIST_VOLUMES_PUBLISHED_NODES,

Diff for: pkg/csi/cinder/openstack/openstack.go

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ type IOpenStack interface {
7070
GetInstanceByID(instanceID string) (*servers.Server, error)
7171
ExpandVolume(volumeID string, status string, size int) error
7272
GetMaxVolLimit() int64
73+
GetFreeCapacity() (int, error)
7374
GetMetadataOpts() metadata.Opts
7475
GetBlockStorageOpts() BlockStorageOpts
7576
}

Diff for: pkg/csi/cinder/openstack/openstack_mock.go

+4
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,10 @@ func (_m *OpenStackMock) GetMaxVolLimit() int64 {
460460
return 256
461461
}
462462

463+
func (_m *OpenStackMock) GetFreeCapacity() (int, error) {
464+
return 100, nil
465+
}
466+
463467
func (_m *OpenStackMock) BackupsAreEnabled() (bool, error) {
464468
return true, nil
465469
}

Diff for: pkg/csi/cinder/openstack/openstack_volumes.go

+16
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"time"
2323

2424
"github.com/gophercloud/gophercloud/openstack"
25+
volumelimit "github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/limits"
2526
volumeexpand "github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/volumeactions"
2627
"github.com/gophercloud/gophercloud/openstack/blockstorage/v3/volumes"
2728
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/volumeattach"
@@ -396,6 +397,21 @@ func (os *OpenStack) GetMaxVolLimit() int64 {
396397
return defaultMaxVolAttachLimit
397398
}
398399

400+
// GetFreeCapacity returns free capacity of the block storage, in GB
401+
func (os *OpenStack) GetFreeCapacity() (int, error) {
402+
res, err := volumelimit.Get(os.blockstorage).Extract()
403+
if err != nil {
404+
return 0, err
405+
}
406+
407+
capacity := res.Absolute.MaxTotalVolumeGigabytes - res.Absolute.TotalGigabytesUsed
408+
if capacity < 0 {
409+
capacity = 0
410+
}
411+
412+
return capacity, nil
413+
}
414+
399415
// diskIsAttached queries if a volume is attached to a compute instance
400416
func (os *OpenStack) diskIsAttached(instanceID, volumeID string) (bool, error) {
401417
volume, err := os.GetVolume(volumeID)

Diff for: tests/sanity/cinder/fakecloud.go

+4
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,10 @@ func (cloud *cloud) GetMaxVolLimit() int64 {
332332
return 256
333333
}
334334

335+
func (cloud *cloud) GetFreeCapacity() (int, error) {
336+
return 100, nil
337+
}
338+
335339
func (cloud *cloud) GetMetadataOpts() metadata.Opts {
336340
var m metadata.Opts
337341
m.SearchOrder = fmt.Sprintf("%s,%s", "configDrive", "metadataService")

0 commit comments

Comments
 (0)