Add config parameter for gcp to customize the snapshot location

Signed-off-by: Cyrill Troxler <cyrill@nine.ch>
This commit is contained in:
Cyrill Troxler
2019-04-17 18:34:09 +02:00
parent ff642d739d
commit 6020823aaf
4 changed files with 31 additions and 7 deletions
+18 -6
View File
@@ -38,14 +38,16 @@ import (
)
const (
projectKey = "project"
zoneSeparator = "__"
projectKey = "project"
zoneSeparator = "__"
snapshotLocationKey = "snapshotLocation"
)
type VolumeSnapshotter struct {
gce *compute.Service
project string
log logrus.FieldLogger
gce *compute.Service
project string
log logrus.FieldLogger
snapshotLocation string
}
func NewVolumeSnapshotter(logger logrus.FieldLogger) *VolumeSnapshotter {
@@ -53,10 +55,12 @@ func NewVolumeSnapshotter(logger logrus.FieldLogger) *VolumeSnapshotter {
}
func (b *VolumeSnapshotter) Init(config map[string]string) error {
if err := cloudprovider.ValidateVolumeSnapshotterConfigKeys(config); err != nil {
if err := cloudprovider.ValidateVolumeSnapshotterConfigKeys(config, snapshotLocationKey); err != nil {
return err
}
b.snapshotLocation = config[snapshotLocationKey]
project, err := extractProjectFromCreds()
if err != nil {
return err
@@ -249,6 +253,10 @@ func (b *VolumeSnapshotter) createSnapshot(snapshotName, volumeID, volumeAZ stri
Description: getSnapshotTags(tags, disk.Description, b.log),
}
if b.snapshotLocation != "" {
gceSnap.StorageLocations = []string{b.snapshotLocation}
}
_, err = b.gce.Disks.CreateSnapshot(b.project, volumeAZ, volumeID, &gceSnap).Do()
if err != nil {
return "", errors.WithStack(err)
@@ -268,6 +276,10 @@ func (b *VolumeSnapshotter) createRegionSnapshot(snapshotName, volumeID, volumeR
Description: getSnapshotTags(tags, disk.Description, b.log),
}
if b.snapshotLocation != "" {
gceSnap.StorageLocations = []string{b.snapshotLocation}
}
_, err = b.gce.RegionDisks.CreateSnapshot(b.project, volumeRegion, volumeID, &gceSnap).Do()
if err != nil {
return "", errors.WithStack(err)