github.com/kubeshop/testkube@v1.17.23/cmd/kubectl-testkube/commands/pro/init.go (about)

     1  package pro
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/spf13/cobra"
     7  
     8  	"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common"
     9  	"github.com/kubeshop/testkube/cmd/kubectl-testkube/config"
    10  	"github.com/kubeshop/testkube/pkg/telemetry"
    11  	"github.com/kubeshop/testkube/pkg/ui"
    12  )
    13  
    14  func NewInitCmd() *cobra.Command {
    15  	options := common.HelmOptions{
    16  		NoMinio: true,
    17  		NoMongo: true,
    18  	}
    19  
    20  	cmd := &cobra.Command{
    21  		Use:     "init",
    22  		Short:   "Install Testkube Pro Agent and connect to Testkube Pro environment",
    23  		Aliases: []string{"install"},
    24  		Run: func(cmd *cobra.Command, args []string) {
    25  			ui.Info("WELCOME TO")
    26  			ui.Logo()
    27  
    28  			cfg, err := config.Load()
    29  			ui.ExitOnError("loading config file", err)
    30  			ui.NL()
    31  
    32  			common.ProcessMasterFlags(cmd, &options, &cfg)
    33  
    34  			sendAttemptTelemetry(cmd, cfg)
    35  
    36  			if !options.NoConfirm {
    37  				ui.Warn("This will install Testkube to the latest version. This may take a few minutes.")
    38  				ui.Warn("Please be sure you're on valid kubectl context before continuing!")
    39  				ui.NL()
    40  
    41  				currentContext, err := common.GetCurrentKubernetesContext()
    42  
    43  				if err != nil {
    44  					sendErrTelemetry(cmd, cfg, "k8s_context", err)
    45  					ui.ExitOnError("getting current context", err)
    46  				}
    47  				ui.Alert("Current kubectl context:", currentContext)
    48  				ui.NL()
    49  
    50  				ok := ui.Confirm("Do you want to continue?")
    51  				if !ok {
    52  					ui.Errf("Testkube installation cancelled")
    53  					sendErrTelemetry(cmd, cfg, "user_cancel", err)
    54  					return
    55  				}
    56  			}
    57  
    58  			spinner := ui.NewSpinner("Installing Testkube")
    59  			err = common.HelmUpgradeOrInstallTestkubeCloud(options, cfg, false)
    60  			if err != nil {
    61  				sendErrTelemetry(cmd, cfg, "helm_install", err)
    62  				ui.ExitOnError("Installing Testkube", err)
    63  			}
    64  			spinner.Success()
    65  
    66  			ui.NL()
    67  
    68  			ui.H2("Saving Testkube CLI Pro context")
    69  			var token, refreshToken string
    70  			if !common.IsUserLoggedIn(cfg, options) {
    71  				token, refreshToken, err = common.LoginUser(options.Master.URIs.Auth)
    72  				sendErrTelemetry(cmd, cfg, "login", err)
    73  				ui.ExitOnError("user login", err)
    74  			}
    75  			err = common.PopulateLoginDataToContext(options.Master.OrgId, options.Master.EnvId, token, refreshToken, options, cfg)
    76  			if err != nil {
    77  				sendErrTelemetry(cmd, cfg, "setting_context", err)
    78  				ui.ExitOnError("Setting Pro environment context", err)
    79  			}
    80  			ui.Info(" Happy Testing! 🚀")
    81  			ui.NL()
    82  		},
    83  	}
    84  
    85  	common.PopulateHelmFlags(cmd, &options)
    86  	common.PopulateMasterFlags(cmd, &options)
    87  
    88  	cmd.Flags().BoolVar(&options.MultiNamespace, "multi-namespace", false, "multi namespace mode")
    89  	cmd.Flags().BoolVar(&options.NoOperator, "no-operator", false, "should operator be installed (for more instances in multi namespace mode it should be set to true)")
    90  
    91  	return cmd
    92  }
    93  
    94  func sendErrTelemetry(cmd *cobra.Command, clientCfg config.Data, errType string, errorLogs error) {
    95  	var errorStackTrace string
    96  	errorStackTrace = fmt.Sprintf("%+v", errorLogs)
    97  	if clientCfg.TelemetryEnabled {
    98  		ui.Debug("collecting anonymous telemetry data, you can disable it by calling `kubectl testkube disable telemetry`")
    99  		out, err := telemetry.SendCmdErrorEvent(cmd, common.Version, errType, errorStackTrace)
   100  		if ui.Verbose && err != nil {
   101  			ui.Err(err)
   102  		}
   103  
   104  		ui.Debug("telemetry send event response", out)
   105  	}
   106  }
   107  
   108  func sendAttemptTelemetry(cmd *cobra.Command, clientCfg config.Data) {
   109  	if clientCfg.TelemetryEnabled {
   110  		ui.Debug("collecting anonymous telemetry data, you can disable it by calling `kubectl testkube disable telemetry`")
   111  		out, err := telemetry.SendCmdAttemptEvent(cmd, common.Version)
   112  		if ui.Verbose && err != nil {
   113  			ui.Err(err)
   114  		}
   115  		ui.Debug("telemetry send event response", out)
   116  	}
   117  }