Skip to content

Commit

Permalink
Added gcp-login command
Browse files Browse the repository at this point in the history
  • Loading branch information
fd committed May 4, 2016
1 parent c3d3b4b commit 84d7cc3
Show file tree
Hide file tree
Showing 98 changed files with 25,119 additions and 1 deletion.
60 changes: 60 additions & 0 deletions cmd/k8s/gcp_login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"bytes"
"io/ioutil"
"os"
"path"
"text/template"

"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"gopkg.in/alecthomas/kingpin.v2"

raw "google.golang.org/api/container/v1"
)

func gcpLogin(app *kingpin.Application, gcpProjectID, gcpZone, gkeCluster string) {
client, err := google.DefaultClient(oauth2.NoContext)
app.FatalIfError(err, "k8s")

svc, err := raw.New(client)
app.FatalIfError(err, "k8s")

cluster, err := svc.Projects.Zones.Clusters.Get(gcpProjectID, gcpZone, gkeCluster).Do()
app.FatalIfError(err, "k8s")

var buf bytes.Buffer
err = k8sConfigTmpl.Execute(&buf, cluster)
app.FatalIfError(err, "k8s")

err = os.MkdirAll(path.Join(os.Getenv("HOME"), ".kube"), 0755)
app.FatalIfError(err, "k8s")

err = ioutil.WriteFile(path.Join(os.Getenv("HOME"), ".kube/config"), buf.Bytes(), 0600)
app.FatalIfError(err, "k8s")
}

var k8sConfigTmpl = template.Must(template.New("").Parse(`
apiVersion: v1
kind: Config
preferences: {}
current-context: k8s-context
- context:
cluster: k8s-cluster
user: k8s-user
name: k8s-context
clusters:
- cluster:
certificate-authority-data: {{.MasterAuth.ClusterCaCertificate}}
server: https://{{.Endpoint}}
name: k8s-cluster
contexts:
users:
- name: k8s-user
user:
client-certificate-data: {{.MasterAuth.ClientCertificate}}
client-key-data: {{.MasterAuth.ClientKey}}
username: {{.MasterAuth.Username}}
password: {{.MasterAuth.Password}}
`))
20 changes: 19 additions & 1 deletion cmd/k8s/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,25 @@ func main() {
secretDir string
secretMapName string
sourceFileName string
gcpProjectID string
gkeClusterName string
gceZoneName string
)

app := kingpin.New("k8s", "kubernetes utilities").Version(version.Get().String()).Author(version.Get().ReleasedBy)
app := kingpin.New("k8s", "kubernetes utilities").
Version(version.Get().String()).
Author(version.Get().ReleasedBy)

gcpLoginCmd := app.Command("gcp-login", "Login to a GKE cluster")
gcpLoginCmd.Flag("gcp-project", "GCP project id").Short('p').
PlaceHolder("GCP_PROJECT_ID").Envar("GCP_PROJECT_ID").
Required().StringVar(&gcpProjectID)
gcpLoginCmd.Flag("gce-zone", "GCE zone").Short('z').
PlaceHolder("GCE_ZONE").Envar("GCE_ZONE").
Required().StringVar(&gceZoneName)
gcpLoginCmd.Flag("gke-cluster", "GKE cluster name").Short('c').
PlaceHolder("GKE_CLUSTER").Envar("GKE_CLUSTER").
Required().StringVar(&gkeClusterName)

genConfigMapCmd := app.Command("gen-configmap", "Generate a ConfigMap from a directory")
genConfigMapCmd.Arg("config-directory", "Directory containing configuration").Default(".").ExistingDirVar(&configDir)
Expand All @@ -31,6 +47,8 @@ func main() {
genTemplateCmd.Arg("template", "Template file").Default(".").ExistingFileVar(&sourceFileName)

switch kingpin.MustParse(app.Parse(os.Args[1:])) {
case gcpLoginCmd.FullCommand():
gcpLogin(app, gcpProjectID, gceZoneName, gkeClusterName)
case genConfigMapCmd.FullCommand():
genConfigMap(app, configDir, configMapName)
case genSecretMapCmd.FullCommand():
Expand Down
31 changes: 31 additions & 0 deletions vendor/github.com/golang/protobuf/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions vendor/github.com/golang/protobuf/proto/Makefile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

223 changes: 223 additions & 0 deletions vendor/github.com/golang/protobuf/proto/clone.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 84d7cc3

Please sign in to comment.