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

     1  package commands
     2  
     3  import (
     4  	"github.com/spf13/cobra"
     5  
     6  	"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common"
     7  	"github.com/kubeshop/testkube/cmd/kubectl-testkube/config"
     8  	"github.com/kubeshop/testkube/pkg/ui"
     9  	"github.com/kubeshop/testkube/pkg/utils/text"
    10  )
    11  
    12  func NewUpgradeCmd() *cobra.Command {
    13  	var options common.HelmOptions
    14  
    15  	cmd := &cobra.Command{
    16  		Use:     "upgrade",
    17  		Short:   "Upgrade Helm chart, install dependencies and run migrations",
    18  		Aliases: []string{"update"},
    19  		Run: func(cmd *cobra.Command, args []string) {
    20  
    21  			cfg, err := config.Load()
    22  			ui.ExitOnError("loading config file", err)
    23  			ui.NL()
    24  
    25  			common.ProcessMasterFlags(cmd, &options, &cfg)
    26  
    27  			// set to cloud context explicitly when user pass agent key and store the key later
    28  			if options.Master.AgentToken != "" {
    29  				cfg.CloudContext.AgentKey = options.Master.AgentToken
    30  				cfg.ContextType = config.ContextTypeCloud
    31  			}
    32  
    33  			if !options.NoConfirm {
    34  				ui.Warn("This will upgrade Testkube to the latest version. This may take a few minutes.")
    35  				ui.Warn("Please be sure you're on valid kubectl context before continuing!")
    36  				ui.NL()
    37  
    38  				currentContext, err := common.GetCurrentKubernetesContext()
    39  				ui.ExitOnError("getting current context", err)
    40  				ui.Alert("Current kubectl context:", currentContext)
    41  				ui.NL()
    42  
    43  				if ui.IsVerbose() && cfg.ContextType == config.ContextTypeCloud {
    44  					ui.Info("Your Testkube is in 'cloud' mode with following context")
    45  					ui.InfoGrid(map[string]string{
    46  						"Agent Key": text.Obfuscate(cfg.CloudContext.AgentKey),
    47  						"Agent URI": cfg.CloudContext.AgentUri,
    48  					})
    49  					ui.NL()
    50  				}
    51  
    52  				ok := ui.Confirm("Do you want to continue?")
    53  				if !ok {
    54  					ui.Errf("Upgrade cancelled")
    55  					return
    56  				}
    57  			}
    58  
    59  			if cfg.ContextType == config.ContextTypeCloud {
    60  				ui.Info("Testkube Pro agent upgrade started")
    61  				err = common.HelmUpgradeOrInstallTestkubeCloud(options, cfg, false)
    62  				ui.ExitOnError("Upgrading Testkube Pro Agent", err)
    63  				err = common.PopulateAgentDataToContext(options, cfg)
    64  				ui.ExitOnError("Storing agent data in context", err)
    65  			} else {
    66  				ui.Info("Updating Testkube")
    67  				hasMigrations, err := common.RunMigrations(cmd)
    68  				ui.ExitOnError("Running migrations", err)
    69  				if hasMigrations {
    70  					ui.Success("All migrations executed successfully")
    71  				}
    72  
    73  				err = common.HelmUpgradeOrInstalTestkube(options)
    74  				ui.ExitOnError("Upgrading Testkube", err)
    75  			}
    76  
    77  		},
    78  	}
    79  
    80  	common.PopulateHelmFlags(cmd, &options)
    81  	common.PopulateMasterFlags(cmd, &options)
    82  
    83  	return cmd
    84  }