Skip to content

Commit 8740db7

Browse files
committed
Allow users to specify "storage-locations" for snapshots.
Use a comma separated string, for example "us-central1,us-west1" as the "snapshot-locations" field of snapshot class parameters. See the PR for detailed examples.
1 parent 1067990 commit 8740db7

File tree

5 files changed

+28
-13
lines changed

5 files changed

+28
-13
lines changed

pkg/common/parameters.go

+4
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@ import (
2222
)
2323

2424
const (
25+
// Parameters for StorageClass
2526
ParameterKeyType = "type"
2627
ParameterKeyReplicationType = "replication-type"
2728
ParameterKeyDiskEncryptionKmsKey = "disk-encryption-kms-key"
2829
ParameterKeyLabels = "labels"
2930

31+
// Parameters for VolumeSnapshotClass
32+
ParameterStorageLocations = "storage-locations"
33+
3034
replicationTypeNone = "none"
3135

3236
// Keys for PV and PVC parameters as reported by external-provisioner

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ func (cloud *FakeCloudProvider) GetSnapshot(ctx context.Context, project, snapsh
387387
return snapshot, nil
388388
}
389389

390-
func (cloud *FakeCloudProvider) CreateSnapshot(ctx context.Context, project string, volKey *meta.Key, snapshotName string) (*computev1.Snapshot, error) {
390+
func (cloud *FakeCloudProvider) CreateSnapshot(ctx context.Context, project string, volKey *meta.Key, snapshotName string, storageLocations string) (*computev1.Snapshot, error) {
391391
if snapshot, ok := cloud.snapshots[snapshotName]; ok {
392392
return snapshot, nil
393393
}
@@ -398,6 +398,7 @@ func (cloud *FakeCloudProvider) CreateSnapshot(ctx context.Context, project stri
398398
CreationTimestamp: Timestamp,
399399
Status: "UPLOADING",
400400
SelfLink: cloud.getGlobalSnapshotURI(project, snapshotName),
401+
StorageLocations: strings.Split(storageLocations, ","),
401402
}
402403
switch volKey.Type() {
403404
case meta.Zonal:
@@ -493,11 +494,11 @@ type FakeBlockingCloudProvider struct {
493494
// Upon starting a CreateSnapshot, it passes a chan 'executeCreateSnapshot' into readyToExecute, then blocks on executeCreateSnapshot.
494495
// The test calling this function can block on readyToExecute to ensure that the operation has started and
495496
// allowed the CreateSnapshot to continue by passing a struct into executeCreateSnapshot.
496-
func (cloud *FakeBlockingCloudProvider) CreateSnapshot(ctx context.Context, project string, volKey *meta.Key, snapshotName string) (*computev1.Snapshot, error) {
497+
func (cloud *FakeBlockingCloudProvider) CreateSnapshot(ctx context.Context, project string, volKey *meta.Key, snapshotName string, storageLocations string) (*computev1.Snapshot, error) {
497498
executeCreateSnapshot := make(chan struct{})
498499
cloud.ReadyToExecute <- executeCreateSnapshot
499500
<-executeCreateSnapshot
500-
return cloud.FakeCloudProvider.CreateSnapshot(ctx, project, volKey, snapshotName)
501+
return cloud.FakeCloudProvider.CreateSnapshot(ctx, project, volKey, snapshotName, storageLocations)
501502
}
502503

503504
func notFoundError() *googleapi.Error {

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

+10-8
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ type GCECompute interface {
7373
ListZones(ctx context.Context, region string) ([]string, error)
7474
ListSnapshots(ctx context.Context, filter string, maxEntries int64, pageToken string) ([]*computev1.Snapshot, string, error)
7575
GetSnapshot(ctx context.Context, project, snapshotName string) (*computev1.Snapshot, error)
76-
CreateSnapshot(ctx context.Context, project string, volKey *meta.Key, snapshotName string) (*computev1.Snapshot, error)
76+
CreateSnapshot(ctx context.Context, project string, volKey *meta.Key, snapshotName string, storageLocations string) (*computev1.Snapshot, error)
7777
DeleteSnapshot(ctx context.Context, project, snapshotName string) error
7878
}
7979

@@ -787,13 +787,13 @@ func (cloud *CloudProvider) DeleteSnapshot(ctx context.Context, project, snapsho
787787
return nil
788788
}
789789

790-
func (cloud *CloudProvider) CreateSnapshot(ctx context.Context, project string, volKey *meta.Key, snapshotName string) (*computev1.Snapshot, error) {
790+
func (cloud *CloudProvider) CreateSnapshot(ctx context.Context, project string, volKey *meta.Key, snapshotName string, storageLocations string) (*computev1.Snapshot, error) {
791791
klog.V(5).Infof("Creating snapshot %s for volume %v", snapshotName, volKey)
792792
switch volKey.Type() {
793793
case meta.Zonal:
794-
return cloud.createZonalDiskSnapshot(ctx, project, volKey, snapshotName)
794+
return cloud.createZonalDiskSnapshot(ctx, project, volKey, snapshotName, storageLocations)
795795
case meta.Regional:
796-
return cloud.createRegionalDiskSnapshot(ctx, project, volKey, snapshotName)
796+
return cloud.createRegionalDiskSnapshot(ctx, project, volKey, snapshotName, storageLocations)
797797
default:
798798
return nil, fmt.Errorf("could not create snapshot, key was neither zonal nor regional, instead got: %v", volKey.String())
799799
}
@@ -864,9 +864,10 @@ func (cloud *CloudProvider) resizeRegionalDisk(ctx context.Context, project stri
864864
return requestGb, nil
865865
}
866866

867-
func (cloud *CloudProvider) createZonalDiskSnapshot(ctx context.Context, project string, volKey *meta.Key, snapshotName string) (*computev1.Snapshot, error) {
867+
func (cloud *CloudProvider) createZonalDiskSnapshot(ctx context.Context, project string, volKey *meta.Key, snapshotName string, storageLocations string) (*computev1.Snapshot, error) {
868868
snapshotToCreate := &computev1.Snapshot{
869-
Name: snapshotName,
869+
Name: snapshotName,
870+
StorageLocations: strings.Split(storageLocations, ","),
870871
}
871872

872873
_, err := cloud.service.Disks.CreateSnapshot(project, volKey.Zone, volKey.Name, snapshotToCreate).Context(ctx).Do()
@@ -878,9 +879,10 @@ func (cloud *CloudProvider) createZonalDiskSnapshot(ctx context.Context, project
878879
return cloud.waitForSnapshotCreation(ctx, project, snapshotName)
879880
}
880881

881-
func (cloud *CloudProvider) createRegionalDiskSnapshot(ctx context.Context, project string, volKey *meta.Key, snapshotName string) (*computev1.Snapshot, error) {
882+
func (cloud *CloudProvider) createRegionalDiskSnapshot(ctx context.Context, project string, volKey *meta.Key, snapshotName string, storageLocations string) (*computev1.Snapshot, error) {
882883
snapshotToCreate := &computev1.Snapshot{
883-
Name: snapshotName,
884+
Name: snapshotName,
885+
StorageLocations: strings.Split(storageLocations, ","),
884886
}
885887

886888
_, err := cloud.service.RegionDisks.CreateSnapshot(project, volKey.Region, volKey.Name, snapshotToCreate).Context(ctx).Do()

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,11 @@ func (gceCS *GCEControllerServer) CreateSnapshot(ctx context.Context, req *csi.C
609609
return nil, status.Error(codes.Internal, fmt.Sprintf("Unknown get snapshot error: %v", err))
610610
}
611611
// If we could not find the snapshot, we create a new one
612-
snapshot, err = gceCS.CloudProvider.CreateSnapshot(ctx, project, volKey, req.Name)
612+
parameters := make(map[string]string)
613+
if req.GetParameters() != nil {
614+
parameters = req.GetParameters()
615+
}
616+
snapshot, err = gceCS.CloudProvider.CreateSnapshot(ctx, project, volKey, req.Name, parameters[common.ParameterStorageLocations])
613617
if err != nil {
614618
if gce.IsGCEError(err, "notFound") {
615619
return nil, status.Error(codes.NotFound, fmt.Sprintf("Could not find volume with ID %v: %v", volKey.String(), err))

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,11 @@ func TestCreateVolumeWithVolumeSource(t *testing.T) {
848848
}
849849

850850
if tc.snapshotOnCloud {
851-
gceDriver.cs.CloudProvider.CreateSnapshot(context.Background(), tc.project, tc.volKey, name)
851+
parameters := make(map[string]string)
852+
if req.GetParameters() != nil {
853+
parameters = req.GetParameters()
854+
}
855+
gceDriver.cs.CloudProvider.CreateSnapshot(context.Background(), tc.project, tc.volKey, name, parameters[common.ParameterStorageLocations])
852856
}
853857
resp, err := gceDriver.cs.CreateVolume(context.Background(), req)
854858
//check response

0 commit comments

Comments
 (0)