Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GCE: Get default project from gcloud CLI #4332

Merged
merged 2 commits into from
Jan 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions cmd/kops/create_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,9 +800,23 @@ func RunCreateCluster(f *util.Factory, out io.Writer, c *CreateClusterOptions) e
}
}

// Populate project
if c.Project != "" {
cluster.Spec.Project = c.Project
}
if api.CloudProviderID(cluster.Spec.CloudProvider) == api.CloudProviderGCE {
if cluster.Spec.Project == "" {
project, err := gce.DefaultProject()
if err != nil {
glog.Warningf("unable to get default google cloud project: %v", err)
} else if project == "" {
glog.Warningf("default google cloud project not set (try `gcloud config set project <name>`")
} else {
glog.Infof("using google cloud project: %s", project)
}
cluster.Spec.Project = project
}
}

if c.KubernetesVersion != "" {
cluster.Spec.KubernetesVersion = c.KubernetesVersion
Expand Down
36 changes: 34 additions & 2 deletions upup/pkg/fi/cloudup/gce/gce_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ limitations under the License.
package gce

import (
"bytes"
"fmt"
"net/http"
"strings"

"os"
"os/exec"
"strings"

"github.com/golang/glog"
"golang.org/x/net/context"
Expand Down Expand Up @@ -79,6 +80,37 @@ func (c *gceCloudImplementation) ProviderID() kops.CloudProviderID {

var gceCloudInstances map[string]GCECloud = make(map[string]GCECloud)

// DefaultProject returns the current project configured in the gcloud SDK, ("", nil) if no project was set
func DefaultProject() (string, error) {
// The default project isn't usually defined by the google cloud APIs,
// for example the Application Default Credential won't have ProjectID set.
// If we're running on a GCP instance, we can get it from the metadata service,
// but the normal kops CLI usage is running locally with gcloud configuration with a project,
// so we use that value.
cmd := exec.Command("gcloud", "config", "get-value", "project")

env := os.Environ()
cmd.Env = env
var stdout bytes.Buffer
cmd.Stdout = &stdout

var stderr bytes.Buffer
cmd.Stderr = &stderr

human := strings.Join(cmd.Args, " ")
glog.V(2).Infof("Running command: %s", human)
err := cmd.Run()
if err != nil {
glog.Infof("error running %s", human)
glog.Info(stdout.String())
glog.Info(stderr.String())
return "", fmt.Errorf("error running %s: %v", human, err)
}

projectID := strings.TrimSpace(stdout.String())
return projectID, err
}

func NewGCECloud(region string, project string, labels map[string]string) (GCECloud, error) {
i := gceCloudInstances[region+"::"+project]
if i != nil {
Expand Down