From b4b2ab3a6da1460738ac6254edf430f170a498c9 Mon Sep 17 00:00:00 2001 From: Cesar Filho <53096417+6za@users.noreply.github.com> Date: Tue, 26 Jul 2022 11:07:53 -0300 Subject: [PATCH] Destroy improvements (#153) * Fix telemetry panic, destroy ui Signed-off-by: 6za <53096417+6za@users.noreply.github.com> * fmt Signed-off-by: 6za <53096417+6za@users.noreply.github.com> * fix outputs Signed-off-by: 6za <53096417+6za@users.noreply.github.com> * changing telemetry host and version Co-authored-by: John Dietz --- cmd/create.go | 10 ++++------ cmd/destroy.go | 35 +++++++++++++++++++++++++++++++++ cmd/root.go | 11 ++++++----- configs/config.go | 2 +- internal/telemetry/telemetry.go | 10 +++++++++- pkg/progress_bar.go | 35 +++++++++++---------------------- 6 files changed, 66 insertions(+), 37 deletions(-) diff --git a/cmd/create.go b/cmd/create.go index 2354b75f1..602976c4b 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -38,6 +38,8 @@ Cobra is a CLI library for Go that empowers applications. This application is a tool to generate the needed files to quickly create a Cobra application.`, Run: func(cmd *cobra.Command, args []string) { + progressPrinter.GetInstance() + progressPrinter.SetupProgress(4) var kPortForwardArgocd *exec.Cmd progressPrinter.AddTracker("step-0", "Process Parameters", 1) @@ -77,7 +79,6 @@ to quickly create a Cobra application.`, restoreSSLCmd.Run(cmd, args) - kubeconfig, err := clientcmd.BuildConfigFromFlags("", config.KubeConfigPath) if err != nil { panic(err.Error()) @@ -87,7 +88,6 @@ to quickly create a Cobra application.`, panic(err.Error()) } - //! soft-serve was just applied softserve.CreateSoftServe(dryRun, config.KubeConfigPath) @@ -298,7 +298,7 @@ to quickly create a Cobra application.`, patchSecret(argocdSecretClient, "argocd-secret", "oidc.gitlab.clientSecret", viper.GetString("gitlab.oidc.argocd.secret")) argocdPodClient := clientset.CoreV1().Pods("argocd") - k8s.DeletePodByLabel(argocdPodClient, "app.kubernetes.io/name=argocd-server") + k8s.DeletePodByLabel(argocdPodClient, "app.kubernetes.io/name=argocd-server") viper.Set("argocd.oidc-patched", true) viper.WriteConfig() } @@ -391,7 +391,7 @@ to quickly create a Cobra application.`, informUser("ArgoCD DNS is ready") break } else { - k8s.DeletePodByLabel(argocdPodClient, "app.kubernetes.io/name=argocd-server") + k8s.DeletePodByLabel(argocdPodClient, "app.kubernetes.io/name=argocd-server") } } @@ -448,6 +448,4 @@ func init() { createCmd.Flags().Bool("skip-vault", false, "Skip post-gitClient lab install and vault setup") createCmd.Flags().Bool("use-telemetry", true, "installer will not send telemetry about this installation") - progressPrinter.GetInstance() - progressPrinter.SetupProgress(4) } diff --git a/cmd/destroy.go b/cmd/destroy.go index 01affd400..9e07fca88 100644 --- a/cmd/destroy.go +++ b/cmd/destroy.go @@ -6,10 +6,12 @@ import ( "log" "os/exec" "syscall" + "time" "github.com/kubefirst/kubefirst/configs" "github.com/kubefirst/kubefirst/internal/gitlab" "github.com/kubefirst/kubefirst/internal/k8s" + "github.com/kubefirst/kubefirst/internal/progressPrinter" "github.com/kubefirst/kubefirst/internal/terraform" "github.com/spf13/cobra" ) @@ -24,6 +26,8 @@ and all of the components in kubernetes. Optional: skip gitlab terraform if the registry has already been deleted.`, Run: func(cmd *cobra.Command, args []string) { + progressPrinter.GetInstance() + progressPrinter.SetupProgress(2) config := configs.ReadConfig() @@ -39,6 +43,17 @@ if the registry has already been deleted.`, if err != nil { log.Panic(err) } + dryRun, err := cmd.Flags().GetBool("dry-run") + if err != nil { + log.Panic(err) + } + + if dryRun { + skipGitlabTerraform = true + skipDeleteRegistryApplication = true + skipBaseTerraform = true + } + progressPrinter.AddTracker("step-prepare", "Open Ports", 3) var kPortForwardOutb, kPortForwardErrb bytes.Buffer kPortForward := exec.Command(config.KubectlClientPath, "--kubeconfig", config.KubeConfigPath, "-n", "gitlab", "port-forward", "svc/gitlab-webservice-default", "8888:8080") @@ -54,6 +69,8 @@ if the registry has already been deleted.`, log.Printf("Commad Execution STDERR: %s", kPortForwardErrb.String()) } + informUser("Open gitlab port-forward") + progressPrinter.IncrementTracker("step-prepare", 1) if !skipDeleteRegistryApplication { var kPortForwardArgocdOutb, kPortForwardArgocdErrb bytes.Buffer @@ -70,6 +87,8 @@ if the registry has already been deleted.`, log.Printf("Commad Execution STDERR: %s", kPortForwardArgocdErrb.String()) } } + informUser("Open argocd port-forward") + progressPrinter.IncrementTracker("step-prepare", 1) var kPortForwardVaultOutb, kPortForwardVaultErrb bytes.Buffer kPortForwardVault := exec.Command(config.KubectlClientPath, "--kubeconfig", config.KubeConfigPath, "-n", "vault", "port-forward", "svc/vault", "8200:8200") @@ -84,19 +103,34 @@ if the registry has already been deleted.`, log.Printf("Commad Execution STDOUT: %s", kPortForwardVaultOutb.String()) log.Printf("Commad Execution STDERR: %s", kPortForwardVaultErrb.String()) } + informUser("Open vault port-forward") + progressPrinter.IncrementTracker("step-prepare", 1) + log.Println("destroying gitlab terraform") + progressPrinter.AddTracker("step-destroy", "Destroy Cloud", 4) + progressPrinter.IncrementTracker("step-destroy", 1) + informUser("Destroying Gitlab") gitlab.DestroyGitlabTerraform(skipGitlabTerraform) + progressPrinter.IncrementTracker("step-destroy", 1) + log.Println("gitlab terraform destruction complete") log.Println("deleting registry application in argocd") // delete argocd registry + informUser("Destroying Registry Application") k8s.DeleteRegistryApplication(skipDeleteRegistryApplication) + progressPrinter.IncrementTracker("step-destroy", 1) log.Println("registry application deleted") log.Println("terraform destroy base") + informUser("Destroying Cluster") terraform.DestroyBaseTerraform(skipBaseTerraform) + progressPrinter.IncrementTracker("step-destroy", 1) + informUser("All Destroyed") + log.Println("terraform base destruction complete") fmt.Println("End of execution destroy") + time.Sleep(time.Millisecond * 100) }, } @@ -108,4 +142,5 @@ func init() { destroyCmd.Flags().Bool("skip-delete-register", false, "whether to skip deletion of register application ") destroyCmd.Flags().Bool("skip-base-terraform", false, "whether to skip the terraform destroy against base install - note: if you already deleted registry it doesnt exist") destroyCmd.Flags().Bool("destroy-buckets", false, "remove created aws buckets, not empty buckets are not cleaned") + destroyCmd.Flags().Bool("dry-run", false, "set to dry-run mode, no changes done on cloud provider selected") } diff --git a/cmd/root.go b/cmd/root.go index b0b4caa85..05fb96ef5 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,9 +1,8 @@ package cmd import ( + "fmt" "github.com/spf13/cobra" - "github.com/spf13/viper" - "log" "os" ) @@ -15,7 +14,9 @@ var rootCmd = &cobra.Command{ open source application delivery platform in under an hour. checkout the docs at docs.kubefirst.com.`, Run: func(cmd *cobra.Command, args []string) { - log.Println(viper.Get("name")) + //log.Println(viper.Get("name")) + fmt.Println("To learn more about kubefirst, run:") + fmt.Println(" kubefirst help") }, } @@ -32,6 +33,6 @@ func init() { cobra.OnInitialize() // Cobra also supports local flags, which will only run, when this action is called directly. - rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") - + rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + } diff --git a/configs/config.go b/configs/config.go index a32cb6f68..037ac1d84 100644 --- a/configs/config.go +++ b/configs/config.go @@ -73,7 +73,7 @@ func ReadConfig() *Config { // todo adopt latest helmVersion := "v3.9.0" config.HelmVersion = "v3.2.1" - config.KubefirstVersion = "1.8.0" + config.KubefirstVersion = "1.8.2" config.InstallerEmail = "kubefirst-bot@kubefirst.com" diff --git a/internal/telemetry/telemetry.go b/internal/telemetry/telemetry.go index 12efdb4c0..a6fa6f4f2 100644 --- a/internal/telemetry/telemetry.go +++ b/internal/telemetry/telemetry.go @@ -10,6 +10,11 @@ import ( // SendTelemetry post telemetry data func SendTelemetry(useTelemetry bool, domain string, metricName string) { + defer func() { + if r := recover(); r != nil { + log.Println("Error sending telemetry. Error:\n", r) + } + }() if !useTelemetry { log.Println("Telemetry disable by user choice, nothing was sent") @@ -18,7 +23,7 @@ func SendTelemetry(useTelemetry bool, domain string, metricName string) { log.Println("SendTelemetry (working...)") - url := "https://metaphor-go-production.kubefirst.io/telemetry" + url := "https://metaphor-go-production.mgmt.kubefirst.com/telemetry" method := "POST" payload := strings.NewReader(fmt.Sprintf(`{"domain": "%s","name": "%s"}`, domain, metricName)) @@ -40,6 +45,9 @@ func SendTelemetry(useTelemetry bool, domain string, metricName string) { } defer res.Body.Close() body, err := ioutil.ReadAll(res.Body) + if err != nil { + log.Println("error") + } log.Println(string(body)) diff --git a/pkg/progress_bar.go b/pkg/progress_bar.go index 532ab3418..aab55872c 100644 --- a/pkg/progress_bar.go +++ b/pkg/progress_bar.go @@ -1,7 +1,6 @@ package pkg import ( - "flag" "fmt" "github.com/jedib0t/go-pretty/v6/progress" "time" @@ -22,18 +21,8 @@ const CreateBuckets = "Create Buckets" const SendTelemetry = "Send Telemetry" var ( - pw progress.Writer - Trackers map[string]*ActionTracker - flagAutoStop = flag.Bool("auto-stop", false, "Auto-stop rendering?") - flagHideETA = flag.Bool("hide-eta", false, "Hide the ETA?") - flagHideETAOverall = flag.Bool("hide-eta-overall", false, "Hide the ETA in the overall tracker?") - flagHideOverallTracker = flag.Bool("hide-overall", false, "Hide the Overall Tracker?") - flagHidePercentage = flag.Bool("hide-percentage", false, "Hide the progress percent?") - flagHideTime = flag.Bool("hide-time", false, "Hide the time taken?") - flagHideValue = flag.Bool("hide-value", false, "Hide the tracker value?") - // flagNumTrackers = flag.Int("num-trackers", 12, "Number of Trackers") - flagRandomFail = flag.Bool("rnd-fail", false, "Introduce random failures in tracking") - flagRandomLogs = flag.Bool("rnd-logs", false, "Output random logs in the middle of tracking") + pw progress.Writer + Trackers map[string]*ActionTracker ) // GetTrackers keeps one single instance of Trackers alive using singleton pattern. @@ -62,26 +51,24 @@ func CreateTracker(title string, total int64) *progress.Tracker { // SetupProgress prepare the progress bar setting its initial configuration func SetupProgress(numTrackers int) { - flagNumTrackers := flag.Int("num-trackers", numTrackers, "Number of Trackers") - flag.Parse() - fmt.Printf("Init actions: %d expected tasks ...\n\n", *flagNumTrackers) + fmt.Printf("Init actions: %d expected tasks ...\n\n", numTrackers) // instantiate a Progress Writer and set up the options pw = progress.NewWriter() - pw.SetAutoStop(*flagAutoStop) + pw.SetAutoStop(false) pw.SetTrackerLength(40) pw.SetMessageWidth(39) - pw.SetNumTrackersExpected(*flagNumTrackers) + pw.SetNumTrackersExpected(numTrackers) pw.SetSortBy(progress.SortByPercentDsc) pw.SetStyle(progress.StyleDefault) pw.SetTrackerPosition(progress.PositionRight) pw.SetUpdateFrequency(time.Millisecond * 100) pw.Style().Colors = progress.StyleColorsExample pw.Style().Options.PercentFormat = "%4.1f%%" - pw.Style().Visibility.ETA = !*flagHideETA - pw.Style().Visibility.ETAOverall = !*flagHideETAOverall - pw.Style().Visibility.Percentage = !*flagHidePercentage - pw.Style().Visibility.Time = !*flagHideTime - pw.Style().Visibility.TrackerOverall = !*flagHideOverallTracker - pw.Style().Visibility.Value = !*flagHideValue + pw.Style().Visibility.ETA = true + pw.Style().Visibility.ETAOverall = true + pw.Style().Visibility.Percentage = true + pw.Style().Visibility.Time = true + pw.Style().Visibility.TrackerOverall = true + pw.Style().Visibility.Value = true go pw.Render() }