Skip to content

switch to use snapshots.insert instead of disks.createsnapshot #1857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ metadata:
name: imagetag-csi-snapshotter-prow-head
imageTag:
name: registry.k8s.io/sig-storage/csi-snapshotter
newTag: "v6.3.3"
newTag: "v7.0.2"
---
apiVersion: builtin
kind: ImageTagTransformer
Expand Down
2 changes: 1 addition & 1 deletion deploy/kubernetes/images/stable-master/image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ metadata:
name: imagetag-csi-snapshotter
imageTag:
name: registry.k8s.io/sig-storage/csi-snapshotter
newTag: "v6.3.3"
newTag: "v7.0.2"
---

apiVersion: builtin
Expand Down
78 changes: 25 additions & 53 deletions pkg/gce-cloud-provider/compute/gce-compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -1264,9 +1264,8 @@ func opIsDone(op *computev1.Operation) (bool, error) {

func (cloud *CloudProvider) GetInstanceOrError(ctx context.Context, instanceZone, instanceName string) (*computev1.Instance, error) {
klog.V(5).Infof("Getting instance %v from zone %v", instanceName, instanceZone)
svc := cloud.service
project := cloud.project
instance, err := svc.Instances.Get(project, instanceZone, instanceName).Do()
instance, err := cloud.service.Instances.Get(project, instanceZone, instanceName).Do()
if err != nil {
return nil, err
}
Expand All @@ -1275,9 +1274,9 @@ func (cloud *CloudProvider) GetInstanceOrError(ctx context.Context, instanceZone

func (cloud *CloudProvider) GetSnapshot(ctx context.Context, project, snapshotName string) (*computev1.Snapshot, error) {
klog.V(5).Infof("Getting snapshot %v", snapshotName)
svc := cloud.service
snapshot, err := svc.Snapshots.Get(project, snapshotName).Context(ctx).Do()
snapshot, err := cloud.service.Snapshots.Get(project, snapshotName).Context(ctx).Do()
if err != nil {
klog.V(5).Infof("Error getting snapshot %v: %v", snapshotName, err)
return nil, err
}
return snapshot, nil
Expand Down Expand Up @@ -1313,15 +1312,34 @@ func (cloud *CloudProvider) CreateSnapshot(ctx context.Context, project string,
if description == "" {
description = "Snapshot created by GCE-PD CSI Driver"
}
return cloud.createZonalDiskSnapshot(ctx, project, volKey, snapshotName, snapshotParams, description)
case meta.Regional:
if description == "" {
description = "Regional Snapshot created by GCE-PD CSI Driver"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like you're setting description on the request. Also I'd invert the description empty string check, and the volKey Type check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you mean by

Also I'd invert the description empty string check, and the volKey Type check.

Thanks for the description catch.

}
return cloud.createRegionalDiskSnapshot(ctx, project, volKey, snapshotName, snapshotParams, description)
default:
return nil, fmt.Errorf("could not create snapshot, key was neither zonal nor regional, instead got: %v", volKey.String())
}

snapshotToCreate := &computev1.Snapshot{
Name: snapshotName,
StorageLocations: snapshotParams.StorageLocations,
Description: description,
Labels: snapshotParams.Labels,
SourceDisk: cloud.GetDiskSourceURI(project, volKey),
}
_, err = cloud.service.Snapshots.Insert(project, snapshotToCreate).Context(ctx).Do()

if err != nil {
return nil, err
}

snapshot, err := cloud.waitForSnapshotCreation(ctx, project, snapshotName)

if err == nil {
err = cloud.attachTagsToResource(ctx, snapshotParams.ResourceTags, project, snapshot.Id, snapshotsType, "", false, resourceManagerHostSubPath)
}

return snapshot, err
}

func (cloud *CloudProvider) CreateImage(ctx context.Context, project string, volKey *meta.Key, imageName string, snapshotParams common.SnapshotParameters) (*computev1.Image, error) {
Expand Down Expand Up @@ -1497,52 +1515,6 @@ func (cloud *CloudProvider) resizeRegionalDisk(ctx context.Context, project stri
return requestGb, nil
}

func (cloud *CloudProvider) createZonalDiskSnapshot(ctx context.Context, project string, volKey *meta.Key, snapshotName string, snapshotParams common.SnapshotParameters, description string) (*computev1.Snapshot, error) {
snapshotToCreate := &computev1.Snapshot{
Name: snapshotName,
StorageLocations: snapshotParams.StorageLocations,
Description: description,
Labels: snapshotParams.Labels,
}

_, err := cloud.service.Disks.CreateSnapshot(project, volKey.Zone, volKey.Name, snapshotToCreate).Context(ctx).Do()

if err != nil {
return nil, err
}

snapshot, err := cloud.waitForSnapshotCreation(ctx, project, snapshotName)

if err == nil {
err = cloud.attachTagsToResource(ctx, snapshotParams.ResourceTags, project, snapshot.Id, snapshotsType, "", false, resourceManagerHostSubPath)
}

return snapshot, err
}

func (cloud *CloudProvider) createRegionalDiskSnapshot(ctx context.Context, project string, volKey *meta.Key, snapshotName string, snapshotParams common.SnapshotParameters, description string) (*computev1.Snapshot, error) {
snapshotToCreate := &computev1.Snapshot{
Name: snapshotName,
StorageLocations: snapshotParams.StorageLocations,
Description: description,
Labels: snapshotParams.Labels,
}

_, err := cloud.service.RegionDisks.CreateSnapshot(project, volKey.Region, volKey.Name, snapshotToCreate).Context(ctx).Do()
if err != nil {
return nil, err
}

snapshot, err := cloud.waitForSnapshotCreation(ctx, project, snapshotName)

if err == nil {
err = cloud.attachTagsToResource(ctx, snapshotParams.ResourceTags, project, snapshot.Id, snapshotsType, "", false, resourceManagerHostSubPath)
}

return snapshot, err

}

func (cloud *CloudProvider) waitForSnapshotCreation(ctx context.Context, project, snapshotName string) (*computev1.Snapshot, error) {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
Expand All @@ -1558,7 +1530,7 @@ func (cloud *CloudProvider) waitForSnapshotCreation(ctx context.Context, project
klog.Warningf("Error in getting snapshot %s, %v", snapshotName, err.Error())
} else if snapshot != nil {
if snapshot.Status != "CREATING" {
klog.V(6).Infof("Snapshot %s status is %s", snapshotName, snapshot.Status)
klog.V(5).Infof("Snapshot %s status is %s", snapshotName, snapshot.Status)
return snapshot, nil
} else {
klog.V(6).Infof("Snapshot %s is still creating ...", snapshotName)
Expand Down