Skip to content

Commit

Permalink
feat: csi-cinder storage capacity
Browse files Browse the repository at this point in the history
Available capacity of disk storage

Signed-off-by: Serge Logvinov <serge.logvinov@sinextra.dev>
  • Loading branch information
sergelogvinov committed May 16, 2024
1 parent 2f186d6 commit b564f33
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/csi/cinder/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,16 @@ func (cs *controllerServer) ValidateVolumeCapabilities(ctx context.Context, req
}

func (cs *controllerServer) GetCapacity(ctx context.Context, req *csi.GetCapacityRequest) (*csi.GetCapacityResponse, error) {
return nil, status.Error(codes.Unimplemented, "GetCapacity is not yet implemented")
klog.V(4).Infof("GetCapacity: called with args %+v", protosanitizer.StripSecrets(*req))

availableCapacity, err := cs.Cloud.GetFreeCapacity()
if err != nil {
return nil, status.Errorf(codes.Internal, "GetCapacity: failed with error %v", err)
}

return &csi.GetCapacityResponse{
AvailableCapacity: availableCapacity,
}, nil
}

func (cs *controllerServer) ControllerGetVolume(ctx context.Context, req *csi.ControllerGetVolumeRequest) (*csi.ControllerGetVolumeResponse, error) {
Expand Down
1 change: 1 addition & 0 deletions pkg/csi/cinder/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func NewDriver(o *DriverOpts) *Driver {
csi.ControllerServiceCapability_RPC_PUBLISH_UNPUBLISH_VOLUME,
csi.ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT,
csi.ControllerServiceCapability_RPC_LIST_SNAPSHOTS,
csi.ControllerServiceCapability_RPC_GET_CAPACITY,
csi.ControllerServiceCapability_RPC_EXPAND_VOLUME,
csi.ControllerServiceCapability_RPC_CLONE_VOLUME,
csi.ControllerServiceCapability_RPC_LIST_VOLUMES_PUBLISHED_NODES,
Expand Down
1 change: 1 addition & 0 deletions pkg/csi/cinder/openstack/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type IOpenStack interface {
GetInstanceByID(instanceID string) (*servers.Server, error)
ExpandVolume(volumeID string, status string, size int) error
GetMaxVolLimit() int64
GetFreeCapacity() (int64, error)
GetMetadataOpts() metadata.Opts
GetBlockStorageOpts() BlockStorageOpts
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/csi/cinder/openstack/openstack_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,10 @@ func (_m *OpenStackMock) GetMaxVolLimit() int64 {
return 256
}

func (_m *OpenStackMock) GetFreeCapacity() (int64, error) {
return 1024 * 1024 * 1024 * 1024, nil
}

func (_m *OpenStackMock) BackupsAreEnabled() (bool, error) {
return true, nil
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/csi/cinder/openstack/openstack_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

"github.com/gophercloud/gophercloud/openstack"
volumelimit "github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/limits"
volumeexpand "github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/volumeactions"
"github.com/gophercloud/gophercloud/openstack/blockstorage/v3/volumes"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/volumeattach"
Expand Down Expand Up @@ -396,6 +397,16 @@ func (os *OpenStack) GetMaxVolLimit() int64 {
return defaultMaxVolAttachLimit
}

// GetFreeCapacity returns free capacity of the block storage
func (os *OpenStack) GetFreeCapacity() (int64, error) {
res, err := volumelimit.Get(os.blockstorage).Extract()
if err != nil {
return 0, err
}

return int64((res.Absolute.MaxTotalVolumeGigabytes - res.Absolute.TotalGigabytesUsed) * 1024 * 1024 * 1024), nil
}

// diskIsAttached queries if a volume is attached to a compute instance
func (os *OpenStack) diskIsAttached(instanceID, volumeID string) (bool, error) {
volume, err := os.GetVolume(volumeID)
Expand Down

0 comments on commit b564f33

Please sign in to comment.