diff --git a/velero-plugin-for-gcp/volume_snapshotter.go b/velero-plugin-for-gcp/volume_snapshotter.go index 19947a7..974259e 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) @@ -300,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 != "" { @@ -324,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 != "" { 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) }) }