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

Use access token to authenticate with the Google Cloud. #83

Merged
merged 3 commits into from
Jul 27, 2023
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
4 changes: 3 additions & 1 deletion cmd/weaver-gke-local/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package main

import (
"context"
"flag"
"net/http"

"github.com/ServiceWeaver/weaver-gke/internal/config"
Expand All @@ -25,7 +26,8 @@ import (
)

var deploySpec = tool.DeploySpec{
Tool: "weaver gke-local",
Tool: "weaver gke-local",
Flags: flag.NewFlagSet("deploy", flag.ContinueOnError),
Controller: func(ctx context.Context, _ *config.GKEConfig) (string, *http.Client, error) {
return local.Controller(ctx)
},
Expand Down
39 changes: 12 additions & 27 deletions cmd/weaver-gke/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,25 @@ import (
"net/url"
"os"
"strings"
"sync"

"github.com/ServiceWeaver/weaver-gke/internal/gke"
"github.com/ServiceWeaver/weaver-gke/internal/tool"
"github.com/ServiceWeaver/weaver/runtime/logging"
)

var (
dashboardFlags = flag.NewFlagSet("dashboard", flag.ContinueOnError)
dashboardProject = dashboardFlags.String("project", "", "Google Cloud project")
dashboardAccount = dashboardFlags.String("account", "", "Google Cloud user account")

// See setupCloudConfig.
cloudConfig gke.CloudConfig
cloudConfigErr error
configInit sync.Once
dashboardFlags = newCloudFlagSet("dashboard", flag.ContinueOnError)
)

// setupCloudConfig caches and returns the cloud config returned by
// gke.SetupCloudConfig called on dashboardProject and dashboardAccount.
func setupCloudConfig() (gke.CloudConfig, error) {
configInit.Do(func() {
flag.Parse()
cloudConfig, cloudConfigErr = gke.SetupCloudConfig(*dashboardProject, *dashboardAccount)
})
return cloudConfig, cloudConfigErr
}

var dashboardSpec = tool.DashboardSpec{
Tool: "weaver gke",
Flags: dashboardFlags,
Flags: dashboardFlags.FlagSet,
Controller: func(ctx context.Context) (string, *http.Client, error) {
config, err := setupCloudConfig()
config, err := dashboardFlags.CloudConfig()
if err != nil {
return "", nil, err
}
fmt.Fprintf(os.Stderr, "Using account %s in project %s", config.Account, config.Project)
fmt.Fprintf(os.Stderr, "Using project %s", config.Project)
return gke.Controller(ctx, config)
},
AppLinks: func(ctx context.Context, app string) (tool.Links, error) {
Expand Down Expand Up @@ -90,11 +72,14 @@ var dashboardSpec = tool.DashboardSpec{
// links are provided for the app.
func links(app, version string) (tool.Links, error) {
// Get the account and project.
config, err := setupCloudConfig()
config, err := dashboardFlags.CloudConfig()
if err != nil {
return tool.Links{}, err
}
account := url.QueryEscape(config.Account)
authuser := "0"
if config.Account != "" {
authuser = url.QueryEscape(config.Account)
}
project := url.QueryEscape(config.Project)

// Form a Google Cloud Logging query that matches this version's logs.
Expand Down Expand Up @@ -126,9 +111,9 @@ func links(app, version string) (tool.Links, error) {
}
traceQuery := url.QueryEscape(fmt.Sprintf(`("traceFilter":("chips":"%s"))`, traceFilter))
return tool.Links{
Metrics: fmt.Sprintf("https://console.cloud.google.com/monitoring/metrics-explorer?authuser=%s&project=%s", account, project),
Logs: fmt.Sprintf("https://console.cloud.google.com/logs/query?authuser=%s&project=%s&query=%s", account, project, logQuery),
Traces: fmt.Sprintf("https://console.cloud.google.com/traces/list?authuser=%s&project=%s&pageState=%s", account, project, traceQuery),
Metrics: fmt.Sprintf("https://console.cloud.google.com/monitoring/metrics-explorer?authuser=%s&project=%s", authuser, project),
Logs: fmt.Sprintf("https://console.cloud.google.com/logs/query?authuser=%s&project=%s&query=%s", authuser, project, logQuery),
Traces: fmt.Sprintf("https://console.cloud.google.com/traces/list?authuser=%s&project=%s&pageState=%s", authuser, project, traceQuery),
}, nil
}

Expand Down
65 changes: 35 additions & 30 deletions cmd/weaver-gke/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package main

import (
"context"
"flag"
"fmt"
"net/http"
"os"
Expand All @@ -29,36 +30,40 @@ import (
"github.com/ServiceWeaver/weaver/runtime/logging"
)

var deploySpec = tool.DeploySpec{
Tool: "weaver gke",
Controller: func(ctx context.Context, cfg *config.GKEConfig) (string, *http.Client, error) {
config, err := gke.SetupCloudConfig(cfg.Project, cfg.Account)
if err != nil {
return "", nil, err
}
return gke.Controller(ctx, config)
},
PrepareRollout: func(ctx context.Context, cfg *config.GKEConfig) (*controller.RolloutRequest, error) {
config, err := gke.SetupCloudConfig(cfg.Project, cfg.Account)
if err != nil {
return nil, err
}
fmt.Fprintf(os.Stderr, "Using account %s in project %s\n",
config.Account, config.Project)
toolBinVersion, err := getToolVersion()
if err != nil {
return nil, fmt.Errorf("error extracting the tool binary version: %w", err)
}
return gke.PrepareRollout(ctx, config, cfg, toolBinVersion)
},
Source: func(ctx context.Context, cfg *config.GKEConfig) (logging.Source, error) {
config, err := gke.SetupCloudConfig(cfg.Project, cfg.Account)
if err != nil {
return nil, err
}
return gke.LogSource(config)
},
}
var (
deployFlags = newCloudFlagSet("deploy", flag.ContinueOnError)

deploySpec = tool.DeploySpec{
Tool: "weaver gke",
Flags: deployFlags.FlagSet,
Controller: func(ctx context.Context, cfg *config.GKEConfig) (string, *http.Client, error) {
config, err := deployFlags.CloudConfig()
if err != nil {
return "", nil, err
}
return gke.Controller(ctx, config)
},
PrepareRollout: func(ctx context.Context, cfg *config.GKEConfig) (*controller.RolloutRequest, error) {
config, err := deployFlags.CloudConfig()
if err != nil {
return nil, err
}
fmt.Fprintf(os.Stderr, "Deploying to project %s\n", config.Project)
toolBinVersion, err := getToolVersion()
if err != nil {
return nil, fmt.Errorf("error extracting the tool binary version: %w", err)
}
return gke.PrepareRollout(ctx, config, cfg, toolBinVersion)
},
Source: func(ctx context.Context, cfg *config.GKEConfig) (logging.Source, error) {
config, err := deployFlags.CloudConfig()
if err != nil {
return nil, err
}
return gke.LogSource(config)
},
}
)

// getToolVersion returns the version of the tool binary.
func getToolVersion() (string, error) {
Expand Down
59 changes: 59 additions & 0 deletions cmd/weaver-gke/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"flag"
"sync"

"github.com/ServiceWeaver/weaver-gke/internal/gke"
)

type cloudFlagSet struct {
spetrovic77 marked this conversation as resolved.
Show resolved Hide resolved
*flag.FlagSet

// Predefined cloud flags.
projectFlag, accessTokenFlag, accountFlag *string

// Initialized lazily.
configInit sync.Once
config gke.CloudConfig
configErr error
}

func newCloudFlagSet(name string, errorHandling flag.ErrorHandling) *cloudFlagSet {
fset := &cloudFlagSet{FlagSet: flag.NewFlagSet(name, errorHandling)}
fset.projectFlag = fset.String("project", "", `
Google Cloud project. If empty, the command will use the project name in the
active gcloud configuration on the local machine.`)
fset.accessTokenFlag = fset.String("access_token", "", `
Google Cloud access token. If empty, the command will acquire cloud access
by using the account name.`)
fset.accountFlag = fset.String("account", "", `
Google Cloud user account. If empty, the command will use the account name
in the active gcloud configuration on the local machine.`)
return fset
}

// CloudConfig returns the cloud configuration file that corresponds to the
// predefined cloud flag values.
func (fs *cloudFlagSet) CloudConfig() (gke.CloudConfig, error) {
fs.configInit.Do(func() {
flag.Parse()
fs.config, fs.configErr = gke.SetupCloudConfig(
*fs.projectFlag, *fs.accessTokenFlag, *fs.accountFlag)
})
return fs.config, fs.configErr
}
10 changes: 4 additions & 6 deletions cmd/weaver-gke/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,18 @@ import (
)

var (
killFlags = flag.NewFlagSet("kill", flag.ContinueOnError)
killProject = killFlags.String("project", "", "Google Cloud project")
killAccount = killFlags.String("account", "", "Google Cloud user account")
killFlags = newCloudFlagSet("kill", flag.ContinueOnError)
)

var killSpec = tool.KillSpec{
Tool: "weaver gke",
Flags: killFlags,
Flags: killFlags.FlagSet,
Controller: func(ctx context.Context) (string, *http.Client, error) {
config, err := gke.SetupCloudConfig(*killProject, *killAccount)
config, err := killFlags.CloudConfig()
if err != nil {
return "", nil, err
}
fmt.Fprintf(os.Stderr, "Using account %s in project %s\n", config.Account, config.Project)
fmt.Fprintf(os.Stderr, "Using project %s\n", config.Project)
return gke.Controller(ctx, config)
},
}
11 changes: 5 additions & 6 deletions cmd/weaver-gke/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ import (
)

var (
logsFlags = flag.NewFlagSet("logs", flag.ContinueOnError)
logsProject = logsFlags.String("project", "", "Google Cloud project")
logsAccount = logsFlags.String("account", "", "Google Cloud user account")
logsFlags = newCloudFlagSet("logs", flag.ContinueOnError)

// NOTE(mwhittaker): The Cloud Logging team informed us that they're
// working on speeding up queries over large time ranges, so in the future,
// we may be able to remove this flag.
Expand All @@ -39,7 +38,7 @@ var (
logsFreshness = logsFlags.String("freshness", "24h", "Only show logs that are this fresh (e.g., 300ms, 1.5h, 2h45m)")
logsSpec = tool.LogsSpec{
Tool: "weaver gke",
Flags: logsFlags,
Flags: logsFlags.FlagSet,
Rewrite: func(q logging.Query) (logging.Query, error) {
hasTime, err := gke.HasTime(q)
if err != nil {
Expand Down Expand Up @@ -70,11 +69,11 @@ a time based predicate in your query to override this behavior.
return q, nil
},
Source: func(context.Context) (logging.Source, error) {
config, err := gke.SetupCloudConfig(*logsProject, *logsAccount)
config, err := logsFlags.CloudConfig()
if err != nil {
return nil, err
}
fmt.Fprintf(os.Stderr, "Using account %s in project %s\n", config.Account, config.Project)
fmt.Fprintf(os.Stderr, "Using project %s\n", config.Project)
return gke.LogSource(config)
},
}
Expand Down
10 changes: 4 additions & 6 deletions cmd/weaver-gke/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,18 @@ import (
)

var (
profileFlags = flag.NewFlagSet("profile", flag.ContinueOnError)
profileProject = profileFlags.String("project", "", "Google Cloud project")
profileAccount = profileFlags.String("account", "", "Google Cloud user account")
profileFlags = newCloudFlagSet("profile", flag.ContinueOnError)
)

var profileSpec = tool.ProfileSpec{
Tool: "weaver gke",
Flags: profileFlags,
Flags: profileFlags.FlagSet,
Controller: func(ctx context.Context) (string, *http.Client, error) {
config, err := gke.SetupCloudConfig(*profileProject, *profileAccount)
config, err := profileFlags.CloudConfig()
if err != nil {
return "", nil, err
}
fmt.Fprintf(os.Stderr, "Using account %s in project %s\n", config.Account, config.Project)
fmt.Fprintf(os.Stderr, "Using project %s\n", config.Project)
return gke.Controller(ctx, config)
},
}
8 changes: 4 additions & 4 deletions cmd/weaver-gke/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ import (
)

var (
purgeFlags = flag.NewFlagSet("purge", flag.ContinueOnError)
purgeFlags = newCloudFlagSet("purge", flag.ContinueOnError)
purgeForce = purgeFlags.Bool("force", false, "Purge without prompt")
)

var purgeCmd = tool.Command{
Name: "purge",
Flags: purgeFlags,
Flags: purgeFlags.FlagSet,
Description: "Purge cloud resources",
Help: fmt.Sprintf(`Usage:
weaver gke purge [--force]
Expand All @@ -45,7 +45,7 @@ Flags:
Description:
"weaver gke purge" deletes all cloud resources created by "weaver gke
deploy". This terminates all running jobs and deletes all data.`,
tool.FlagsHelp(purgeFlags)),
tool.FlagsHelp(purgeFlags.FlagSet)),

Fn: purge,
// TODO(mwhittaker): Unhide the purge command when it's fully implemented.
Expand Down Expand Up @@ -74,7 +74,7 @@ Enter (y)es to continue: `)
}
}

config, err := gke.SetupCloudConfig("", "")
config, err := purgeFlags.CloudConfig()
if err != nil {
return nil
}
Expand Down
10 changes: 4 additions & 6 deletions cmd/weaver-gke/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,18 @@ import (
)

var (
statusFlags = flag.NewFlagSet("status", flag.ContinueOnError)
statusProject = statusFlags.String("project", "", "Google Cloud project")
statusAccount = statusFlags.String("account", "", "Google Cloud user account")
statusFlags = newCloudFlagSet("status", flag.ContinueOnError)
)

var statusSpec = tool.StatusSpec{
Tool: "weaver gke",
Flags: statusFlags,
Flags: statusFlags.FlagSet,
Controller: func(ctx context.Context) (string, *http.Client, error) {
config, err := gke.SetupCloudConfig(*statusProject, *statusAccount)
config, err := statusFlags.CloudConfig()
if err != nil {
return "", nil, err
}
fmt.Fprintf(os.Stderr, "Using account %s in project %s", config.Account, config.Project)
fmt.Fprintf(os.Stderr, "Using project %s", config.Project)
return gke.Controller(ctx, config)
},
}
Loading