Skip to content

Commit 294cc42

Browse files
Allow cross project snapshots
1 parent 1b79ce7 commit 294cc42

File tree

4 files changed

+15
-13
lines changed

4 files changed

+15
-13
lines changed

Diff for: pkg/common/utils.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,15 @@ func GenerateUnderspecifiedVolumeID(diskName string, isZonal bool) string {
100100
return fmt.Sprintf(volIDRegionalFmt, UnspecifiedValue, UnspecifiedValue, diskName)
101101
}
102102

103-
func SnapshotIDToKey(id string) (string, error) {
103+
func SnapshotIDToProjectKey(id string) (string, string, error) {
104104
splitId := strings.Split(id, "/")
105105
if len(splitId) != snapshotTotalElements {
106-
return "", fmt.Errorf("failed to get id components. Expected projects/{project}/global/snapshot/{name}. Got: %s", id)
106+
return "", "", fmt.Errorf("failed to get id components. Expected projects/{project}/global/snapshot/{name}. Got: %s", id)
107107
}
108108
if splitId[snapshotTopologyKey] == "global" {
109-
return splitId[snapshotTotalElements-1], nil
109+
return splitId[1], splitId[snapshotTotalElements-1], nil
110110
} else {
111-
return "", fmt.Errorf("could not get id components, expected global, got: %v", splitId[snapshotTopologyKey])
111+
return "", "", fmt.Errorf("could not get id components, expected global, got: %v", splitId[snapshotTopologyKey])
112112
}
113113
}
114114

Diff for: pkg/gce-cloud-provider/compute/fake-gce.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ func (cloud *FakeCloudProvider) GetInstanceOrError(ctx context.Context, instance
375375
}
376376

377377
// Snapshot Methods
378-
func (cloud *FakeCloudProvider) GetSnapshot(ctx context.Context, snapshotName string) (*computev1.Snapshot, error) {
378+
func (cloud *FakeCloudProvider) GetSnapshot(ctx context.Context, project, snapshotName string) (*computev1.Snapshot, error) {
379379
snapshot, ok := cloud.snapshots[snapshotName]
380380
if !ok {
381381
return nil, notFoundError()

Diff for: pkg/gce-cloud-provider/compute/gce-compute.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ type GCECompute interface {
7272
// Zone Methods
7373
ListZones(ctx context.Context, region string) ([]string, error)
7474
ListSnapshots(ctx context.Context, filter string, maxEntries int64, pageToken string) ([]*computev1.Snapshot, string, error)
75-
GetSnapshot(ctx context.Context, snapshotName string) (*computev1.Snapshot, error)
75+
GetSnapshot(ctx context.Context, project, snapshotName string) (*computev1.Snapshot, error)
7676
CreateSnapshot(ctx context.Context, volKey *meta.Key, snapshotName string) (*computev1.Snapshot, error)
7777
DeleteSnapshot(ctx context.Context, snapshotName string) error
7878
}
@@ -765,10 +765,12 @@ func (cloud *CloudProvider) GetInstanceOrError(ctx context.Context, instanceZone
765765
return instance, nil
766766
}
767767

768-
func (cloud *CloudProvider) GetSnapshot(ctx context.Context, snapshotName string) (*computev1.Snapshot, error) {
768+
func (cloud *CloudProvider) GetSnapshot(ctx context.Context, project, snapshotName string) (*computev1.Snapshot, error) {
769769
klog.V(5).Infof("Getting snapshot %v", snapshotName)
770770
svc := cloud.service
771-
project := cloud.project
771+
if project == "" {
772+
project = cloud.project
773+
}
772774
snapshot, err := svc.Snapshots.Get(project, snapshotName).Context(ctx).Do()
773775
if err != nil {
774776
return nil, err
@@ -908,7 +910,7 @@ func (cloud *CloudProvider) waitForSnapshotCreation(ctx context.Context, snapsho
908910
select {
909911
case <-ticker.C:
910912
klog.V(6).Infof("Checking GCE Snapshot %s.", snapshotName)
911-
snapshot, err := cloud.GetSnapshot(ctx, snapshotName)
913+
snapshot, err := cloud.GetSnapshot(ctx, "", snapshotName)
912914
if err != nil {
913915
klog.Warningf("Error in getting snapshot %s, %v", snapshotName, err)
914916
} else if snapshot != nil {

Diff for: pkg/gce-pd-csi-driver/controller.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ func (gceCS *GCEControllerServer) CreateSnapshot(ctx context.Context, req *csi.C
587587

588588
// Check if snapshot already exists
589589
var snapshot *compute.Snapshot
590-
snapshot, err = gceCS.CloudProvider.GetSnapshot(ctx, req.Name)
590+
snapshot, err = gceCS.CloudProvider.GetSnapshot(ctx, "", req.Name)
591591
if err != nil {
592592
if !gce.IsGCEError(err, "notFound") {
593593
return nil, status.Error(codes.Internal, fmt.Sprintf("Unknown get snapshot error: %v", err))
@@ -673,7 +673,7 @@ func (gceCS *GCEControllerServer) DeleteSnapshot(ctx context.Context, req *csi.D
673673
return nil, status.Error(codes.InvalidArgument, "DeleteSnapshot Snapshot ID must be provided")
674674
}
675675

676-
key, err := common.SnapshotIDToKey(snapshotID)
676+
_, key, err := common.SnapshotIDToProjectKey(snapshotID)
677677
if err != nil {
678678
// Cannot get snapshot ID from the passing request
679679
// This is a success according to the spec
@@ -759,14 +759,14 @@ func (gceCS *GCEControllerServer) getSnapshots(ctx context.Context, req *csi.Lis
759759
}
760760

761761
func (gceCS *GCEControllerServer) getSnapshotByID(ctx context.Context, snapshotID string) (*csi.ListSnapshotsResponse, error) {
762-
key, err := common.SnapshotIDToKey(snapshotID)
762+
project, key, err := common.SnapshotIDToProjectKey(snapshotID)
763763
if err != nil {
764764
// Cannot get snapshot ID from the passing request
765765
klog.Warningf("invalid snapshot id format %s", snapshotID)
766766
return &csi.ListSnapshotsResponse{}, nil
767767
}
768768

769-
snapshot, err := gceCS.CloudProvider.GetSnapshot(ctx, key)
769+
snapshot, err := gceCS.CloudProvider.GetSnapshot(ctx, project, key)
770770
if err != nil {
771771
if gce.IsGCEError(err, "notFound") {
772772
// return empty list if no snapshot is found

0 commit comments

Comments
 (0)