From a03d8e1aebb1401d25eda955b80fe79cb1fe1a7c Mon Sep 17 00:00:00 2001 From: RyanSwanson Date: Fri, 13 Dec 2024 10:18:04 -0700 Subject: [PATCH] Bypass email prompt for upgrades if installed Moves prepare call to Run, similar to Destroy Removes interactive argumment in place of checking itself. (cherry picked from commit a0f27546a638d0a23bcbad8edea48ab1093c8701) --- cmd/vclusterctl/cmd/platform/destroy.go | 10 ++++++--- cmd/vclusterctl/cmd/platform/start.go | 27 ++++++++++++++++++++----- pkg/cli/start/start.go | 11 ++++------ 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/cmd/vclusterctl/cmd/platform/destroy.go b/cmd/vclusterctl/cmd/platform/destroy.go index e74f29ca0..5b92dad3c 100644 --- a/cmd/vclusterctl/cmd/platform/destroy.go +++ b/cmd/vclusterctl/cmd/platform/destroy.go @@ -8,6 +8,7 @@ import ( "github.com/loft-sh/log" "github.com/loft-sh/log/survey" + "github.com/loft-sh/log/terminal" "github.com/loft-sh/vcluster/pkg/cli/destroy" "github.com/loft-sh/vcluster/pkg/cli/flags" "github.com/loft-sh/vcluster/pkg/cli/start" @@ -40,7 +41,7 @@ func NewDestroyCmd(globalFlags *flags.GlobalFlags) *cobra.Command { Destroys a vCluster Platform instance in your Kubernetes cluster. IMPORTANT: This action is done against the cluster the the kube-context is pointing to, and not the vCluster Platform instance that is logged in. -It does not require logging in to vCluster Platform. +It does not require logging in to vCluster Platform. Please make sure you meet the following requirements before running this command: @@ -55,6 +56,9 @@ VirtualClusterInstances managed with driver helm will be deleted, but the underl `, Args: cobra.NoArgs, RunE: func(cobraCmd *cobra.Command, _ []string) error { + if cmd.NonInteractive { + terminal.IsTerminalIn = false + } return cmd.Run(cobraCmd.Context()) }, } @@ -72,7 +76,7 @@ VirtualClusterInstances managed with driver helm will be deleted, but the underl func (cmd *DestroyCmd) Run(ctx context.Context) error { // initialise clients, verify binaries exist, sanity-check context - err := cmd.Options.Prepare(cmd.NonInteractive) + err := cmd.Options.Prepare() if err != nil { return fmt.Errorf("failed to prepare clients: %w", err) } @@ -103,7 +107,7 @@ func (cmd *DestroyCmd) Run(ctx context.Context) error { return fmt.Errorf("platform not installed in namespace %q", cmd.Namespace) } - if !cmd.NonInteractive { + if terminal.IsTerminalIn { deleteOpt := "delete" out, err := cmd.Log.Question(&survey.QuestionOptions{ Question: fmt.Sprintf("IMPORTANT! You are destroy the vCluster Platform in the namespace %q.\nThis may result in data loss. Please ensure your kube-context is pointed at the right cluster.\n Please type %q to continue:", cmd.Namespace, deleteOpt), diff --git a/cmd/vclusterctl/cmd/platform/start.go b/cmd/vclusterctl/cmd/platform/start.go index ee4b7d4c6..8da5d2830 100644 --- a/cmd/vclusterctl/cmd/platform/start.go +++ b/cmd/vclusterctl/cmd/platform/start.go @@ -17,6 +17,7 @@ import ( "github.com/loft-sh/vcluster/pkg/platform" "github.com/loft-sh/vcluster/pkg/platform/clihelper" "github.com/spf13/cobra" + "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" ) @@ -84,10 +85,6 @@ before running this command: } func (cmd *StartCmd) Run(ctx context.Context) error { - if err := cmd.ensureEmailWithDisclaimer(); err != nil { - return err - } - // get version to deploy if cmd.Version == "latest" || cmd.Version == "" { cmd.Version = platform.MinimumVersionTag @@ -150,10 +147,30 @@ func (cmd *StartCmd) Run(ctx context.Context) error { } } + if err := cmd.StartOptions.Prepare(); err != nil { + return err + } + + if err := cmd.ensureEmailWithDisclaimer(ctx, cmd.KubeClient, cmd.Namespace); err != nil { + return err + } + return start.NewLoftStarter(cmd.StartOptions).Start(ctx) } -func (cmd *StartCmd) ensureEmailWithDisclaimer() error { +func (cmd *StartCmd) ensureEmailWithDisclaimer(ctx context.Context, kc kubernetes.Interface, namespace string) error { + if cmd.Upgrade { + isInstalled, err := clihelper.IsLoftAlreadyInstalled(ctx, kc, namespace) + + if err != nil { + return err + } + + if isInstalled { + return nil + } + } + fmt.Printf(`By providing your email, you accept our Terms of Service and Privacy Statement: Terms of Service: https://www.loft.sh/legal/terms Privacy Statement: https://www.loft.sh/legal/privacy diff --git a/pkg/cli/start/start.go b/pkg/cli/start/start.go index 184379a52..3d32f0f7f 100644 --- a/pkg/cli/start/start.go +++ b/pkg/cli/start/start.go @@ -10,6 +10,7 @@ import ( "github.com/loft-sh/api/v4/pkg/product" "github.com/loft-sh/log" "github.com/loft-sh/log/survey" + "github.com/loft-sh/log/terminal" "github.com/loft-sh/vcluster/pkg/cli/flags" "github.com/loft-sh/vcluster/pkg/platform" "github.com/loft-sh/vcluster/pkg/platform/clihelper" @@ -93,15 +94,11 @@ func (l *LoftStarter) Start(ctx context.Context) error { l.LocalPort = "9898" } - err := l.Prepare(false) - if err != nil { - return err - } l.Log.WriteString(logrus.InfoLevel, "\n") // Uninstall already existing Loft instance if l.Reset { - err = clihelper.UninstallLoft(ctx, l.KubeClient, l.RestConfig, l.Context, l.Namespace, l.Log) + err := clihelper.UninstallLoft(ctx, l.KubeClient, l.RestConfig, l.Context, l.Namespace, l.Log) if err != nil { return err } @@ -141,7 +138,7 @@ func (l *LoftStarter) Start(ctx context.Context) error { } // Prepare initializes clients, verifies the existense of binaries, and ensures we are starting with the right kube context -func (l *Options) Prepare(cliNonInteractive bool) error { +func (l *Options) Prepare() error { platformClient := platform.NewClientFromConfig(l.LoadedConfig(l.Log)) platformConfig := platformClient.Config().Platform @@ -160,7 +157,7 @@ func (l *Options) Prepare(cliNonInteractive bool) error { if l.Context != "" { contextToLoad = l.Context } else if platformConfig.LastInstallContext != "" && platformConfig.LastInstallContext != contextToLoad { - if !cliNonInteractive { + if terminal.IsTerminalIn { contextToLoad, err = l.Log.Question(&survey.QuestionOptions{ Question: product.Replace(fmt.Sprintf("Seems like you try to use 'vcluster %s' with a different kubernetes context than before. Please choose which kubernetes context you want to use", l.CommandName)), DefaultValue: contextToLoad,