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 <[email protected]>
  • Loading branch information
sergelogvinov committed Oct 15, 2024
1 parent a294df6 commit 5c3d65b
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 1 deletion.
24 changes: 23 additions & 1 deletion pkg/csi/cinder/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"

corev1 "k8s.io/api/core/v1"
"k8s.io/cloud-provider-openstack/pkg/csi/cinder/openstack"
"k8s.io/cloud-provider-openstack/pkg/util"
cpoerrors "k8s.io/cloud-provider-openstack/pkg/util/errors"
Expand Down Expand Up @@ -949,7 +950,28 @@ 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))

topology := req.GetAccessibleTopology()
if topology == nil {
return &csi.GetCapacityResponse{}, nil
}

region := topology.GetSegments()[corev1.LabelTopologyRegion]

cloud, cloudExist := cs.Clouds[region]
if !cloudExist {
return nil, status.Error(codes.InvalidArgument, "[GetCapacity] specified cloud undefined")
}

availableCapacity, err := cloud.GetFreeQuotaStorageSpace()
if err != nil {
return nil, status.Errorf(codes.Internal, "[GetCapacity]: failed with error %v", err)
}

return &csi.GetCapacityResponse{
AvailableCapacity: int64(availableCapacity * 1024 * 1024 * 1024),
}, 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 @@ -99,6 +99,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
GetFreeQuotaStorageSpace() (int, 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) GetFreeQuotaStorageSpace() (int, error) {
return 100, nil
}

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

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

// GetFreeQuotaStorageSpace returns the tenant quota capacity of the block storage, in GB
func (os *OpenStack) GetFreeQuotaStorageSpace() (int, error) {
mc := metrics.NewMetricContext("limits", "get")

res, err := limits.Get(context.TODO(), os.blockstorage).Extract()
if mc.ObserveRequest(err) != nil {
return 0, err
}

capacity := res.Absolute.MaxTotalVolumeGigabytes - res.Absolute.TotalGigabytesUsed
if capacity < 0 {
capacity = 0
}
return capacity, 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
4 changes: 4 additions & 0 deletions tests/sanity/cinder/fakecloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ func (cloud *cloud) GetMaxVolLimit() int64 {
return 256
}

func (cloud *cloud) GetFreeQuotaStorageSpace() (int, error) {
return 100, nil
}

func (cloud *cloud) GetMetadataOpts() metadata.Opts {
var m metadata.Opts
m.SearchOrder = fmt.Sprintf("%s,%s", "configDrive", "metadataService")
Expand Down

0 comments on commit 5c3d65b

Please sign in to comment.