From 1e5f9858d106b0fc555f9776f6fdab4594beb96f Mon Sep 17 00:00:00 2001 From: mrrishi Date: Tue, 3 Sep 2024 17:57:22 +0530 Subject: [PATCH] rebase --- cmd/akamai/command.go | 16 +------- cmd/akamai/create.go | 10 ++--- cmd/civo/backup.go | 2 + cmd/k3d/create.go | 7 +--- cmd/k3d/destroy.go | 1 + cmd/k3d/mkcert.go | 1 + cmd/k3d/root-credentials.go | 1 + cmd/k3d/vault.go | 10 ++--- cmd/vultr/command.go | 5 --- go.mod | 2 +- go.sum | 4 +- internal/common/common.go | 78 ++++++++++++++++++++++++++++++++++++- 12 files changed, 96 insertions(+), 41 deletions(-) diff --git a/cmd/akamai/command.go b/cmd/akamai/command.go index 7fbbe8a4d..e97dd2897 100644 --- a/cmd/akamai/command.go +++ b/cmd/akamai/command.go @@ -31,15 +31,10 @@ var ( gitProtocolFlag string gitopsTemplateURLFlag string gitopsTemplateBranchFlag string - gitopsRepoName string - metaphorRepoName string - adminTeamName string - developerTeamName string useTelemetryFlag bool nodeTypeFlag string nodeCountFlag string installCatalogApps string - installKubefirstProFlag bool // RootCredentials copyArgoCDPasswordToClipboardFlag bool @@ -60,7 +55,7 @@ func NewCommand() *cobra.Command { Long: "kubefirst akamai", Run: func(cmd *cobra.Command, args []string) { fmt.Println("To learn more about akamai in kubefirst, run:") - fmt.Println(" kubefirst beta akamai --help") + fmt.Println(" kubefirst akamai --help") if progress.Progress != nil { progress.Progress.Quit() @@ -105,15 +100,6 @@ func Create() *cobra.Command { createCmd.Flags().StringVar(&gitopsTemplateURLFlag, "gitops-template-url", "https://github.com/kubefirst/gitops-template.git", "the fully qualified url to the gitops-template repository to clone") createCmd.Flags().StringVar(&installCatalogApps, "install-catalog-apps", "", "comma seperated values to install after provision") createCmd.Flags().BoolVar(&useTelemetryFlag, "use-telemetry", true, "whether to emit telemetry") - createCmd.Flags().StringVar(&gitopsRepoName, "gitops-repo-name", "gitops", "the custom gitops name") - createCmd.Flags().StringVar(&metaphorRepoName, "metaphor-repo-name", "metaphor", "the custom metaphor name") - createCmd.Flags().StringVar(&adminTeamName, "admin-team-name", "admins", "admin team name for this repo") - createCmd.Flags().StringVar(&developerTeamName, "developer-team-name", "developers", "developer team name for this repo") - createCmd.Flags().BoolVar(&installKubefirstProFlag, "install-kubefirst-pro", true, "whether or not to install kubefirst pro") - createCmd.Flags().StringVar(&gitopsRepoName, "gitops-repo-name", "gitops", "the custom gitops name") - createCmd.Flags().StringVar(&metaphorRepoName, "metaphor-repo-name", "metaphor", "the custom metaphor name") - createCmd.Flags().StringVar(&adminTeamName, "admin-team-name", "admins", "admin team name for this repo") - createCmd.Flags().StringVar(&developerTeamName, "developer-team-name", "developers", "developer team name for this repo") return createCmd } diff --git a/cmd/akamai/create.go b/cmd/akamai/create.go index ba9419716..752e3cced 100644 --- a/cmd/akamai/create.go +++ b/cmd/akamai/create.go @@ -9,7 +9,6 @@ package akamai import ( "fmt" "os" - "strings" internalssh "github.com/konstructio/kubefirst-api/pkg/ssh" pkg "github.com/konstructio/kubefirst-api/pkg/utils" @@ -50,6 +49,7 @@ func createAkamai(cmd *cobra.Command, args []string) error { utilities.CreateK1ClusterDirectory(clusterNameFlag) gitAuth, err := gitShim.ValidateGitCredentials(cliFlags.GitProvider, cliFlags.GithubOrg, cliFlags.GitlabGroup) + if err != nil { progress.Error(err.Error()) return nil @@ -58,8 +58,8 @@ func createAkamai(cmd *cobra.Command, args []string) error { // Validate git executionControl := viper.GetBool(fmt.Sprintf("kubefirst-checks.%s-credentials", cliFlags.GitProvider)) if !executionControl { - newRepositoryNames := []string{cliFlags.GitopsRepoName, cliFlags.MetaphorRepoName} - newTeamNames := []string{cliFlags.AdminTeamName, cliFlags.DeveloperTeamName} + newRepositoryNames := []string{"gitops", "metaphor"} + newTeamNames := []string{"admins", "developers"} initGitParameters := gitShim.GitInitParameters{ GitProvider: cliFlags.GitProvider, @@ -79,9 +79,7 @@ func createAkamai(cmd *cobra.Command, args []string) error { viper.WriteConfig() k3dClusterCreationComplete := viper.GetBool("launch.deployed") - isK1Debug := strings.ToLower(os.Getenv("K1_LOCAL_DEBUG")) == "true" - - if !k3dClusterCreationComplete && !isK1Debug { + if !k3dClusterCreationComplete { launch.Up(nil, true, cliFlags.UseTelemetry) } diff --git a/cmd/civo/backup.go b/cmd/civo/backup.go index b8d46c586..acd37dc2d 100644 --- a/cmd/civo/backup.go +++ b/cmd/civo/backup.go @@ -56,6 +56,8 @@ func backupCivoSSL(cmd *cobra.Command, args []string) error { os.Getenv("CF_ORIGIN_CA_ISSUER_API_TOKEN"), gitopsRepoName, metaphorRepoName, + adminTeamName, + developerTeamName, ) if _, err := os.Stat(config.SSLBackupDir + "/certificates"); os.IsNotExist(err) { diff --git a/cmd/k3d/create.go b/cmd/k3d/create.go index 4ca54ef1f..12a871ccb 100644 --- a/cmd/k3d/create.go +++ b/cmd/k3d/create.go @@ -15,7 +15,6 @@ import ( "os" "strconv" "strings" - "syscall" "time" "github.com/atotto/clipboard" @@ -52,9 +51,7 @@ import ( "github.com/minio/minio-go/v7/pkg/credentials" "github.com/spf13/cobra" "github.com/spf13/viper" - apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/util/wait" ) func runK3d(cmd *cobra.Command, args []string) error { @@ -342,7 +339,7 @@ func runK3d(cmd *cobra.Command, args []string) error { //} // Instantiate K3d config - config := k3d.GetConfig(clusterNameFlag, gitProviderFlag, cGitOwner, gitProtocolFlag) + config := k3d.GetConfig(clusterNameFlag, gitProviderFlag, cGitOwner, gitProtocolFlag, gitopsRepoName, metaphorRepoName, adminTeamName, developerTeamName) switch gitProviderFlag { case "github": config.GithubToken = cGitToken @@ -554,7 +551,7 @@ func runK3d(cmd *cobra.Command, args []string) error { if !viper.GetBool("kubefirst-checks.tools-downloaded") { log.Info().Msg("installing kubefirst dependencies") - err := k3d.DownloadTools(clusterNameFlag, config.GitProvider, cGitOwner, config.ToolsDir, config.GitProtocol) + err := k3d.DownloadTools(clusterNameFlag, config.GitProvider, cGitOwner, config.ToolsDir, config.GitProtocol, gitopsRepoName, metaphorRepoName, adminTeamName, developerTeamName) if err != nil { return err } diff --git a/cmd/k3d/destroy.go b/cmd/k3d/destroy.go index e321d5316..0a233a84c 100644 --- a/cmd/k3d/destroy.go +++ b/cmd/k3d/destroy.go @@ -21,6 +21,7 @@ import ( "github.com/konstructio/kubefirst-api/pkg/k8s" "github.com/konstructio/kubefirst-api/pkg/progressPrinter" "github.com/konstructio/kubefirst-api/pkg/terraform" + "github.com/konstructio/kubefirst/internal/common" "github.com/konstructio/kubefirst/internal/progress" "github.com/rs/zerolog/log" "github.com/spf13/cobra" diff --git a/cmd/k3d/mkcert.go b/cmd/k3d/mkcert.go index 6bee98f5e..0f2e8ee24 100644 --- a/cmd/k3d/mkcert.go +++ b/cmd/k3d/mkcert.go @@ -12,6 +12,7 @@ import ( "github.com/konstructio/kubefirst-api/pkg/k3d" "github.com/konstructio/kubefirst-api/pkg/k8s" utils "github.com/konstructio/kubefirst-api/pkg/utils" + "github.com/konstructio/kubefirst/internal/common" "github.com/konstructio/kubefirst/internal/progress" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" diff --git a/cmd/k3d/root-credentials.go b/cmd/k3d/root-credentials.go index 36f9ad1c7..dd1081f0e 100644 --- a/cmd/k3d/root-credentials.go +++ b/cmd/k3d/root-credentials.go @@ -12,6 +12,7 @@ import ( "github.com/konstructio/kubefirst-api/pkg/credentials" "github.com/konstructio/kubefirst-api/pkg/k3d" "github.com/konstructio/kubefirst-api/pkg/k8s" + "github.com/konstructio/kubefirst/internal/common" "github.com/konstructio/kubefirst/internal/progress" "github.com/spf13/cobra" "github.com/spf13/viper" diff --git a/cmd/k3d/vault.go b/cmd/k3d/vault.go index 283eede95..f0e620b15 100644 --- a/cmd/k3d/vault.go +++ b/cmd/k3d/vault.go @@ -14,11 +14,11 @@ import ( "time" vaultapi "github.com/hashicorp/vault/api" - "github.com/kubefirst/kubefirst-api/pkg/k3d" - "github.com/kubefirst/kubefirst-api/pkg/k8s" - utils "github.com/kubefirst/kubefirst-api/pkg/utils" - "github.com/kubefirst/kubefirst/internal/common" - "github.com/kubefirst/kubefirst/internal/progress" + "github.com/konstructio/kubefirst-api/pkg/k3d" + "github.com/konstructio/kubefirst-api/pkg/k8s" + utils "github.com/konstructio/kubefirst-api/pkg/utils" + "github.com/konstructio/kubefirst/internal/common" + "github.com/konstructio/kubefirst/internal/progress" "github.com/rs/zerolog/log" "github.com/spf13/cobra" "github.com/spf13/viper" diff --git a/cmd/vultr/command.go b/cmd/vultr/command.go index 978f0815b..880634e22 100644 --- a/cmd/vultr/command.go +++ b/cmd/vultr/command.go @@ -112,13 +112,8 @@ func Create() *cobra.Command { createCmd.Flags().StringVar(&adminTeamName, "adminTeamName", "admins", "admin team name for this repo ") createCmd.Flags().StringVar(&developerTeamName, "developerTeamName", "developers", " developer team name for this repo") createCmd.Flags().StringVar(&installCatalogApps, "install-catalog-apps", "", "comma seperated values to install after provision") - createCmd.Flags().StringVar(&installCatalogApps, "install-catalog-apps", "", "comma separated values to install after provision") createCmd.Flags().BoolVar(&useTelemetryFlag, "use-telemetry", true, "whether to emit telemetry") createCmd.Flags().BoolVar(&installKubefirstProFlag, "install-kubefirst-pro", true, "whether or not to install kubefirst pro") - createCmd.Flags().StringVar(&gitopsRepoName, "gitops-repo-name", "gitops", "the custom gitops name") - createCmd.Flags().StringVar(&metaphorRepoName, "metaphor-repo-name", "metaphor", "the custom metaphor name") - createCmd.Flags().StringVar(&adminTeamName, "admin-team-name", "admins", "admin team name for this repo") - createCmd.Flags().StringVar(&developerTeamName, "developer-team-name", "developers", "developer team name for this repo") return createCmd } diff --git a/go.mod b/go.mod index 3ccbc5510..efe1aa6e9 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( github.com/dustin/go-humanize v1.0.1 github.com/go-git/go-git/v5 v5.6.1 github.com/hashicorp/vault/api v1.9.0 - github.com/konstructio/kubefirst-api v0.102.0 + github.com/konstructio/kubefirst-api v0.105.0 github.com/kubefirst/metrics-client v0.3.0 github.com/nxadm/tail v1.4.8 github.com/rs/zerolog v1.29.1 diff --git a/go.sum b/go.sum index 2e67aec77..4c4c439cf 100644 --- a/go.sum +++ b/go.sum @@ -720,8 +720,8 @@ github.com/klauspost/cpuid/v2 v2.2.3/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8t github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/konstructio/kubefirst-api v0.102.0 h1:0l1q8TbD+K3ShQCb1Nv5AJdjc/BVso2nQY2MLIMZWHE= -github.com/konstructio/kubefirst-api v0.102.0/go.mod h1:jpBfSHjZhzcotYBvX4GG7H/y3VZANxri37/3ZO6B/Hk= +github.com/konstructio/kubefirst-api v0.105.0 h1:iy8P3wVI+BySJr2H3+sFZgbkoH9SikDXvmjD/BTHA6E= +github.com/konstructio/kubefirst-api v0.105.0/go.mod h1:jpBfSHjZhzcotYBvX4GG7H/y3VZANxri37/3ZO6B/Hk= github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= diff --git a/internal/common/common.go b/internal/common/common.go index 870495bd4..54909510c 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -52,7 +52,7 @@ func CheckForVersionUpdate() { case "darwin": fmt.Printf("A newer version (v%s) is available! Please upgrade with: \"brew update && brew upgrade kubefirst\"\n", res.Current) default: - fmt.Printf("A newer version (v%s) is available! \"https://github.com/konstructio/kubefirst/blob/main/build/README.md\"\n", res.Current) + fmt.Printf("A newer version (v%s) is available! \"https://github.com/kubefirst/kubefirst/blob/main/build/README.md\"\n", res.Current) } } } @@ -65,6 +65,7 @@ func versionCheck() (res *CheckResponse, skip bool) { flatVersion := strings.ReplaceAll(configs.K1Version, "v", "") resp, err := http.Get("https://raw.githubusercontent.com/Homebrew/homebrew-core/master/Formula/k/kubefirst.rb") + if err != nil { fmt.Printf("checking for a newer version failed (cannot get Homebrew formula) with: %s", err) return nil, true @@ -83,7 +84,7 @@ func versionCheck() (res *CheckResponse, skip bool) { } bodyString := string(bodyBytes) - if !strings.Contains(bodyString, "url \"https://github.com/konstructio/kubefirst/archive/refs/tags/") { + if !strings.Contains(bodyString, "url \"https://github.com/kubefirst/kubefirst/archive/refs/tags/") { fmt.Printf("checking for a newer version failed (no reference to kubefirst release) with: %s", err) return nil, true } @@ -114,6 +115,70 @@ func GetRootCredentials(cmd *cobra.Command, args []string) error { return nil } +func GetGitmeta(clusterName string) (string, string, error) { + + var gitopsFound, metaphorFound bool + var gitopsRepoName, metaphorRepoName string + + homePath, err := os.UserHomeDir() + if err != nil { + return "", "", fmt.Errorf("unable to get user's home directory: %w", err) + } + + basePath := filepath.Join(homePath, ".k1", clusterName) + + err = filepath.WalkDir(basePath, func(path string, info fs.DirEntry, err error) error { + if err != nil { + return err + } + + if info.IsDir() { + relPath, err := filepath.Rel(basePath, path) + + if err != nil { + return fmt.Errorf("error while finding repository : %w", err) + } + + if relPath == "." || strings.Count(relPath, string(os.PathSeparator)) == 1 { + if info.Name() == "registry" { + if !gitopsFound { + gitopsRepoName = filepath.Dir(relPath) + gitopsFound = true + } + } + if info.Name() == ".github" { + if !metaphorFound { + metaphorRepoName = filepath.Dir(relPath) + metaphorFound = true + } + } + } + } + if metaphorFound && gitopsFound { + return fs.SkipDir + } + + return nil + }) + + if err != nil { + log.Info().Msg("Error reading directory") + return "", "", fmt.Errorf("Error Reading %w", err) + } + + if !gitopsFound { + log.Info().Msg("Gitops Repo not found") + return "", "", fmt.Errorf("Gitopsrepo Not Found") + } + + if !metaphorFound { + log.Info().Msg("Metaphor Repo not found") + return "", "", fmt.Errorf("MetaphorRepo Not Found") + } + + return gitopsRepoName, metaphorRepoName, nil +} + func Destroy(cmd *cobra.Command, args []string) error { // Determine if there are active instal ls gitProvider := viper.GetString("flags.git-provider") @@ -125,6 +190,11 @@ func Destroy(cmd *cobra.Command, args []string) error { clusterName := viper.GetString("flags.cluster-name") domainName := viper.GetString("flags.domain-name") + gitopsRepoName, metaphorRepoName, err := GetGitmeta(clusterName) + + if err != nil { + return err + } // Switch based on git provider, set params cGitOwner := "" switch gitProvider { @@ -145,6 +215,10 @@ func Destroy(cmd *cobra.Command, args []string) error { gitProtocol, os.Getenv("CF_API_TOKEN"), os.Getenv("CF_ORIGIN_CA_ISSUER_API_TOKEN"), + gitopsRepoName, + metaphorRepoName, + "admins", + "developers", ) progress.AddStep("Destroying k3d")