Skip to content

Commit

Permalink
Bypass email prompt for upgrades if installed
Browse files Browse the repository at this point in the history
Moves prepare call to Run, similar to Destroy
Removes interactive argumment in place of checking itself.

(cherry picked from commit a0f2754)
  • Loading branch information
zerbitx authored and cbron committed Dec 17, 2024
1 parent 812af10 commit 878bfd1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
10 changes: 7 additions & 3 deletions cmd/vclusterctl/cmd/platform/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand All @@ -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())
},
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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),
Expand Down
27 changes: 22 additions & 5 deletions cmd/vclusterctl/cmd/platform/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 4 additions & 7 deletions pkg/cli/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down

0 comments on commit 878bfd1

Please sign in to comment.