Skip to content

Commit 2447112

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 1b79ce7 commit 2447112

File tree

5 files changed

+29
-13
lines changed

5 files changed

+29
-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
@@ -384,7 +384,7 @@ func (cloud *FakeCloudProvider) GetSnapshot(ctx context.Context, snapshotName st
384384
return snapshot, nil
385385
}
386386

387-
func (cloud *FakeCloudProvider) CreateSnapshot(ctx context.Context, volKey *meta.Key, snapshotName string) (*computev1.Snapshot, error) {
387+
func (cloud *FakeCloudProvider) CreateSnapshot(ctx context.Context, volKey *meta.Key, snapshotName string, storageLocations string) (*computev1.Snapshot, error) {
388388
if snapshot, ok := cloud.snapshots[snapshotName]; ok {
389389
return snapshot, nil
390390
}
@@ -395,6 +395,7 @@ func (cloud *FakeCloudProvider) CreateSnapshot(ctx context.Context, volKey *meta
395395
CreationTimestamp: Timestamp,
396396
Status: "UPLOADING",
397397
SelfLink: cloud.getGlobalSnapshotURI(snapshotName),
398+
StorageLocations: strings.Split(storageLocations, ","),
398399
}
399400
switch volKey.Type() {
400401
case meta.Zonal:
@@ -490,11 +491,11 @@ type FakeBlockingCloudProvider struct {
490491
// Upon starting a CreateSnapshot, it passes a chan 'executeCreateSnapshot' into readyToExecute, then blocks on executeCreateSnapshot.
491492
// The test calling this function can block on readyToExecute to ensure that the operation has started and
492493
// allowed the CreateSnapshot to continue by passing a struct into executeCreateSnapshot.
493-
func (cloud *FakeBlockingCloudProvider) CreateSnapshot(ctx context.Context, volKey *meta.Key, snapshotName string) (*computev1.Snapshot, error) {
494+
func (cloud *FakeBlockingCloudProvider) CreateSnapshot(ctx context.Context, volKey *meta.Key, snapshotName string, storageLocations string) (*computev1.Snapshot, error) {
494495
executeCreateSnapshot := make(chan struct{})
495496
cloud.ReadyToExecute <- executeCreateSnapshot
496497
<-executeCreateSnapshot
497-
return cloud.FakeCloudProvider.CreateSnapshot(ctx, volKey, snapshotName)
498+
return cloud.FakeCloudProvider.CreateSnapshot(ctx, volKey, snapshotName, storageLocations)
498499
}
499500

500501
func notFoundError() *googleapi.Error {

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

+11-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, snapshotName string) (*computev1.Snapshot, error)
76-
CreateSnapshot(ctx context.Context, volKey *meta.Key, snapshotName string) (*computev1.Snapshot, error)
76+
CreateSnapshot(ctx context.Context, volKey *meta.Key, snapshotName string, storageLocations string) (*computev1.Snapshot, error)
7777
DeleteSnapshot(ctx context.Context, snapshotName string) error
7878
}
7979

@@ -793,13 +793,13 @@ func (cloud *CloudProvider) DeleteSnapshot(ctx context.Context, snapshotName str
793793
return nil
794794
}
795795

796-
func (cloud *CloudProvider) CreateSnapshot(ctx context.Context, volKey *meta.Key, snapshotName string) (*computev1.Snapshot, error) {
796+
func (cloud *CloudProvider) CreateSnapshot(ctx context.Context, volKey *meta.Key, snapshotName string, storageLocations string) (*computev1.Snapshot, error) {
797797
klog.V(5).Infof("Creating snapshot %s for volume %v", snapshotName, volKey)
798798
switch volKey.Type() {
799799
case meta.Zonal:
800-
return cloud.createZonalDiskSnapshot(ctx, volKey, snapshotName)
800+
return cloud.createZonalDiskSnapshot(ctx, volKey, snapshotName, storageLocations)
801801
case meta.Regional:
802-
return cloud.createRegionalDiskSnapshot(ctx, volKey, snapshotName)
802+
return cloud.createRegionalDiskSnapshot(ctx, volKey, snapshotName, storageLocations)
803803
default:
804804
return nil, fmt.Errorf("could not create snapshot, key was neither zonal nor regional, instead got: %v", volKey.String())
805805
}
@@ -870,9 +870,11 @@ func (cloud *CloudProvider) resizeRegionalDisk(ctx context.Context, volKey *meta
870870
return requestGb, nil
871871
}
872872

873-
func (cloud *CloudProvider) createZonalDiskSnapshot(ctx context.Context, volKey *meta.Key, snapshotName string) (*computev1.Snapshot, error) {
873+
func (cloud *CloudProvider) createZonalDiskSnapshot(ctx context.Context, volKey *meta.Key, snapshotName string,
874+
storageLocations string) (*computev1.Snapshot, error) {
874875
snapshotToCreate := &computev1.Snapshot{
875-
Name: snapshotName,
876+
Name: snapshotName,
877+
StorageLocations: strings.Split(storageLocations, ","),
876878
}
877879

878880
_, err := cloud.service.Disks.CreateSnapshot(cloud.project, volKey.Zone, volKey.Name, snapshotToCreate).Context(ctx).Do()
@@ -884,9 +886,10 @@ func (cloud *CloudProvider) createZonalDiskSnapshot(ctx context.Context, volKey
884886
return cloud.waitForSnapshotCreation(ctx, snapshotName)
885887
}
886888

887-
func (cloud *CloudProvider) createRegionalDiskSnapshot(ctx context.Context, volKey *meta.Key, snapshotName string) (*computev1.Snapshot, error) {
889+
func (cloud *CloudProvider) createRegionalDiskSnapshot(ctx context.Context, volKey *meta.Key, snapshotName string, storageLocations string) (*computev1.Snapshot, error) {
888890
snapshotToCreate := &computev1.Snapshot{
889-
Name: snapshotName,
891+
Name: snapshotName,
892+
StorageLocations: strings.Split(storageLocations, ","),
890893
}
891894

892895
_, err := cloud.service.RegionDisks.CreateSnapshot(cloud.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
@@ -593,7 +593,11 @@ func (gceCS *GCEControllerServer) CreateSnapshot(ctx context.Context, req *csi.C
593593
return nil, status.Error(codes.Internal, fmt.Sprintf("Unknown get snapshot error: %v", err))
594594
}
595595
// If we could not find the snapshot, we create a new one
596-
snapshot, err = gceCS.CloudProvider.CreateSnapshot(ctx, volKey, req.Name)
596+
parameters := make(map[string]string)
597+
if req.GetParameters() != nil {
598+
parameters = req.GetParameters()
599+
}
600+
snapshot, err = gceCS.CloudProvider.CreateSnapshot(ctx, volKey, req.Name, parameters[common.ParameterStorageLocations])
597601
if err != nil {
598602
if gce.IsGCEError(err, "notFound") {
599603
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
@@ -845,7 +845,11 @@ func TestCreateVolumeWithVolumeSource(t *testing.T) {
845845
}
846846

847847
if tc.snapshotOnCloud {
848-
gceDriver.cs.CloudProvider.CreateSnapshot(context.Background(), tc.volKey, name)
848+
parameters := make(map[string]string)
849+
if req.GetParameters() != nil {
850+
parameters = req.GetParameters()
851+
}
852+
gceDriver.cs.CloudProvider.CreateSnapshot(context.Background(), tc.volKey, name, parameters[common.ParameterStorageLocations])
849853
}
850854
resp, err := gceDriver.cs.CreateVolume(context.Background(), req)
851855
//check response

0 commit comments

Comments
 (0)