From 17bc7486d59872f9032dff316bce46b43ba36c47 Mon Sep 17 00:00:00 2001 From: Oula Kuuva Date: Fri, 23 Feb 2024 14:26:00 +0200 Subject: [PATCH 1/3] feat: add support for archive snapshot type Signed-off-by: Oula Kuuva --- velero-plugin-for-gcp/volume_snapshotter.go | 16 +++++++++++++++- .../volume_snapshotter_test.go | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/velero-plugin-for-gcp/volume_snapshotter.go b/velero-plugin-for-gcp/volume_snapshotter.go index 19947a7..be2f76c 100644 --- a/velero-plugin-for-gcp/volume_snapshotter.go +++ b/velero-plugin-for-gcp/volume_snapshotter.go @@ -43,6 +43,7 @@ const ( zoneSeparator = "__" projectKey = "project" snapshotLocationKey = "snapshotLocation" + snapshotTypeKey = "snapshotType" volumeProjectKey = "volumeProject" ) @@ -59,6 +60,7 @@ type VolumeSnapshotter struct { snapshotLocation string volumeProject string snapshotProject string + snapshotType string } func newVolumeSnapshotter(logger logrus.FieldLogger) *VolumeSnapshotter { @@ -67,7 +69,7 @@ func newVolumeSnapshotter(logger logrus.FieldLogger) *VolumeSnapshotter { func (b *VolumeSnapshotter) Init(config map[string]string) error { if err := veleroplugin.ValidateVolumeSnapshotterConfigKeys(config, - snapshotLocationKey, projectKey, credentialsFileConfigKey, volumeProjectKey); err != nil { + snapshotLocationKey, snapshotTypeKey, projectKey, credentialsFileConfigKey, volumeProjectKey); err != nil { return err } @@ -116,6 +118,18 @@ func (b *VolumeSnapshotter) Init(config map[string]string) error { b.snapshotProject = b.volumeProject } + // get snapshot type from 'snapshotType' config key if specified, + // otherwise default to "STANDARD" + snapshotType := strings.ToUpper(config[snapshotTypeKey]) + switch snapshotType { + case "": + b.snapshotType = "STANDARD" + case "STANDARD", "ARCHIVE": + b.snapshotType = snapshotType + default: + return errors.Errorf("unsupported snapshot type: %q", snapshotType) + } + gce, err := compute.NewService(context.TODO(), clientOptions...) if err != nil { return errors.WithStack(err) diff --git a/velero-plugin-for-gcp/volume_snapshotter_test.go b/velero-plugin-for-gcp/volume_snapshotter_test.go index 6148454..2d4bcb5 100644 --- a/velero-plugin-for-gcp/volume_snapshotter_test.go +++ b/velero-plugin-for-gcp/volume_snapshotter_test.go @@ -451,6 +451,7 @@ func TestInit(t *testing.T) { snapshotLocation: "default", volumeProject: "project-b", snapshotProject: "project-a", + snapshotType: "STANDARD", }, }, { @@ -458,12 +459,29 @@ func TestInit(t *testing.T) { config: map[string]string{ "project": "project-a", "snapshotLocation": "default", + "snapshotType": "standard", "volumeProject": "project-b", }, expectedVolumeSnapshotter: VolumeSnapshotter{ snapshotLocation: "default", volumeProject: "project-b", snapshotProject: "project-a", + snapshotType: "STANDARD", + }, + }, + { + name: "Init with archive snapshot type.", + config: map[string]string{ + "project": "project-a", + "snapshotLocation": "default", + "snapshotType": "archive", + "volumeProject": "project-b", + }, + expectedVolumeSnapshotter: VolumeSnapshotter{ + snapshotLocation: "default", + volumeProject: "project-b", + snapshotProject: "project-a", + snapshotType: "ARCHIVE", }, }, } @@ -476,6 +494,7 @@ func TestInit(t *testing.T) { require.Equal(t, test.expectedVolumeSnapshotter.snapshotLocation, volumeSnapshotter.snapshotLocation) require.Equal(t, test.expectedVolumeSnapshotter.volumeProject, volumeSnapshotter.volumeProject) require.Equal(t, test.expectedVolumeSnapshotter.snapshotProject, volumeSnapshotter.snapshotProject) + require.Equal(t, test.expectedVolumeSnapshotter.snapshotType, volumeSnapshotter.snapshotType) }) } From beaaa5a0bd15ce6b2d76c35d6fdcc8654a7982ad Mon Sep 17 00:00:00 2001 From: Oula Kuuva Date: Fri, 23 Feb 2024 14:40:27 +0200 Subject: [PATCH 2/3] style: fix indenting How I managed to mess it up and not run gofmt, I don't know. Signed-off-by: Oula Kuuva --- velero-plugin-for-gcp/volume_snapshotter.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/velero-plugin-for-gcp/volume_snapshotter.go b/velero-plugin-for-gcp/volume_snapshotter.go index be2f76c..038fefe 100644 --- a/velero-plugin-for-gcp/volume_snapshotter.go +++ b/velero-plugin-for-gcp/volume_snapshotter.go @@ -118,8 +118,8 @@ func (b *VolumeSnapshotter) Init(config map[string]string) error { b.snapshotProject = b.volumeProject } - // get snapshot type from 'snapshotType' config key if specified, - // otherwise default to "STANDARD" + // get snapshot type from 'snapshotType' config key if specified, + // otherwise default to "STANDARD" snapshotType := strings.ToUpper(config[snapshotTypeKey]) switch snapshotType { case "": @@ -127,7 +127,7 @@ func (b *VolumeSnapshotter) Init(config map[string]string) error { case "STANDARD", "ARCHIVE": b.snapshotType = snapshotType default: - return errors.Errorf("unsupported snapshot type: %q", snapshotType) + return errors.Errorf("unsupported snapshot type: %q", snapshotType) } gce, err := compute.NewService(context.TODO(), clientOptions...) From bfe0ef14f7c8f5bf1e12ce11bf256f0faa36f3af Mon Sep 17 00:00:00 2001 From: Oula Kuuva Date: Mon, 26 Feb 2024 10:34:26 +0200 Subject: [PATCH 3/3] fix: actually use the snapshotType Oopsie daisie. Signed-off-by: Oula Kuuva --- velero-plugin-for-gcp/volume_snapshotter.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/velero-plugin-for-gcp/volume_snapshotter.go b/velero-plugin-for-gcp/volume_snapshotter.go index 038fefe..974259e 100644 --- a/velero-plugin-for-gcp/volume_snapshotter.go +++ b/velero-plugin-for-gcp/volume_snapshotter.go @@ -314,9 +314,10 @@ func (b *VolumeSnapshotter) createSnapshot(snapshotName, volumeID, volumeAZ stri } gceSnap := compute.Snapshot{ - Name: snapshotName, - Description: getSnapshotTags(tags, disk.Description, b.log), - SourceDisk: disk.SelfLink, + Name: snapshotName, + Description: getSnapshotTags(tags, disk.Description, b.log), + SourceDisk: disk.SelfLink, + SnapshotType: b.snapshotType, } if b.snapshotLocation != "" { @@ -338,9 +339,10 @@ func (b *VolumeSnapshotter) createRegionSnapshot(snapshotName, volumeID, volumeR } gceSnap := compute.Snapshot{ - Name: snapshotName, - Description: getSnapshotTags(tags, disk.Description, b.log), - SourceDisk: disk.SelfLink, + Name: snapshotName, + Description: getSnapshotTags(tags, disk.Description, b.log), + SourceDisk: disk.SelfLink, + SnapshotType: b.snapshotType, } if b.snapshotLocation != "" {