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

feat: add testkube cloud init #4042

Merged
merged 1 commit into from
Jun 15, 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
1 change: 1 addition & 0 deletions cmd/kubectl-testkube/commands/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func NewCloudCmd() *cobra.Command {

cmd.AddCommand(cloud.NewConnectCmd())
cmd.AddCommand(cloud.NewDisconnectCmd())
cmd.AddCommand(cloud.NewInitCmd())

return cmd
}
2 changes: 1 addition & 1 deletion cmd/kubectl-testkube/commands/cloud/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func NewConnectCmd() *cobra.Command {
}

spinner := ui.NewSpinner("Connecting Testkube Cloud")
err = common.HelmUpgradeOrInstallTestkubeCloud(opts, cfg)
err = common.HelmUpgradeOrInstallTestkubeCloud(opts, cfg, true)
ui.ExitOnError("Installing Testkube Cloud", err)
spinner.Success()

Expand Down
83 changes: 83 additions & 0 deletions cmd/kubectl-testkube/commands/cloud/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package cloud

import (
"github.com/spf13/cobra"

"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common"
"github.com/kubeshop/testkube/cmd/kubectl-testkube/config"
"github.com/kubeshop/testkube/pkg/ui"
)

func NewInitCmd() *cobra.Command {
options := common.HelmOptions{
NoMinio: true,
NoMongo: true,
NoDashboard: true,
}

cmd := &cobra.Command{
Use: "init",
Short: "Install Helm chart registry in current kubectl context and update dependencies",
Aliases: []string{"install"},
Run: func(cmd *cobra.Command, args []string) {
ui.Info("WELCOME TO")
ui.Logo()

cfg, err := config.Load()
ui.ExitOnError("loading config file", err)
ui.NL()

// create new cloud uris
options.CloudUris = common.NewCloudUris(options.CloudRootDomain)
if !options.NoConfirm {
ui.Warn("This will install Testkube to the latest version. This may take a few minutes.")
ui.Warn("Please be sure you're on valid kubectl context before continuing!")
ui.NL()

currentContext, err := common.GetCurrentKubernetesContext()
ui.ExitOnError("getting current context", err)
ui.Alert("Current kubectl context:", currentContext)
ui.NL()

ok := ui.Confirm("Do you want to continue?")
if !ok {
ui.Errf("Testkube installation cancelled")
return
}
}

spinner := ui.NewSpinner("Installing Testkube")
err = common.HelmUpgradeOrInstallTestkubeCloud(options, cfg, false)
ui.ExitOnError("Installing Testkube", err)
spinner.Success()

ui.NL()

ui.H2("Saving testkube cli cloud context")
token, err := LoginUser(options)
ui.ExitOnError("user login", err)
err = common.PopulateLoginDataToContext(options.CloudOrgId, options.CloudEnvId, token, options, cfg)
ui.ExitOnError("Setting cloud environment context", err)

ui.Info(" Happy Testing! 🚀")
ui.NL()

},
}

cmd.Flags().StringVar(&options.Chart, "chart", "kubeshop/testkube", "chart name (usually you don't need to change it)")
cmd.Flags().StringVar(&options.Name, "name", "testkube", "installation name (usually you don't need to change it)")
cmd.Flags().StringVar(&options.Namespace, "namespace", "testkube", "namespace where to install")
cmd.Flags().StringVar(&options.Values, "values", "", "path to Helm values file")

cmd.Flags().StringVar(&options.CloudAgentToken, "agent-token", "", "Testkube Cloud agent key")
cmd.Flags().StringVar(&options.CloudOrgId, "org-id", "", "Testkube Cloud organization id")
cmd.Flags().StringVar(&options.CloudEnvId, "env-id", "", "Testkube Cloud environment id")

cmd.Flags().StringVar(&options.CloudRootDomain, "cloud-root-domain", "testkube.io", "defaults to testkube.io, usually don't need to be changed [required for cloud mode]")

cmd.Flags().BoolVar(&options.NoConfirm, "no-confirm", false, "don't ask for confirmation - unatended installation mode")
cmd.Flags().BoolVar(&options.DryRun, "dry-run", false, "dry run mode - only print commands that would be executed")

return cmd
}
22 changes: 20 additions & 2 deletions cmd/kubectl-testkube/commands/common/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func GetCurrentKubernetesContext() (string, error) {
return strings.TrimSpace(string(out)), nil
}

