Skip to content

Commit

Permalink
Destroy improvements (#153)
Browse files Browse the repository at this point in the history
* Fix telemetry panic, destroy ui

Signed-off-by: 6za <[email protected]>

* fmt

Signed-off-by: 6za <[email protected]>

* fix outputs

Signed-off-by: 6za <[email protected]>

* changing telemetry host and version

Co-authored-by: John Dietz <[email protected]>
  • Loading branch information
6za and johndietz authored Jul 26, 2022
1 parent ff1ee41 commit b4b2ab3
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 37 deletions.
10 changes: 4 additions & 6 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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())
Expand All @@ -87,7 +88,6 @@ to quickly create a Cobra application.`,
panic(err.Error())
}


//! soft-serve was just applied

softserve.CreateSoftServe(dryRun, config.KubeConfigPath)
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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")
}
}

Expand Down Expand Up @@ -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)
}
35 changes: 35 additions & 0 deletions cmd/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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()

Expand All @@ -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")
Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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)
},
}

Expand All @@ -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")
}
11 changes: 6 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package cmd

import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"log"
"os"
)

Expand All @@ -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")
},
}

Expand All @@ -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")

}
2 changes: 1 addition & 1 deletion configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]"

Expand Down
10 changes: 9 additions & 1 deletion internal/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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))
Expand All @@ -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))

Expand Down
35 changes: 11 additions & 24 deletions pkg/progress_bar.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pkg

import (
"flag"
"fmt"
"github.com/jedib0t/go-pretty/v6/progress"
"time"
Expand All @@ -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.
Expand Down Expand Up @@ -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()
}

0 comments on commit b4b2ab3

Please sign in to comment.