extract GCP project ID from creds file, remove from config

Signed-off-by: Steve Kriss <steve@heptio.com>
pull/345/head
Steve Kriss 2018-03-01 16:25:30 -08:00
parent 52641e8e29
commit cbff5ef53b
4 changed files with 32 additions and 12 deletions

View File

@ -86,9 +86,7 @@ No parameters required.
#### persistentVolumeProvider/config
| Key | Type | Default | Meaning |
| --- | --- | --- | --- |
| `project` | string | Required Field | *Example*: "project-example-3jsn23"<br><br> See the [Project ID documentation][4] for details. |
No parameters required.
### Azure
@ -107,7 +105,6 @@ No parameters required.
[1]: #gcp
[2]: #azure
[3]: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html#concepts-available-regions
[4]: https://cloud.google.com/resource-manager/docs/creating-managing-projects#identifying_projects
[5]: https://azure.microsoft.com/en-us/regions/
[6]: #parameter-reference
[7]: #main-config-parameters

View File

@ -82,7 +82,7 @@ Specify the following values in the example files:
* In file `examples/gcp/00-ark-config.yaml`:
* Replace `<YOUR_BUCKET>` and `<YOUR_PROJECT>`. See the [Config definition][7] for details.
* Replace `<YOUR_BUCKET>`. See the [Config definition][7] for details.
* In file `examples/common/10-deployment.yaml`:

View File

@ -20,8 +20,6 @@ metadata:
name: default
persistentVolumeProvider:
name: gcp
config:
project: <YOUR_PROJECT>
backupStorageProvider:
name: gcp
bucket: <YOUR_BUCKET>

View File

@ -17,6 +17,10 @@ limitations under the License.
package gcp
import (
"encoding/json"
"io/ioutil"
"os"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
"golang.org/x/oauth2"
@ -41,10 +45,9 @@ func NewBlockStore() cloudprovider.BlockStore {
}
func (b *blockStore) Init(config map[string]string) error {
project := config[projectKey]
if project == "" {
return errors.Errorf("missing %s in gcp configuration", projectKey)
project, err := extractProjectFromCreds()
if err != nil {
return err
}
client, err := google.DefaultClient(oauth2.NoContext, compute.ComputeScope)
@ -57,7 +60,7 @@ func (b *blockStore) Init(config map[string]string) error {
return errors.WithStack(err)
}
// validate project
// validate connection
res, err := gce.Projects.Get(project).Do()
if err != nil {
return errors.WithStack(err)
@ -73,6 +76,28 @@ func (b *blockStore) Init(config map[string]string) error {
return nil
}
func extractProjectFromCreds() (string, error) {
credsBytes, err := ioutil.ReadFile(os.Getenv("GOOGLE_APPLICATION_CREDENTIALS"))
if err != nil {
return "", errors.WithStack(err)
}
type credentials struct {
ProjectID string `json:"project_id"`
}
var creds credentials
if err := json.Unmarshal(credsBytes, &creds); err != nil {
return "", errors.WithStack(err)
}
if creds.ProjectID == "" {
return "", errors.New("cannot fetch project_id from GCP credentials file")
}
return creds.ProjectID, nil
}
func (b *blockStore) CreateVolumeFromSnapshot(snapshotID, volumeType, volumeAZ string, iops *int64) (volumeID string, err error) {
res, err := b.gce.Snapshots.Get(b.project, snapshotID).Do()
if err != nil {