-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* new destroy by flavor Signed-off-by: 6za <53096417+6za@users.noreply.github.com> * Review destroy Signed-off-by: 6za <53096417+6za@users.noreply.github.com> * Set github aws destroy (#700) Signed-off-by: Jessica Marinho <jessica@kubeshop.io> Signed-off-by: Jessica Marinho <jessica@kubeshop.io> Co-authored-by: Jessica Marinho <jessica@kubeshop.io> Signed-off-by: 6za <53096417+6za@users.noreply.github.com> Signed-off-by: Jessica Marinho <jessica@kubeshop.io> Co-authored-by: Jéssica Marinho <jlmarinhocosta@gmail.com> Co-authored-by: Jessica Marinho <jessica@kubeshop.io>
- Loading branch information
1 parent
a9f0de0
commit 2cada58
Showing
7 changed files
with
471 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
Copyright © 2022 NAME HERE <EMAIL ADDRESS> | ||
*/ | ||
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 | ||
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, | ||
) | ||
} | ||
|
||
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 | ||
}, | ||
} | ||
|
||
func init() { | ||
clusterCmd.AddCommand(destroyAwsGithubCmd) | ||
currentCommand := destroyAwsGithubCmd | ||
flagset.DefineGlobalFlags(currentCommand) | ||
flagset.DefineDestroyFlags(currentCommand) | ||
} |
Oops, something went wrong.