Skip to content

Commit

Permalink
Merge pull request #173 from okuuva/support-archive-snapshots
Browse files Browse the repository at this point in the history
feat: add support for archive snapshot type
  • Loading branch information
blackpiglet authored Mar 19, 2024
2 parents 7b61900 + bfe0ef1 commit 07c222c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
30 changes: 23 additions & 7 deletions velero-plugin-for-gcp/volume_snapshotter.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const (
zoneSeparator = "__"
projectKey = "project"
snapshotLocationKey = "snapshotLocation"
snapshotTypeKey = "snapshotType"
volumeProjectKey = "volumeProject"
)

Expand All @@ -59,6 +60,7 @@ type VolumeSnapshotter struct {
snapshotLocation string
volumeProject string
snapshotProject string
snapshotType string
}

func newVolumeSnapshotter(logger logrus.FieldLogger) *VolumeSnapshotter {
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 != "" {
Expand All @@ -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 != "" {
Expand Down
19 changes: 19 additions & 0 deletions velero-plugin-for-gcp/volume_snapshotter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,19 +451,37 @@ func TestInit(t *testing.T) {
snapshotLocation: "default",
volumeProject: "project-b",
snapshotProject: "project-a",
snapshotType: "STANDARD",
},
},
{
name: "Init without Credential files.",
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",
},
},
}
Expand All @@ -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)
})
}

Expand Down

0 comments on commit 07c222c

Please sign in to comment.