Skip to content

Commit c72a290

Browse files
committed
Add Controller modify volume functionality
1 parent 4004cd7 commit c72a290

File tree

6 files changed

+118
-2
lines changed

6 files changed

+118
-2
lines changed

pkg/common/parameters.go

+28
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ type StoragePool struct {
121121
ResourceName string
122122
}
123123

124+
type ModifyVolumeParameters struct {
125+
IOPS int64
126+
Throughput int64
127+
}
128+
124129
// ExtractAndDefaultParameters will take the relevant parameters from a map and
125130
// put them into a well defined struct making sure to default unspecified fields.
126131
// extraVolumeLabels are added as labels; if there are also labels specified in
@@ -301,3 +306,26 @@ func extractResourceTagsParameter(tagsString string, resourceTags map[string]str
301306
}
302307
return nil
303308
}
309+
310+
func ExtractModifyVolumeParameters(parameters map[string]string) (ModifyVolumeParameters, error) {
311+
312+
modifyVolumeParams := ModifyVolumeParameters{}
313+
314+
for key, value := range parameters {
315+
switch strings.ToLower(key) {
316+
case "iops":
317+
iops, err := ConvertStringToInt64(value)
318+
if err != nil {
319+
return ModifyVolumeParameters{}, fmt.Errorf("parameters contain invalid iops parameter: %w", err)
320+
}
321+
modifyVolumeParams.IOPS = iops
322+
case "throughput":
323+
throughput, err := ConvertStringToInt64(value)
324+
if err != nil {
325+
return ModifyVolumeParameters{}, fmt.Errorf("parameters contain invalid throughput parameter: %w", err)
326+
}
327+
modifyVolumeParams.Throughput = throughput
328+
}
329+
}
330+
return modifyVolumeParams, nil
331+
}

pkg/gce-cloud-provider/compute/fake-gce.go

+5
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,11 @@ func (cloud *FakeCloudProvider) InsertDisk(ctx context.Context, project string,
266266
return nil
267267
}
268268

