From 7824f32ce6bf24607c6579f8fd043eb2b5aebb5f Mon Sep 17 00:00:00 2001 From: 6za <53096417+6za@users.noreply.github.com> Date: Wed, 9 Nov 2022 17:37:24 +0000 Subject: [PATCH 1/3] new destroy by flavor Signed-off-by: 6za <53096417+6za@users.noreply.github.com> --- cmd/destroyAwsGithub.go | 53 +++++++++++++++++++++++++++++++++++++++ cmd/destroyAwsGitlab.go | 53 +++++++++++++++++++++++++++++++++++++++ cmd/destroyLocalGithub.go | 53 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 159 insertions(+) create mode 100644 cmd/destroyAwsGithub.go create mode 100644 cmd/destroyAwsGitlab.go create mode 100644 cmd/destroyLocalGithub.go diff --git a/cmd/destroyAwsGithub.go b/cmd/destroyAwsGithub.go new file mode 100644 index 000000000..9030daf16 --- /dev/null +++ b/cmd/destroyAwsGithub.go @@ -0,0 +1,53 @@ +/* +Copyright © 2022 NAME HERE + +*/ +package cmd + +import ( + "fmt" + "log" + + "github.com/kubefirst/kubefirst/configs" + "github.com/kubefirst/kubefirst/internal/flagset" + "github.com/spf13/cobra" +) + +// destroyAwsGithubCmd represents the destroyAwsGithub command +var destroyAwsGithubCmd = &cobra.Command{ + Use: "destroy-aws-github", + Short: "A brief description of your command", + Long: `TBD`, + RunE: func(cmd *cobra.Command, args []string) error { + fmt.Println("destroy-aws-github called") + config := configs.ReadConfig() + + destroyFlags, err := flagset.ProcessDestroyFlags(cmd) + if err != nil { + log.Println(err) + return err + } + + globalFlags, err := flagset.ProcessGlobalFlags(cmd) + if err != nil { + log.Println(err) + return err + } + + if globalFlags.SilentMode { + informUser( + "Silent mode enabled, most of the UI prints wont be showed. Please check the logs for more details.\n", + globalFlags.SilentMode, + ) + } + log.Println(destroyFlags, config) + return nil + }, +} + +func init() { + clusterCmd.AddCommand(destroyAwsGithubCmd) + currentCommand := destroyAwsGithubCmd + flagset.DefineGlobalFlags(currentCommand) + flagset.DefineDestroyFlags(currentCommand) +} diff --git a/cmd/destroyAwsGitlab.go b/cmd/destroyAwsGitlab.go new file mode 100644 index 000000000..39de452ab --- /dev/null +++ b/cmd/destroyAwsGitlab.go @@ -0,0 +1,53 @@ +/* +Copyright © 2022 NAME HERE + +*/ +package cmd + +import ( + "fmt" + "log" + + "github.com/kubefirst/kubefirst/configs" + "github.com/kubefirst/kubefirst/internal/flagset" + "github.com/spf13/cobra" +) + +// destroyAwsGitlabCmd represents the destroyAwsGitlab command +var destroyAwsGitlabCmd = &cobra.Command{ + Use: "destroy-aws-gitlab", + Short: "A brief description of your command", + Long: `TDB`, + RunE: func(cmd *cobra.Command, args []string) error { + fmt.Println("destroy-aws-gitlab called") + config := configs.ReadConfig() + + destroyFlags, err := flagset.ProcessDestroyFlags(cmd) + if err != nil { + log.Println(err) + return err + } + + globalFlags, err := flagset.ProcessGlobalFlags(cmd) + if err != nil { + log.Println(err) + return err + } + + if globalFlags.SilentMode { + informUser( + "Silent mode enabled, most of the UI prints wont be showed. Please check the logs for more details.\n", + globalFlags.SilentMode, + ) + } + log.Println(destroyFlags, config) + return nil + }, +} + +func init() { + clusterCmd.AddCommand(destroyAwsGitlabCmd) + currentCommand := destroyAwsGitlabCmd + flagset.DefineGlobalFlags(currentCommand) + flagset.DefineDestroyFlags(currentCommand) +} diff --git a/cmd/destroyLocalGithub.go b/cmd/destroyLocalGithub.go new file mode 100644 index 000000000..82ea2e968 --- /dev/null +++ b/cmd/destroyLocalGithub.go @@ -0,0 +1,53 @@ +/* +Copyright © 2022 NAME HERE + +*/ +package cmd + +import ( + "fmt" + "log" + + "github.com/kubefirst/kubefirst/configs" + "github.com/kubefirst/kubefirst/internal/flagset" + "github.com/spf13/cobra" +) + +// destroyLocalGithubCmd represents the destroyLocalGithub command +var destroyLocalGithubCmd = &cobra.Command{ + Use: "destroy-local-github", + Short: "A brief description of your command", + Long: `TDB`, + RunE: func(cmd *cobra.Command, args []string) error { + fmt.Println("destroy-local-github called") + config := configs.ReadConfig() + + destroyFlags, err := flagset.ProcessDestroyFlags(cmd) + if err != nil { + log.Println(err) + return err + } + + globalFlags, err := flagset.ProcessGlobalFlags(cmd) + if err != nil { + log.Println(err) + return err + } + + if globalFlags.SilentMode { + informUser( + "Silent mode enabled, most of the UI prints wont be showed. Please check the logs for more details.\n", + globalFlags.SilentMode, + ) + } + log.Println(destroyFlags, config) + return nil + }, +} + +func init() { + clusterCmd.AddCommand(destroyLocalGithubCmd) + currentCommand := destroyLocalGithubCmd + flagset.DefineGlobalFlags(currentCommand) + flagset.DefineDestroyFlags(currentCommand) +} From 5279404915ea9b56d62ccca3d04d9b2f90c9f343 Mon Sep 17 00:00:00 2001 From: 6za <53096417+6za@users.noreply.github.com> Date: Wed, 9 Nov 2022 18:10:02 +0000 Subject: [PATCH 2/3] Review destroy Signed-off-by: 6za <53096417+6za@users.noreply.github.com> --- cmd/destroy.go | 202 ++++---------------------------------- cmd/destroyAwsGitlab.go | 84 ++++++++++++++++ cmd/destroyLocalGithub.go | 87 ++++++++++++++++ internal/gitClient/git.go | 6 ++ 4 files changed, 196 insertions(+), 183 deletions(-) diff --git a/cmd/destroy.go b/cmd/destroy.go index dbdf31ed7..83530d2c6 100644 --- a/cmd/destroy.go +++ b/cmd/destroy.go @@ -1,24 +1,12 @@ package cmd import ( - "errors" "fmt" - "github.com/kubefirst/kubefirst/internal/services" - "github.com/kubefirst/kubefirst/pkg" "log" - "net/http" - "os" - "syscall" "time" - "github.com/kubefirst/kubefirst/configs" "github.com/kubefirst/kubefirst/internal/flagset" - "github.com/kubefirst/kubefirst/internal/gitlab" - "github.com/kubefirst/kubefirst/internal/handlers" - "github.com/kubefirst/kubefirst/internal/k3d" - "github.com/kubefirst/kubefirst/internal/k8s" - "github.com/kubefirst/kubefirst/internal/progressPrinter" - "github.com/kubefirst/kubefirst/internal/terraform" + "github.com/kubefirst/kubefirst/internal/gitClient" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -30,186 +18,34 @@ var destroyCmd = &cobra.Command{ Long: "destroy all the resources installed via Kubefirst installer", RunE: func(cmd *cobra.Command, args []string) error { - config := configs.ReadConfig() - - destroyFlags, err := flagset.ProcessDestroyFlags(cmd) - if err != nil { - log.Println(err) - return err - } - - globalFlags, err := flagset.ProcessGlobalFlags(cmd) - if err != nil { - log.Println(err) - return err - } - - if globalFlags.SilentMode { - informUser( - "Silent mode enabled, most of the UI prints wont be showed. Please check the logs for more details.\n", - globalFlags.SilentMode, - ) - } - - // todo: wrap business logic into the handler - if viper.GetString("cloud") == pkg.CloudK3d && config.GitHubPersonalAccessToken == "" { - - httpClient := http.DefaultClient - gitHubService := services.NewGitHubService(httpClient) - gitHubHandler := handlers.NewGitHubHandler(gitHubService) - gitHubAccessToken, err := gitHubHandler.AuthenticateUser() + //Destroy is implemented based on the flavor selected. + if viper.GetString("cloud") == flagset.CloudK3d { + err := destroyLocalGithubCmd.RunE(cmd, args) if err != nil { + log.Println("Erroring destroying local+github:", err) return err } - - if len(gitHubAccessToken) == 0 { - return errors.New("unable to retrieve a GitHub token for the user") - } - - err = os.Setenv("KUBEFIRST_GITHUB_AUTH_TOKEN", gitHubAccessToken) - if err != nil { - return errors.New("unable to set KUBEFIRST_GITHUB_AUTH_TOKEN") - } - err = viper.WriteConfig() - if err != nil { - return err - } - - // todo: set common way to load env. values (viper->struct->load-env) - // todo: use viper file to load it, not load env. value - if err := os.Setenv("KUBEFIRST_GITHUB_AUTH_TOKEN", gitHubAccessToken); err != nil { - return err - } - log.Println("\nKUBEFIRST_GITHUB_AUTH_TOKEN set via OAuth") - } - - if viper.GetString("cloud") == "k3d" { - // todo add progress bars to this - - //* step 1.1 - open port-forward to state store and vault - // todo --skip-git-terraform - kPortForwardMinio, err := k8s.PortForward(globalFlags.DryRun, "minio", "svc/minio", "9000:9000") - defer func() { - err = kPortForwardMinio.Process.Signal(syscall.SIGTERM) + } else if viper.GetString("cloud") == flagset.CloudAws { + if viper.GetString("gitprovider") == gitClient.Github { + err := destroyAwsGithubCmd.RunE(cmd, args) if err != nil { - log.Println("Error closing kPortForwardMinio") + log.Println("Error destroying aws+github:", err) + return err } - }() - kPortForwardVault, err := k8s.PortForward(globalFlags.DryRun, "vault", "svc/vault", "8200:8200") - defer func() { - err = kPortForwardVault.Process.Signal(syscall.SIGTERM) + + } else if viper.GetString("gitprovider") == gitClient.Gitlab { + err := destroyAwsGitlabCmd.RunE(cmd, args) if err != nil { - log.Println("Error closing kPortForwardVault") + log.Println("Error destroying aws+gitlab:", err) + return err } - }() - - //* step 1.2 - // usersTfApplied := viper.GetBool("terraform.users.apply.complete") - // if usersTfApplied { - // informUser("terraform destroying users resources", globalFlags.SilentMode) - // tfEntrypoint := config.GitOpsRepoPath + "/terraform/users" - // terraform.InitDestroyAutoApprove(globalFlags.DryRun, tfEntrypoint) - // informUser("successfully destroyed users resources", globalFlags.SilentMode) - // } - - //* step 1.3 - terraform destroy github - githubTfApplied := viper.GetBool("terraform.github.apply.complete") - if githubTfApplied { - informUser("terraform destroying github resources", globalFlags.SilentMode) - tfEntrypoint := config.GitOpsRepoPath + "/terraform/github" - terraform.InitDestroyAutoApprove(globalFlags.DryRun, tfEntrypoint) - informUser("successfully destroyed github resources", globalFlags.SilentMode) + } else { + return fmt.Errorf("not supported git-provider") } - //* step 2 - delete k3d cluster - // this could be useful for us to chase down in eks and destroy everything - // in the cloud / cluster minus eks to iterate from argocd forward - // todo --skip-cluster-destroy - informUser("deleting k3d cluster", globalFlags.SilentMode) - k3d.DeleteK3dCluster() - informUser("k3d cluster deleted", globalFlags.SilentMode) - informUser("be sure to run `kubefirst clean` before your next cloud provision", globalFlags.SilentMode) - - //* step 3 - clean local .k1 dir - // err = cleanCmd.RunE(cmd, args) - // if err != nil { - // log.Println("Error running:", cleanCmd.Name()) - // return err - // } - os.Exit(0) - } - - progressPrinter.SetupProgress(2, globalFlags.SilentMode) - - if globalFlags.DryRun { - destroyFlags.SkipGitlabTerraform = true - destroyFlags.SkipDeleteRegistryApplication = true - destroyFlags.SkipBaseTerraform = true - } - progressPrinter.AddTracker("step-prepare", "Open Ports", 3) - - informUser("Open argocd port-forward", globalFlags.SilentMode) - 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", globalFlags.SilentMode) - if !destroyFlags.SkipGitlabTerraform { - kPortForward, _ := k8s.PortForward(globalFlags.DryRun, "gitlab", "svc/gitlab-webservice-default", "8888:8080") - defer func() { - if kPortForward != nil { - log.Println("Closed GitLab port forward") - _ = kPortForward.Process.Signal(syscall.SIGTERM) - } - }() - informUser("Open gitlab port-forward", globalFlags.SilentMode) - progressPrinter.IncrementTracker("step-prepare", 1) - - gitlab.DestroyGitlabTerraform(destroyFlags.SkipGitlabTerraform) + } else { + return fmt.Errorf("not supported mode") } - progressPrinter.IncrementTracker("step-destroy", 1) - - log.Println("gitlab terraform destruction complete") - - //This should wrapped into a function, maybe to move to: k8s.DeleteRegistryApplication - if !destroyFlags.SkipDeleteRegistryApplication { - kPortForwardArgocd, _ := k8s.PortForward(globalFlags.DryRun, "argocd", "svc/argocd-server", "8080:80") - defer func() { - if kPortForwardArgocd != nil { - log.Println("Closed argocd port forward") - _ = kPortForwardArgocd.Process.Signal(syscall.SIGTERM) - } - }() - informUser("Open argocd port-forward", globalFlags.SilentMode) - progressPrinter.IncrementTracker("step-prepare", 1) - - log.Println("deleting registry application in argocd") - // delete argocd registry - informUser("Destroying Registry Application", globalFlags.SilentMode) - k8s.DeleteRegistryApplication(destroyFlags.SkipDeleteRegistryApplication) - progressPrinter.IncrementTracker("step-destroy", 1) - log.Println("registry application deleted") - } - - log.Println("terraform destroy base") - informUser("Destroying Cluster", globalFlags.SilentMode) - terraform.DestroyBaseTerraform(destroyFlags.SkipBaseTerraform) - progressPrinter.IncrementTracker("step-destroy", 1) - - // destroy hosted zone - if destroyFlags.HostedZoneDelete { - hostedZone := viper.GetString("aws.hostedzonename") - awsHandler := handlers.NewAwsHandler(hostedZone, destroyFlags) - err := awsHandler.HostedZoneDelete() - if err != nil { - // if error, just log it - log.Println(err) - } - } - - informUser("All Destroyed", globalFlags.SilentMode) log.Println("terraform base destruction complete") fmt.Println("End of execution destroy") diff --git a/cmd/destroyAwsGitlab.go b/cmd/destroyAwsGitlab.go index 39de452ab..45bf15c0b 100644 --- a/cmd/destroyAwsGitlab.go +++ b/cmd/destroyAwsGitlab.go @@ -7,10 +7,18 @@ package cmd import ( "fmt" "log" + "syscall" + "time" "github.com/kubefirst/kubefirst/configs" "github.com/kubefirst/kubefirst/internal/flagset" + "github.com/kubefirst/kubefirst/internal/gitlab" + "github.com/kubefirst/kubefirst/internal/handlers" + "github.com/kubefirst/kubefirst/internal/k8s" + "github.com/kubefirst/kubefirst/internal/progressPrinter" + "github.com/kubefirst/kubefirst/internal/terraform" "github.com/spf13/cobra" + "github.com/spf13/viper" ) // destroyAwsGitlabCmd represents the destroyAwsGitlab command @@ -41,6 +49,82 @@ var destroyAwsGitlabCmd = &cobra.Command{ ) } log.Println(destroyFlags, config) + + progressPrinter.SetupProgress(2, globalFlags.SilentMode) + + if globalFlags.DryRun { + destroyFlags.SkipGitlabTerraform = true + destroyFlags.SkipDeleteRegistryApplication = true + destroyFlags.SkipBaseTerraform = true + } + progressPrinter.AddTracker("step-prepare", "Open Ports", 3) + progressPrinter.AddTracker("step-destroy", "Destroy Cloud", 4) + progressPrinter.SetupProgress(progressPrinter.TotalOfTrackers(), globalFlags.SilentMode) + + informUser("Open argocd port-forward", globalFlags.SilentMode) + progressPrinter.IncrementTracker("step-prepare", 1) + + log.Println("destroying gitlab terraform") + + progressPrinter.IncrementTracker("step-destroy", 1) + informUser("Destroying Gitlab", globalFlags.SilentMode) + if !destroyFlags.SkipGitlabTerraform { + kPortForward, _ := k8s.PortForward(globalFlags.DryRun, "gitlab", "svc/gitlab-webservice-default", "8888:8080") + defer func() { + if kPortForward != nil { + log.Println("Closed GitLab port forward") + _ = kPortForward.Process.Signal(syscall.SIGTERM) + } + }() + informUser("Open gitlab port-forward", globalFlags.SilentMode) + gitlab.DestroyGitlabTerraform(destroyFlags.SkipGitlabTerraform) + } + progressPrinter.IncrementTracker("step-prepare", 1) + progressPrinter.IncrementTracker("step-destroy", 1) + + log.Println("gitlab terraform destruction complete") + + //This should wrapped into a function, maybe to move to: k8s.DeleteRegistryApplication + if !destroyFlags.SkipDeleteRegistryApplication { + kPortForwardArgocd, _ := k8s.PortForward(globalFlags.DryRun, "argocd", "svc/argocd-server", "8080:80") + defer func() { + if kPortForwardArgocd != nil { + log.Println("Closed argocd port forward") + _ = kPortForwardArgocd.Process.Signal(syscall.SIGTERM) + } + }() + informUser("Open argocd port-forward", globalFlags.SilentMode) + log.Println("deleting registry application in argocd") + // delete argocd registry + informUser("Destroying Registry Application", globalFlags.SilentMode) + k8s.DeleteRegistryApplication(destroyFlags.SkipDeleteRegistryApplication) + } + + progressPrinter.IncrementTracker("step-prepare", 1) + progressPrinter.IncrementTracker("step-destroy", 1) + log.Println("registry application deleted") + + log.Println("terraform destroy base") + informUser("Destroying Cluster", globalFlags.SilentMode) + terraform.DestroyBaseTerraform(destroyFlags.SkipBaseTerraform) + progressPrinter.IncrementTracker("step-destroy", 1) + + // destroy hosted zone + if destroyFlags.HostedZoneDelete { + hostedZone := viper.GetString("aws.hostedzonename") + awsHandler := handlers.NewAwsHandler(hostedZone, destroyFlags) + err := awsHandler.HostedZoneDelete() + if err != nil { + // if error, just log it + log.Println(err) + } + } + + informUser("All Destroyed", globalFlags.SilentMode) + + log.Println("terraform base destruction complete") + fmt.Println("End of execution destroy") + time.Sleep(time.Millisecond * 100) return nil }, } diff --git a/cmd/destroyLocalGithub.go b/cmd/destroyLocalGithub.go index 82ea2e968..6308136a9 100644 --- a/cmd/destroyLocalGithub.go +++ b/cmd/destroyLocalGithub.go @@ -5,12 +5,23 @@ Copyright © 2022 NAME HERE package cmd import ( + "errors" "fmt" "log" + "net/http" + "os" + "syscall" + "time" "github.com/kubefirst/kubefirst/configs" "github.com/kubefirst/kubefirst/internal/flagset" + "github.com/kubefirst/kubefirst/internal/handlers" + "github.com/kubefirst/kubefirst/internal/k3d" + "github.com/kubefirst/kubefirst/internal/k8s" + "github.com/kubefirst/kubefirst/internal/services" + "github.com/kubefirst/kubefirst/internal/terraform" "github.com/spf13/cobra" + "github.com/spf13/viper" ) // destroyLocalGithubCmd represents the destroyLocalGithub command @@ -41,6 +52,82 @@ var destroyLocalGithubCmd = &cobra.Command{ ) } log.Println(destroyFlags, config) + + // todo: wrap business logic into the handler + if config.GitHubPersonalAccessToken == "" { + + httpClient := http.DefaultClient + gitHubService := services.NewGitHubService(httpClient) + gitHubHandler := handlers.NewGitHubHandler(gitHubService) + gitHubAccessToken, err := gitHubHandler.AuthenticateUser() + if err != nil { + return err + } + + if len(gitHubAccessToken) == 0 { + return errors.New("unable to retrieve a GitHub token for the user") + } + + err = os.Setenv("KUBEFIRST_GITHUB_AUTH_TOKEN", gitHubAccessToken) + if err != nil { + return errors.New("unable to set KUBEFIRST_GITHUB_AUTH_TOKEN") + } + + // todo: set common way to load env. values (viper->struct->load-env) + // todo: use viper file to load it, not load env. value + if err := os.Setenv("KUBEFIRST_GITHUB_AUTH_TOKEN", gitHubAccessToken); err != nil { + return err + } + log.Println("\nKUBEFIRST_GITHUB_AUTH_TOKEN set via OAuth") + } + + // todo add progress bars to this + + //* step 1.1 - open port-forward to state store and vault + // todo --skip-git-terraform + kPortForwardMinio, err := k8s.PortForward(globalFlags.DryRun, "minio", "svc/minio", "9000:9000") + defer func() { + err = kPortForwardMinio.Process.Signal(syscall.SIGTERM) + if err != nil { + log.Println("Error closing kPortForwardMinio") + } + }() + kPortForwardVault, err := k8s.PortForward(globalFlags.DryRun, "vault", "svc/vault", "8200:8200") + defer func() { + err = kPortForwardVault.Process.Signal(syscall.SIGTERM) + if err != nil { + log.Println("Error closing kPortForwardVault") + } + }() + + //* step 1.3 - terraform destroy github + githubTfApplied := viper.GetBool("terraform.github.apply.complete") + if githubTfApplied { + informUser("terraform destroying github resources", globalFlags.SilentMode) + tfEntrypoint := config.GitOpsRepoPath + "/terraform/github" + terraform.InitDestroyAutoApprove(globalFlags.DryRun, tfEntrypoint) + informUser("successfully destroyed github resources", globalFlags.SilentMode) + } + + //* step 2 - delete k3d cluster + // this could be useful for us to chase down in eks and destroy everything + // in the cloud / cluster minus eks to iterate from argocd forward + // todo --skip-cluster-destroy + informUser("deleting k3d cluster", globalFlags.SilentMode) + k3d.DeleteK3dCluster() + informUser("k3d cluster deleted", globalFlags.SilentMode) + informUser("be sure to run `kubefirst clean` before your next cloud provision", globalFlags.SilentMode) + + //* step 3 - clean local .k1 dir + // err = cleanCmd.RunE(cmd, args) + // if err != nil { + // log.Println("Error running:", cleanCmd.Name()) + // return err + // } + + fmt.Println("End of execution destroy") + time.Sleep(time.Millisecond * 100) + return nil }, } diff --git a/internal/gitClient/git.go b/internal/gitClient/git.go index 42b32b953..5cfa03bb7 100644 --- a/internal/gitClient/git.go +++ b/internal/gitClient/git.go @@ -19,6 +19,12 @@ import ( "golang.org/x/crypto/ssh" ) +// Github - git-provider github +const Github = "github" + +// Gitlab - git-provider github +const Gitlab = "gitlab" + // CloneRepoAndDetokenizeTemplate - clone repo using CloneRepoAndDetokenizeTemplate that uses fallback rule to try to capture version func CloneRepoAndDetokenizeTemplate(githubOwner, repoName, folderName string, branch string, tag string) (string, error) { config := configs.ReadConfig() From 2e28961622940b2521d7cb361d4142524791941c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9ssica=20Marinho?= Date: Wed, 9 Nov 2022 16:59:37 -0300 Subject: [PATCH 3/3] Set github aws destroy (#700) Signed-off-by: Jessica Marinho Signed-off-by: Jessica Marinho Co-authored-by: Jessica Marinho --- cmd/destroyAwsGithub.go | 108 +++++++++++++++++++++++++++++++- internal/flagset/destroy.go | 9 +++ internal/terraform/terraform.go | 1 + 3 files changed, 117 insertions(+), 1 deletion(-) diff --git a/cmd/destroyAwsGithub.go b/cmd/destroyAwsGithub.go index 9030daf16..f5acb1752 100644 --- a/cmd/destroyAwsGithub.go +++ b/cmd/destroyAwsGithub.go @@ -1,16 +1,26 @@ /* Copyright © 2022 NAME HERE - */ package cmd import ( + "errors" "fmt" "log" + "net/http" + "os" + "syscall" + "time" "github.com/kubefirst/kubefirst/configs" "github.com/kubefirst/kubefirst/internal/flagset" + "github.com/kubefirst/kubefirst/internal/handlers" + "github.com/kubefirst/kubefirst/internal/k8s" + "github.com/kubefirst/kubefirst/internal/progressPrinter" + "github.com/kubefirst/kubefirst/internal/services" + "github.com/kubefirst/kubefirst/internal/terraform" "github.com/spf13/cobra" + "github.com/spf13/viper" ) // destroyAwsGithubCmd represents the destroyAwsGithub command @@ -40,6 +50,102 @@ var destroyAwsGithubCmd = &cobra.Command{ globalFlags.SilentMode, ) } + + gitHubAccessToken := config.GitHubPersonalAccessToken + httpClient := http.DefaultClient + gitHubService := services.NewGitHubService(httpClient) + gitHubHandler := handlers.NewGitHubHandler(gitHubService) + + if gitHubAccessToken == "" { + + gitHubAccessToken, err = gitHubHandler.AuthenticateUser() + if err != nil { + return err + } + + if gitHubAccessToken == "" { + return errors.New("cannot create a cluster without a github auth token. please export your " + + "KUBEFIRST_GITHUB_AUTH_TOKEN in your terminal", + ) + } + + // todo: set common way to load env. values (viper->struct->load-env) + if err := os.Setenv("KUBEFIRST_GITHUB_AUTH_TOKEN", gitHubAccessToken); err != nil { + return err + } + log.Println("\nKUBEFIRST_GITHUB_AUTH_TOKEN set via OAuth") + } + + progressPrinter.SetupProgress(2, globalFlags.SilentMode) + + if globalFlags.DryRun { + destroyFlags.SkipDeleteRegistryApplication = true + destroyFlags.SkipBaseTerraform = true + destroyFlags.SkipGithubTerraform = true + } + progressPrinter.AddTracker("step-prepare", "Open Ports", 3) + + informUser("Open argocd port-forward", globalFlags.SilentMode) + progressPrinter.IncrementTracker("step-prepare", 1) + + progressPrinter.AddTracker("step-destroy", "Destroy Cloud", 4) + progressPrinter.IncrementTracker("step-destroy", 1) + + if !destroyFlags.SkipGithubTerraform { + githubTfApplied := viper.GetBool("terraform.github.apply.complete") + if githubTfApplied { + informUser("terraform destroying github resources", globalFlags.SilentMode) + tfEntrypoint := config.GitOpsRepoPath + "/terraform/github" + terraform.InitDestroyAutoApprove(globalFlags.DryRun, tfEntrypoint) + informUser("successfully destroyed github resources", globalFlags.SilentMode) + } + log.Println("github terraform destruction complete") + } + + progressPrinter.IncrementTracker("step-destroy", 1) + + //This should wrapped into a function, maybe to move to: k8s.DeleteRegistryApplication + if !destroyFlags.SkipDeleteRegistryApplication { + kPortForwardArgocd, _ := k8s.PortForward(globalFlags.DryRun, "argocd", "svc/argocd-server", "8080:80") + defer func() { + if kPortForwardArgocd != nil { + log.Println("Closed argocd port forward") + _ = kPortForwardArgocd.Process.Signal(syscall.SIGTERM) + } + }() + informUser("Open argocd port-forward", globalFlags.SilentMode) + progressPrinter.IncrementTracker("step-prepare", 1) + + log.Println("deleting registry application in argocd") + // delete argocd registry + informUser("Destroying Registry Application", globalFlags.SilentMode) + k8s.DeleteRegistryApplication(destroyFlags.SkipDeleteRegistryApplication) + log.Println("registry application deleted") + } + + progressPrinter.IncrementTracker("step-destroy", 1) + + log.Println("terraform destroy base") + informUser("Destroying Cluster", globalFlags.SilentMode) + terraform.DestroyBaseTerraform(destroyFlags.SkipBaseTerraform) + progressPrinter.IncrementTracker("step-destroy", 1) + + // destroy hosted zone + if destroyFlags.HostedZoneDelete { + hostedZone := viper.GetString("aws.hostedzonename") + awsHandler := handlers.NewAwsHandler(hostedZone, destroyFlags) + err := awsHandler.HostedZoneDelete() + if err != nil { + // if error, just log it + log.Println(err) + } + } + + informUser("All Destroyed", globalFlags.SilentMode) + + fmt.Println("End of execution destroy") + time.Sleep(time.Millisecond * 100) + log.Println(destroyFlags, config) return nil }, diff --git a/internal/flagset/destroy.go b/internal/flagset/destroy.go index 8e90a67f4..93821143b 100644 --- a/internal/flagset/destroy.go +++ b/internal/flagset/destroy.go @@ -10,6 +10,7 @@ import ( // DestroyFlags - Global flags type DestroyFlags struct { SkipGitlabTerraform bool + SkipGithubTerraform bool SkipDeleteRegistryApplication bool SkipBaseTerraform bool HostedZoneDelete bool @@ -19,6 +20,7 @@ type DestroyFlags struct { // DefineDestroyFlags - Define global flags func DefineDestroyFlags(currentCommand *cobra.Command) { currentCommand.Flags().Bool("skip-gitlab-terraform", false, "whether to skip the terraform destroy against gitlab - note: if you already deleted registry it doesnt exist") + currentCommand.Flags().Bool("skip-github-terraform", false, "whether to skip the terraform destroy against github - note: if you already deleted registry it doesnt exist") currentCommand.Flags().Bool("skip-delete-register", false, "whether to skip deletion of register application") currentCommand.Flags().Bool("skip-base-terraform", false, "whether to skip the terraform destroy against base install - note: if you already deleted registry it doesnt exist") @@ -37,6 +39,13 @@ func ProcessDestroyFlags(cmd *cobra.Command) (DestroyFlags, error) { } flags.SkipGitlabTerraform = skipGitlabTerraform + skipGithubTerraform, err := ReadConfigBool(cmd, "skip-github-terraform") + if err != nil { + log.Printf("Error Processing - skip-github-terraform, error: %v", err) + return flags, err + } + flags.SkipGithubTerraform = skipGithubTerraform + skipDeleteRegistryApplication, err := ReadConfigBool(cmd, "skip-delete-register") if err != nil { log.Printf("Error Processing - skip-delete-register flag, error: %v", err) diff --git a/internal/terraform/terraform.go b/internal/terraform/terraform.go index f205b1904..9af32ef41 100644 --- a/internal/terraform/terraform.go +++ b/internal/terraform/terraform.go @@ -196,6 +196,7 @@ func DestroyBaseTerraform(skipBaseTerraform bool) { } viper.Set("destroy.terraformdestroy.base", true) viper.WriteConfig() + log.Println("terraform base destruction complete") } else { log.Println("skip: destroyBaseTerraform") }