Skip to content

Commit

Permalink
feat: civo create using launch up (#1820)
Browse files Browse the repository at this point in the history
  • Loading branch information
CristhianF7 authored Sep 19, 2023
1 parent 378dfff commit a11fe13
Show file tree
Hide file tree
Showing 14 changed files with 1,146 additions and 1,657 deletions.
1,366 changes: 93 additions & 1,273 deletions cmd/civo/create.go

Large diffs are not rendered by default.

259 changes: 18 additions & 241 deletions cmd/civo/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,25 @@ See the LICENSE file for more details.
package civo

import (
"crypto/tls"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"time"

"github.com/civo/civogo"
"github.com/kubefirst/kubefirst/internal/launch"
"github.com/kubefirst/kubefirst/internal/progress"
"github.com/kubefirst/runtime/pkg"
"github.com/kubefirst/runtime/pkg/argocd"
"github.com/kubefirst/runtime/pkg/civo"
gitlab "github.com/kubefirst/runtime/pkg/gitlab"
"github.com/kubefirst/runtime/pkg/helpers"
"github.com/kubefirst/runtime/pkg/k8s"
"github.com/kubefirst/runtime/pkg/progressPrinter"
"github.com/kubefirst/runtime/pkg/providerConfigs"
"github.com/kubefirst/runtime/pkg/terraform"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func destroyCivo(cmd *cobra.Command, args []string) error {
helpers.DisplayLogHints()
progress.DisplayLogHints()

// Determine if there are active installs
gitProvider := viper.GetString("flags.git-provider")
gitProtocol := viper.GetString("flags.git-protocol")
// _, err := helpers.EvalDestroy(civo.CloudProvider, gitProvider)
// if err != nil {
// return err
// }

// Check for existing port forwards before continuing
err := k8s.CheckForExistingPortForwards(8080)
if err != nil {
return fmt.Errorf("%s - this port is required to tear down your kubefirst environment - please close any existing port forwards before continuing", err.Error())
}

progressPrinter.AddTracker("preflight-checks", "Running preflight checks", 1)
progressPrinter.SetupProgress(progressPrinter.TotalOfTrackers(), false)

log.Info().Msg("destroying kubefirst platform in civo")

Expand Down Expand Up @@ -95,232 +72,32 @@ func destroyCivo(cmd *cobra.Command, args []string) error {
if len(config.CivoToken) == 0 {
return fmt.Errorf("\n\nYour CIVO_TOKEN environment variable isn't set,\nvisit this link https://dashboard.civo.com/security and set the environment variable")
}
progressPrinter.IncrementTracker("preflight-checks", 1)

progressPrinter.AddTracker("platform-destroy", "Destroying your kubefirst platform", 2)
progressPrinter.SetupProgress(progressPrinter.TotalOfTrackers(), false)

switch gitProvider {
case "github":
if viper.GetBool("kubefirst-checks.terraform-apply-github") {
log.Info().Msg("destroying github resources with terraform")

tfEntrypoint := config.GitopsDir + "/terraform/github"
tfEnvs := map[string]string{}
tfEnvs = civo.GetCivoTerraformEnvs(config, tfEnvs)
tfEnvs = civo.GetGithubTerraformEnvs(config, tfEnvs)
err := terraform.InitDestroyAutoApprove(config.TerraformClient, tfEntrypoint, tfEnvs)
if err != nil {
log.Printf("error executing terraform destroy %s", tfEntrypoint)
return err
}
viper.Set("kubefirst-checks.terraform-apply-github", false)
viper.WriteConfig()
log.Info().Msg("github resources terraform destroyed")
progressPrinter.IncrementTracker("platform-destroy", 1)
}
case "gitlab":
if viper.GetBool("kubefirst-checks.terraform-apply-gitlab") {
log.Info().Msg("destroying gitlab resources with terraform")
gitlabClient, err := gitlab.NewGitLabClient(cGitToken, cGitOwner)
if err != nil {
return err
}

// Before removing Terraform resources, remove any container registry repositories
// since failing to remove them beforehand will result in an apply failure
var projectsForDeletion = []string{"gitops", "metaphor"}
for _, project := range projectsForDeletion {
projectExists, err := gitlabClient.CheckProjectExists(project)
if err != nil {
log.Fatal().Msgf("could not check for existence of project %s: %s", project, err)
}
if projectExists {
log.Info().Msgf("checking project %s for container registries...", project)
crr, err := gitlabClient.GetProjectContainerRegistryRepositories(project)
if err != nil {
log.Fatal().Msgf("could not retrieve container registry repositories: %s", err)
}
if len(crr) > 0 {
for _, cr := range crr {
err := gitlabClient.DeleteContainerRegistryRepository(project, cr.ID)
if err != nil {
log.Fatal().Msgf("error deleting container registry repository: %s", err)
}
}
} else {
log.Info().Msgf("project %s does not have any container registries, skipping", project)
}
} else {
log.Info().Msgf("project %s does not exist, skipping", project)
}
}

tfEntrypoint := config.GitopsDir + "/terraform/gitlab"
tfEnvs := map[string]string{}
tfEnvs = civo.GetCivoTerraformEnvs(config, tfEnvs)
tfEnvs = civo.GetGitlabTerraformEnvs(config, tfEnvs, gitlabClient.ParentGroupID)
err = terraform.InitDestroyAutoApprove(config.TerraformClient, tfEntrypoint, tfEnvs)
if err != nil {
log.Printf("error executing terraform destroy %s", tfEntrypoint)
return err
}
viper.Set("kubefirst-checks.terraform-apply-gitlab", false)
viper.WriteConfig()
log.Info().Msg("github resources terraform destroyed")

progressPrinter.IncrementTracker("platform-destroy", 1)
}
}

if viper.GetBool("kubefirst-checks.terraform-apply-civo") || viper.GetBool("kubefirst-checks.terraform-apply-civo-failed") {
kcfg := k8s.CreateKubeConfig(false, config.Kubeconfig)

log.Info().Msg("destroying civo resources with terraform")

clusterName := viper.GetString("flags.cluster-name")
region := viper.GetString("flags.cloud-region")

client, err := civogo.NewClient(os.Getenv("CIVO_TOKEN"), region)
if err != nil {
return fmt.Errorf(err.Error())
}

cluster, err := client.FindKubernetesCluster(clusterName)
if err != nil {
return err
}
log.Info().Msg("cluster name: " + cluster.ID)

clusterVolumes, err := client.ListVolumesForCluster(cluster.ID)
if err != nil {
return err
}

// Only port-forward to ArgoCD and delete registry if ArgoCD was installed
if viper.GetBool("kubefirst-checks.argocd-install") {
log.Info().Msg("opening argocd port forward")
//* ArgoCD port-forward
argoCDStopChannel := make(chan struct{}, 1)
defer func() {
close(argoCDStopChannel)
}()
k8s.OpenPortForwardPodWrapper(
kcfg.Clientset,
kcfg.RestConfig,
"argocd-server",
"argocd",
8080,
8080,
argoCDStopChannel,
)

log.Info().Msg("getting new auth token for argocd")

secData, err := k8s.ReadSecretV2(kcfg.Clientset, "argocd", "argocd-initial-admin-secret")
if err != nil {
return err
}
argocdPassword := secData["password"]

argocdAuthToken, err := argocd.GetArgoCDToken("admin", argocdPassword)
if err != nil {
return err
}

log.Info().Msgf("port-forward to argocd is available at %s", providerConfigs.ArgocdPortForwardURL)

customTransport := http.DefaultTransport.(*http.Transport).Clone()
customTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
argocdHttpClient := http.Client{Transport: customTransport}
log.Info().Msg("deleting the registry application")
httpCode, _, err := argocd.DeleteApplication(&argocdHttpClient, config.RegistryAppName, argocdAuthToken, "true")
if err != nil {
return err
}
log.Info().Msgf("http status code %d", httpCode)
}

for _, vol := range clusterVolumes {
log.Info().Msg("removing volume with name: " + vol.Name)
_, err := client.DeleteVolume(vol.ID)
if err != nil {
return err
}
log.Info().Msg("volume " + vol.ID + " deleted")
}

// Pause before cluster destroy to prevent a race condition
log.Info().Msg("waiting for Civo Kubernetes cluster resource removal to finish...")
time.Sleep(time.Second * 10)

log.Info().Msg("destroying civo cloud resources")
tfEntrypoint := config.GitopsDir + "/terraform/civo"
tfEnvs := map[string]string{}
tfEnvs = civo.GetCivoTerraformEnvs(config, tfEnvs)
launch.Down(true)

switch gitProvider {
case "github":
tfEnvs = civo.GetGithubTerraformEnvs(config, tfEnvs)
case "gitlab":
gid, err := strconv.Atoi(viper.GetString("flags.gitlab-owner-group-id"))
if err != nil {
return fmt.Errorf("couldn't convert gitlab group id to int: %s", err)
}
tfEnvs = civo.GetGitlabTerraformEnvs(config, tfEnvs, gid)
}
err = terraform.InitDestroyAutoApprove(config.TerraformClient, tfEntrypoint, tfEnvs)
if err != nil {
log.Printf("error executing terraform destroy %s", tfEntrypoint)
return err
}
viper.Set("kubefirst-checks.terraform-apply-civo", false)
viper.WriteConfig()
log.Info().Msg("civo resources terraform destroyed")
progressPrinter.IncrementTracker("platform-destroy", 1)
}

// remove ssh key provided one was created
if viper.GetString("kbot.gitlab-user-based-ssh-key-title") != "" {
gitlabClient, err := gitlab.NewGitLabClient(cGitToken, cGitOwner)
if err != nil {
return err
}
log.Info().Msg("attempting to delete managed ssh key...")
err = gitlabClient.DeleteUserSSHKey(viper.GetString("kbot.gitlab-user-based-ssh-key-title"))
if err != nil {
log.Warn().Msg(err.Error())
}
err := pkg.ResetK1Dir(config.K1Dir)
if err != nil {
return err
}
log.Info().Msg("previous platform content removed")

//* remove local content and kubefirst config file for re-execution
if !viper.GetBool(fmt.Sprintf("kubefirst-checks.terraform-apply-%s", gitProvider)) && !viper.GetBool("kubefirst-checks.terraform-apply-civo") {
log.Info().Msg("removing previous platform content")

err := pkg.ResetK1Dir(config.K1Dir)
if err != nil {
return err
}
log.Info().Msg("previous platform content removed")

log.Info().Msg("resetting `$HOME/.kubefirst` config")
viper.Set("argocd", "")
viper.Set(gitProvider, "")
viper.Set("components", "")
viper.Set("kbot", "")
viper.Set("kubefirst-checks", "")
viper.Set("kubefirst", "")
viper.WriteConfig()
}
log.Info().Msg("resetting `$HOME/.kubefirst` config")
viper.Set("argocd", "")
viper.Set(gitProvider, "")
viper.Set("components", "")
viper.Set("kbot", "")
viper.Set("kubefirst-checks", "")
viper.Set("kubefirst", "")
viper.WriteConfig()

if _, err := os.Stat(config.K1Dir + "/kubeconfig"); !os.IsNotExist(err) {
err = os.Remove(config.K1Dir + "/kubeconfig")
if err != nil {
return fmt.Errorf("unable to delete %q folder, error: %s", config.K1Dir+"/kubeconfig", err)
}
}
time.Sleep(time.Second * 2) // allows progress bars to finish
fmt.Printf("Your kubefirst platform running in %s has been destroyed.", civo.CloudProvider)

progress.Success("Your kubefirst platform has been destroyed.")

return nil
}
4 changes: 2 additions & 2 deletions cmd/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func launchUp() *cobra.Command {
TraverseChildren: true,
PreRun: checkDocker,
Run: func(cmd *cobra.Command, args []string) {
launch.Up(additionalHelmFlags)
launch.Up(additionalHelmFlags, false, true)
},
}

Expand All @@ -57,7 +57,7 @@ func launchDown() *cobra.Command {
Short: "remove console and api instance",
TraverseChildren: true,
Run: func(cmd *cobra.Command, args []string) {
launch.Down()
launch.Down(false)
},
}

Expand Down
Loading

0 comments on commit a11fe13

Please sign in to comment.