From 4ed63edea0ae61ce36406f62ded79e5dbc5d845a Mon Sep 17 00:00:00 2001 From: Steve Kriss Date: Thu, 25 Apr 2019 12:32:43 -0600 Subject: [PATCH] GCP: add optional 'project' config to volume snapshot location Signed-off-by: Steve Kriss --- changelogs/unreleased/1405-skriss | 1 + docs/api-types/volumesnapshotlocation.md | 1 + pkg/cloudprovider/gcp/volume_snapshotter.go | 18 ++++++++++++------ 3 files changed, 14 insertions(+), 6 deletions(-) create mode 100644 changelogs/unreleased/1405-skriss diff --git a/changelogs/unreleased/1405-skriss b/changelogs/unreleased/1405-skriss new file mode 100644 index 000000000..62f954430 --- /dev/null +++ b/changelogs/unreleased/1405-skriss @@ -0,0 +1 @@ +GCP: add optional 'project' config to volume snapshot location for if snapshots are in a different project than the IAM account diff --git a/docs/api-types/volumesnapshotlocation.md b/docs/api-types/volumesnapshotlocation.md index 7d287dc73..10cc61735 100644 --- a/docs/api-types/volumesnapshotlocation.md +++ b/docs/api-types/volumesnapshotlocation.md @@ -57,6 +57,7 @@ The configurable parameters are as follows: | Key | Type | Default | Meaning | | --- | --- | --- | --- | | `snapshotLocation` | string | Empty | *Example*: "us-central1"

See [GCP documentation][4] for the full list.

If not specified the snapshots are stored in the [default location][5]. | +| `project` | string | Empty | The project ID where snapshots should be stored, if different than the project that your IAM account is in. Optional. | [0]: #aws [1]: #gcp diff --git a/pkg/cloudprovider/gcp/volume_snapshotter.go b/pkg/cloudprovider/gcp/volume_snapshotter.go index 8a1c5dc63..d9fe8dc3c 100644 --- a/pkg/cloudprovider/gcp/volume_snapshotter.go +++ b/pkg/cloudprovider/gcp/volume_snapshotter.go @@ -38,8 +38,8 @@ import ( ) const ( - projectKey = "project" zoneSeparator = "__" + projectKey = "project" snapshotLocationKey = "snapshotLocation" ) @@ -55,15 +55,22 @@ func NewVolumeSnapshotter(logger logrus.FieldLogger) *VolumeSnapshotter { } func (b *VolumeSnapshotter) Init(config map[string]string) error { - if err := cloudprovider.ValidateVolumeSnapshotterConfigKeys(config, snapshotLocationKey); err != nil { + if err := cloudprovider.ValidateVolumeSnapshotterConfigKeys(config, snapshotLocationKey, projectKey); err != nil { return err } b.snapshotLocation = config[snapshotLocationKey] - project, err := extractProjectFromCreds() - if err != nil { - return err + // take 'project' from config if specified, otherwise get it + // from the credentials file + if val := config[projectKey]; val != "" { + b.project = val + } else { + project, err := extractProjectFromCreds() + if err != nil { + return err + } + b.project = project } client, err := google.DefaultClient(oauth2.NoContext, compute.ComputeScope) @@ -77,7 +84,6 @@ func (b *VolumeSnapshotter) Init(config map[string]string) error { } b.gce = gce - b.project = project return nil }