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

     1  package pro
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/pterm/pterm"
     8  
     9  	"github.com/spf13/cobra"
    10  
    11  	"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common"
    12  	"github.com/kubeshop/testkube/cmd/kubectl-testkube/config"
    13  	"github.com/kubeshop/testkube/pkg/ui"
    14  )
    15  
    16  func NewDisconnectCmd() *cobra.Command {
    17  
    18  	var opts common.HelmOptions
    19  
    20  	cmd := &cobra.Command{
    21  		Use:     "disconnect",
    22  		Aliases: []string{"d"},
    23  		Short:   "Switch back to Testkube OSS mode, based on active .kube/config file",
    24  		Run: func(cmd *cobra.Command, args []string) {
    25  
    26  			ui.H1("Disconnecting your Pro environment:")
    27  			ui.Paragraph("Rolling back to your clusters Testkube OSS installation")
    28  			ui.Paragraph("If you need more details click into following link: " + docsUrl)
    29  			ui.H2("You can safely switch between connecting Pro and disconnecting without losing your data.")
    30  
    31  			cfg, err := config.Load()
    32  			if err != nil {
    33  				pterm.Error.Printfln("Failed to load config file: %s", err.Error())
    34  				return
    35  			}
    36  
    37  			client, _, err := common.GetClient(cmd)
    38  			ui.ExitOnError("getting client", err)
    39  
    40  			info, err := client.GetServerInfo()
    41  			firstInstall := err != nil && strings.Contains(err.Error(), "not found")
    42  			if err != nil && !firstInstall {
    43  				ui.Failf("Can't get Testkube cluster information: %s", err.Error())
    44  			}
    45  			var apiContext string
    46  			if actx, ok := contextDescription[info.Context]; ok {
    47  				apiContext = actx
    48  			}
    49  			var clusterContext string
    50  			if cfg.ContextType == config.ContextTypeKubeconfig {
    51  				clusterContext, err = common.GetCurrentKubernetesContext()
    52  				if err != nil {
    53  					pterm.Error.Printfln("Failed to get current Kubernetes context: %s", err.Error())
    54  					return
    55  				}
    56  			}
    57  
    58  			// TODO: implement context info
    59  			ui.H1("Current status of your Testkube instance")
    60  
    61  			summary := [][]string{
    62  				{"Testkube mode"},
    63  				{"Context", apiContext},
    64  				{"Kubectl context", clusterContext},
    65  				{"Namespace", cfg.Namespace},
    66  				{ui.Separator, ""},
    67  
    68  				{"Testkube is connected to Pro organizations environment"},
    69  				{"Organization Id", cfg.CloudContext.OrganizationId},
    70  				{"Environment Id", cfg.CloudContext.EnvironmentId},
    71  			}
    72  
    73  			ui.Properties(summary)
    74  
    75  			if ok := ui.Confirm("Shall we disconnect your Pro environment now?"); !ok {
    76  				return
    77  			}
    78  
    79  			ui.NL(2)
    80  
    81  			spinner := ui.NewSpinner("Disconnecting from Testkube Pro")
    82  
    83  			err = common.HelmUpgradeOrInstalTestkube(opts)
    84  			ui.ExitOnError("Installing Testkube Pro", err)
    85  			spinner.Success()
    86  
    87  			// let's scale down deployment of mongo
    88  			if opts.MongoReplicas > 0 {
    89  				spinner = ui.NewSpinner("Scaling up MongoDB")
    90  				common.KubectlScaleDeployment(opts.Namespace, "testkube-mongodb", opts.MongoReplicas)
    91  				spinner.Success()
    92  			}
    93  			if opts.MinioReplicas > 0 {
    94  				spinner = ui.NewSpinner("Scaling up MinIO")
    95  				common.KubectlScaleDeployment(opts.Namespace, "testkube-minio-testkube", opts.MinioReplicas)
    96  				spinner.Success()
    97  			}
    98  
    99  			spinner = ui.NewSpinner("Resetting Testkube config.json")
   100  			cfg.ContextType = config.ContextTypeKubeconfig
   101  			cfg.CloudContext = config.CloudContext{}
   102  			if err = config.Save(cfg); err != nil {
   103  				spinner.Fail(fmt.Sprintf("Error updating local Testkube config file: %s", err))
   104  				ui.Warn("Please manually remove the fields contextType and cloudContext from your config file.")
   105  			} else {
   106  				spinner.Success()
   107  			}
   108  
   109  			ui.NL()
   110  			ui.Success("Disconnect finished successfully")
   111  			ui.NL()
   112  		},
   113  	}
   114  
   115  	// populate options
   116  	common.PopulateHelmFlags(cmd, &opts)
   117  	cmd.Flags().IntVar(&opts.MinioReplicas, "minio-replicas", 1, "MinIO replicas")
   118  	cmd.Flags().IntVar(&opts.MongoReplicas, "mongo-replicas", 1, "MongoDB replicas")
   119  	return cmd
   120  }