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

[v0.22] Bypass email prompt for upgrades if installed #2339

Merged
merged 1 commit into from
Dec 17, 2024
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
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
Loading