269+
func (cloud *FakeCloudProvider) UpdateDisk(ctx context.Context, project string, volKey *meta.Key, existingDisk *CloudDisk, params common.ModifyVolumeParameters) error {
270+
271+
return nil
272+
}
273+
269274
func (cloud *FakeCloudProvider) DeleteDisk(ctx context.Context, project string, volKey *meta.Key) error {
270275
delete(cloud.disks, volKey.Name)
271276
return nil

pkg/gce-cloud-provider/compute/gce-compute.go

+40
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ type GCECompute interface {
101101
ValidateExistingDisk(ctx context.Context, disk *CloudDisk, params common.DiskParameters, reqBytes, limBytes int64, multiWriter bool) error
102102
InsertDisk(ctx context.Context, project string, volKey *meta.Key, params common.DiskParameters, capBytes int64, capacityRange *csi.CapacityRange, replicaZones []string, snapshotID string, volumeContentSourceVolumeID string, multiWriter bool) error
103103
DeleteDisk(ctx context.Context, project string, volumeKey *meta.Key) error
104+
UpdateDisk(ctx context.Context, project string, volKey *meta.Key, existingDisk *CloudDisk, params common.ModifyVolumeParameters) error
104105
AttachDisk(ctx context.Context, project string, volKey *meta.Key, readWrite, diskType, instanceZone, instanceName string, forceAttach bool) error
105106
DetachDisk(ctx context.Context, project, deviceName, instanceZone, instanceName string) error
106107
GetDiskSourceURI(project string, volKey *meta.Key) string
@@ -442,6 +443,45 @@ func (cloud *CloudProvider) InsertDisk(ctx context.Context, project string, volK
442443
}
443444
}
444445

446+
func (cloud *CloudProvider) UpdateDisk(ctx context.Context, project string, volKey *meta.Key, existingDisk *CloudDisk, params common.ModifyVolumeParameters) error {
447+
448+
klog.V(5).Infof("Updating disk %v", volKey)
449+
450+
switch volKey.Type() {
451+
case meta.Zonal:
452+
return cloud.updateZonalDisk(ctx, project, volKey, existingDisk, params)
453+
case meta.Regional:
454+
return cloud.updateRegionalDisk(ctx, project, volKey, existingDisk, params)
455+
default:
456+
return fmt.Errorf("could not update disk, key was neither zonal nor regional, instead got: %v", volKey.String())
457+
}
458+
}
459+
460+
func (cloud *CloudProvider) updateZonalDisk(ctx context.Context, project string, volKey *meta.Key, existingDisk *CloudDisk, params common.ModifyVolumeParameters) error {
461+
462+
updatedDisk := &computev1.Disk{
463+
Name: existingDisk.GetName(),
464+
ProvisionedIops: params.IOPS,
465+
ProvisionedThroughput: params.Throughput,
466+
}
467+
468+
diskUpdateOp := cloud.service.Disks.Update(project, volKey.Zone, volKey.Name, updatedDisk)
469+
diskUpdateOp.Paths("provisionedIops", "provisionedThroughput")
470+
updateOpResult, err := diskUpdateOp.Context(ctx).Do()
471+
472+
if err != nil {
473+
return fmt.Errorf("error updating disk %v: %w", volKey, err)
474+
}
475+
476+
fmt.Printf("http status : %d", updateOpResult.HTTPStatusCode)
477+
478+
return nil
479+
}
480+
481+
func (cloud *CloudProvider) updateRegionalDisk(ctx context.Context, project string, volKey *meta.Key, existingDisk *CloudDisk, params common.ModifyVolumeParameters) error {
482+
// TODO : Implement this
483+
return nil
484+
}
445485
func convertV1CustomerEncryptionKeyToBeta(v1Key *computev1.CustomerEncryptionKey) *computebeta.CustomerEncryptionKey {
446486
return &computebeta.CustomerEncryptionKey{
447487
KmsKeyName: v1Key.KmsKeyName,

pkg/gce-pd-csi-driver/controller.go

+42
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,48 @@ func (gceCS *GCEControllerServer) CreateVolume(ctx context.Context, req *csi.Cre
527527

528528
}
529529

530+
func (gceCS *GCEControllerServer) ControllerModifyVolume(ctx context.Context, req *csi.ControllerModifyVolumeRequest) (*csi.ControllerModifyVolumeResponse, error) {
531+
532+
volumeID := req.GetVolumeId()
533+
klog.V(4).Infof("Modifying Volume ID: %s", volumeID)
534+
535+
if volumeID == "" {
536+
return nil, status.Error(codes.InvalidArgument, "volume ID must be provided")
537+
}
538+
539+
project, volKey, err := common.VolumeIDToKey(volumeID)
540+
541+
if err != nil {
542+
return nil, status.Errorf(codes.InvalidArgument, "volume ID is invalid: %v", err.Error())
543+
}
544+
545+
volumeModifyParams, err := common.ExtractModifyVolumeParameters(req.GetMutableParameters())
546+
if err != nil {
547+
klog.Errorf("Failed to extract parameters for volume %s: %v", volumeID, err)
548+
return nil, status.Errorf(codes.InvalidArgument, "Invalid parameters: %v", err)
549+
}
550+
klog.V(4).Infof("Modify Volume Parameters for %s: %v", volumeID, volumeModifyParams)
551+
552+
existingDisk, err := gceCS.CloudProvider.GetDisk(ctx, project, volKey, gce.GCEAPIVersionBeta)
553+
554+
if err != nil {
555+
return nil, status.Errorf(codes.NotFound, "failed to get volume: %v", err)
556+
}
557+
558+
if existingDisk != nil || existingDisk.GetSelfLink() == "" {
559+
560+
return nil, status.Errorf(codes.Internal, "failed to get volume : %s", volumeID)
561+
}
562+
563+
err = gceCS.CloudProvider.UpdateDisk(ctx, project, volKey, existingDisk, volumeModifyParams)
564+
if err != nil {
565+
klog.Errorf("Failed to modify volume %s: %v", volumeID, err)
566+
return nil, status.Errorf(codes.Internal, "Failed to modify volume %s: %v", volumeID, err)
567+
}
568+
569+
return &csi.ControllerModifyVolumeResponse{}, nil
570+
}
571+
530572
func (gceCS *GCEControllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest) (*csi.DeleteVolumeResponse, error) {
531573
var err error
532574
diskTypeForMetric := metrics.DefaultDiskTypeForMetric

pkg/gce-pd-csi-driver/gce-pd-driver.go

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func (gceDriver *GCEDriver) SetupGCEDriver(name, vendorVersion string, extraVolu
7373
csi.ControllerServiceCapability_RPC_LIST_VOLUMES,
7474
csi.ControllerServiceCapability_RPC_LIST_VOLUMES_PUBLISHED_NODES,
7575
csi.ControllerServiceCapability_RPC_CLONE_VOLUME,
76+
csi.ControllerServiceCapability_RPC_MODIFY_VOLUME,
7677
}
7778
gceDriver.AddControllerServiceCapabilities(csc)
7879
ns := []csi.NodeServiceCapability_RPC_Type{

vendor/modules.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ github.com/cenkalti/backoff/v4
7070
# github.com/cespare/xxhash/v2 v2.2.0
7171
## explicit; go 1.11
7272
github.com/cespare/xxhash/v2
73-
# github.com/container-storage-interface/spec v1.6.0
74-
## explicit; go 1.16
73+
# github.com/container-storage-interface/spec v1.9.0
74+
## explicit; go 1.18
7575
github.com/container-storage-interface/spec/lib/go/csi
7676
# github.com/davecgh/go-spew v1.1.1
7777
## explicit

0 commit comments

Comments
 (0)