func HelmUpgradeOrInstallTestkubeCloud(options HelmOptions, cfg config.Data) error {
func HelmUpgradeOrInstallTestkubeCloud(options HelmOptions, cfg config.Data, isMigration bool) error {
// use config if set
if cfg.CloudContext.AgentKey != "" && options.CloudAgentToken == "" {
options.CloudAgentToken = cfg.CloudContext.AgentKey
Expand Down Expand Up @@ -77,7 +77,16 @@ func HelmUpgradeOrInstallTestkubeCloud(options HelmOptions, cfg config.Data) err
"--namespace", options.Namespace,
"--set", "testkube-api.cloud.url=" + options.CloudUris.Agent,
"--set", "testkube-api.cloud.key=" + options.CloudAgentToken,
"--set", "testkube-api.cloud.migrate=true",
}
if isMigration {
args = append(args, "--set", "testkube-api.cloud.migrate=true")
}

if options.CloudEnvId != "" {
args = append(args, "--set", fmt.Sprintf("testkube-api.cloud.envId=%s", options.CloudEnvId))
}
if options.CloudOrgId != "" {
args = append(args, "--set", fmt.Sprintf("testkube-api.cloud.orgId=%s", options.CloudOrgId))
}

args = append(args, "--set", fmt.Sprintf("testkube-dashboard.enabled=%t", !options.NoDashboard))
Expand Down Expand Up @@ -212,6 +221,15 @@ func PopulateAgentDataToContext(options HelmOptions, cfg config.Data) error {
cfg.CloudContext.ApiKey = options.CloudIdToken
updated = true
}
if options.CloudEnvId != "" {
cfg.CloudContext.EnvironmentId = options.CloudEnvId
updated = true
}
if options.CloudOrgId != "" {
cfg.CloudContext.OrganizationId = options.CloudOrgId
updated = true
}

if updated {
return config.Save(cfg)
}
Expand Down
35 changes: 5 additions & 30 deletions cmd/kubectl-testkube/commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"github.com/spf13/cobra"

"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common"
"github.com/kubeshop/testkube/cmd/kubectl-testkube/config"
"github.com/kubeshop/testkube/pkg/ui"
"github.com/kubeshop/testkube/pkg/utils/text"
)

func NewInitCmd() *cobra.Command {
Expand All @@ -20,17 +18,8 @@ func NewInitCmd() *cobra.Command {
ui.Info("WELCOME TO")
ui.Logo()

cfg, err := config.Load()
ui.ExitOnError("loading config file", err)
ui.NL()

// set to cloud context explicitly when user pass agent key and store the key later
if options.CloudAgentToken != "" {
cfg.CloudContext.AgentKey = options.CloudAgentToken
cfg.ContextType = config.ContextTypeCloud
options.CloudUris = common.NewCloudUris(options.CloudRootDomain)
}

if !options.NoConfirm {
ui.Warn("This will install Testkube to the latest version. This may take a few minutes.")
ui.Warn("Please be sure you're on valid kubectl context before continuing!")
Expand All @@ -41,35 +30,21 @@ func NewInitCmd() *cobra.Command {
ui.Alert("Current kubectl context:", currentContext)
ui.NL()

if ui.IsVerbose() && cfg.ContextType == config.ContextTypeCloud {
ui.Info("Your Testkube is in 'cloud' mode with following context")
ui.InfoGrid(map[string]string{
"Agent Key": text.Obfuscate(cfg.CloudContext.AgentKey),
"Agent URI": cfg.CloudContext.AgentUri,
})
ui.NL()
}

ok := ui.Confirm("Do you want to continue?")
if !ok {
ui.Errf("Testkube installation cancelled")
return
}
}

err = common.HelmUpgradeOrInstalTestkube(options)
err := common.HelmUpgradeOrInstalTestkube(options)
ui.ExitOnError("Installing testkube", err)

if cfg.ContextType == config.ContextTypeCloud {
err = common.PopulateAgentDataToContext(options, cfg)
ui.ExitOnError("Storing agent data in context", err)
} else {
ui.Info(`To help improve the quality of Testkube, we collect anonymous basic telemetry data. Head out to https://docs.testkube.io/articles/telemetry to read our policy or feel free to:`)
ui.Info(`To help improve the quality of Testkube, we collect anonymous basic telemetry data. Head out to https://docs.testkube.io/articles/telemetry to read our policy or feel free to:`)

ui.NL()
ui.ShellCommand("disable telemetry by typing", "testkube disable telemetry")
ui.NL()
}
ui.NL()
ui.ShellCommand("disable telemetry by typing", "testkube disable telemetry")
ui.NL()

ui.Info(" Happy Testing! 🚀")
ui.NL()
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubectl-testkube/commands/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func NewUpgradeCmd() *cobra.Command {

if cfg.ContextType == config.ContextTypeCloud {
ui.Info("Testkube Cloud agent upgrade started")
err = common.HelmUpgradeOrInstallTestkubeCloud(options, cfg)
err = common.HelmUpgradeOrInstallTestkubeCloud(options, cfg, false)
ui.ExitOnError("Upgrading Testkube Cloud Agent", err)
err = common.PopulateAgentDataToContext(options, cfg)
ui.ExitOnError("Storing agent data in context", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloudlogin/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func CloudLogin(ctx context.Context, providerURL string) (string, chan string, e
code := r.URL.Query().Get("code")
if code != "" {
ch <- code
fmt.Fprintln(w, "Authorization successful.")
fmt.Fprintln(w, "Your testkube CLI is now succesfully authenticated. Go back to the terminal to continue.")
} else {
fmt.Fprintln(w, "Authorization failed.")
}
Expand Down