From e5cac33f599edcb6e4f4cce8e2f285254e5e0140 Mon Sep 17 00:00:00 2001 From: M R Rishi Date: Thu, 1 Aug 2024 12:44:45 +0530 Subject: [PATCH] resolved the suggestions --- cmd/akamai/command.go | 16 ++++---- cmd/aws/command.go | 16 ++++---- cmd/civo/backup.go | 3 +- cmd/civo/command.go | 16 ++++---- cmd/civo/create.go | 6 +-- cmd/digitalocean/command.go | 16 ++++---- cmd/google/command.go | 16 ++++---- cmd/k3d/command.go | 12 +++--- cmd/k3d/create.go | 31 ++++++--------- cmd/k3d/destroy.go | 12 +++--- cmd/k3d/mkcert.go | 10 ++--- cmd/k3d/root-credentials.go | 6 +-- cmd/k3d/vault.go | 4 +- cmd/k3s/command.go | 16 ++++---- cmd/reset.go | 2 +- cmd/vultr/command.go | 16 ++++---- internal/common/common.go | 70 ++++++++++++++++++++------------- internal/k3d/menu.go | 2 - internal/types/flags.go | 4 +- internal/utilities/flags.go | 8 ++-- internal/utilities/utilities.go | 8 ++-- tools/aws-assume-role.sh | 2 +- 22 files changed, 148 insertions(+), 144 deletions(-) diff --git a/cmd/akamai/command.go b/cmd/akamai/command.go index a35cf9a03..17a8cd70d 100644 --- a/cmd/akamai/command.go +++ b/cmd/akamai/command.go @@ -31,10 +31,10 @@ var ( gitProtocolFlag string gitopsTemplateURLFlag string gitopsTemplateBranchFlag string - gitopsRepoName string - metaphorRepoName string - adminTeamName string - developerTeamName string + gitopsRepoName string + metaphorRepoName string + adminTeamName string + developerTeamName string useTelemetryFlag bool nodeTypeFlag string nodeCountFlag string @@ -102,10 +102,10 @@ func Create() *cobra.Command { createCmd.Flags().StringVar(&gitlabGroupFlag, "gitlab-group", "", "the GitLab group for the new gitops and metaphor projects - required if using gitlab") createCmd.Flags().StringVar(&gitopsTemplateBranchFlag, "gitops-template-branch", "", "the branch to clone for the gitops-template repository") 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(&gitopsRepoName,"gitopsRepoName","gitops","the custom gitops name") - createCmd.Flags().StringVar(&metaphorRepoName,"metaphorRepoName","metaphor","the custom metpahor name") - 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(&gitopsRepoName, "gitopsRepoName", "gitops", "the custom gitops name") + createCmd.Flags().StringVar(&metaphorRepoName, "metaphorRepoName", "metaphor", "the custom metpahor name") + 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().BoolVar(&useTelemetryFlag, "use-telemetry", true, "whether to emit telemetry") diff --git a/cmd/aws/command.go b/cmd/aws/command.go index e5bea7f22..cfb967be2 100644 --- a/cmd/aws/command.go +++ b/cmd/aws/command.go @@ -29,10 +29,10 @@ var ( gitProtocolFlag string gitopsTemplateURLFlag string gitopsTemplateBranchFlag string - gitopsRepoName string - metaphorRepoName string - adminTeamName string - developerTeamName string + gitopsRepoName string + metaphorRepoName string + adminTeamName string + developerTeamName string domainNameFlag string subdomainNameFlag string useTelemetryFlag bool @@ -104,10 +104,10 @@ func Create() *cobra.Command { createCmd.Flags().StringVar(&gitlabGroupFlag, "gitlab-group", "", "the GitLab group for the new gitops and metaphor projects - required if using gitlab") createCmd.Flags().StringVar(&gitopsTemplateBranchFlag, "gitops-template-branch", "", "the branch to clone for the gitops-template repository") 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(&gitopsRepoName,"gitopsRepoName","gitops","the custom gitops name") - createCmd.Flags().StringVar(&metaphorRepoName,"metaphorRepoName","metaphor","the custom metpahor name") - 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(&gitopsRepoName, "gitopsRepoName", "gitops", "the custom gitops name") + createCmd.Flags().StringVar(&metaphorRepoName, "metaphorRepoName", "metaphor", "the custom metpahor name") + 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().BoolVar(&useTelemetryFlag, "use-telemetry", true, "whether to emit telemetry") createCmd.Flags().BoolVar(&ecrFlag, "ecr", false, "whether or not to use ecr vs the git provider") diff --git a/cmd/civo/backup.go b/cmd/civo/backup.go index 5c8179927..6c4cbdb19 100644 --- a/cmd/civo/backup.go +++ b/cmd/civo/backup.go @@ -35,7 +35,7 @@ func backupCivoSSL(cmd *cobra.Command, args []string) error { default: log.Panic().Msgf("invalid git provider option") } - + gitopsRepoName, err := cmd.Flags().GetString("gitopRepoName") if err != nil { return err @@ -46,7 +46,6 @@ func backupCivoSSL(cmd *cobra.Command, args []string) error { return err } - config := providerConfigs.GetConfig( clusterName, domainName, diff --git a/cmd/civo/command.go b/cmd/civo/command.go index 8ef7825a9..c06a8d011 100644 --- a/cmd/civo/command.go +++ b/cmd/civo/command.go @@ -36,10 +36,10 @@ var ( nodeCountFlag string installCatalogApps string installKubefirstProFlag bool - gitopsRepoName string - metaphorRepoName string - adminTeamName string - developerTeamName string + gitopsRepoName string + metaphorRepoName string + adminTeamName string + developerTeamName string // RootCredentials copyArgoCDPasswordToClipboardFlag bool @@ -109,10 +109,10 @@ func Create() *cobra.Command { createCmd.Flags().StringVar(&subdomainNameFlag, "subdomain", "", "the subdomain to use for DNS records (Cloudflare)") createCmd.Flags().StringVar(&domainNameFlag, "domain-name", "", "the Civo DNS Name to use for DNS records (i.e. your-domain.com|subdomain.your-domain.com) (required)") createCmd.MarkFlagRequired("domain-name") - createCmd.Flags().StringVar(&gitopsRepoName,"gitopsRepoName","gitops","the custom gitops name") - createCmd.Flags().StringVar(&metaphorRepoName,"metaphorRepoName","metaphor","the custom metaphor name") - 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(&gitopsRepoName, "gitopsRepoName", "gitops", "the custom gitops name") + createCmd.Flags().StringVar(&metaphorRepoName, "metaphorRepoName", "metaphor", "the custom metaphor name") + 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(&gitProviderFlag, "git-provider", "github", fmt.Sprintf("the git provider - one of: %s", supportedGitProviders)) createCmd.Flags().StringVar(&gitProtocolFlag, "git-protocol", "ssh", fmt.Sprintf("the git protocol - one of: %s", supportedGitProtocolOverride)) createCmd.Flags().StringVar(&githubOrgFlag, "github-org", "", "the GitHub organization for the new gitops and metaphor repositories - required if using github") diff --git a/cmd/civo/create.go b/cmd/civo/create.go index cb3329f57..9f1d57ed3 100644 --- a/cmd/civo/create.go +++ b/cmd/civo/create.go @@ -59,8 +59,8 @@ func createCivo(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{cliFlags.GitopsRepoName, cliFlags.MetaphorRepoName} + newTeamNames := []string{cliFlags.AdminTeamName, cliFlags.DeveloperTeamName} initGitParameters := gitShim.GitInitParameters{ GitProvider: cliFlags.GitProvider, @@ -79,7 +79,7 @@ func createCivo(cmd *cobra.Command, args []string) error { viper.Set(fmt.Sprintf("kubefirst-checks.%s-credentials", cliFlags.GitProvider), true) viper.WriteConfig() - + k3dClusterCreationComplete := viper.GetBool("launch.deployed") isK1Debug := strings.ToLower(os.Getenv("K1_LOCAL_DEBUG")) == "true" diff --git a/cmd/digitalocean/command.go b/cmd/digitalocean/command.go index 1abb1f81a..811b5f525 100644 --- a/cmd/digitalocean/command.go +++ b/cmd/digitalocean/command.go @@ -32,10 +32,10 @@ var ( gitProtocolFlag string gitopsTemplateURLFlag string gitopsTemplateBranchFlag string - gitopsRepoName string - metaphorRepoName string - adminTeamName string - developerTeamName string + gitopsRepoName string + metaphorRepoName string + adminTeamName string + developerTeamName string useTelemetryFlag bool nodeTypeFlag string nodeCountFlag string @@ -108,10 +108,10 @@ func Create() *cobra.Command { createCmd.Flags().StringVar(&gitlabGroupFlag, "gitlab-group", "", "the GitLab group for the new gitops and metaphor projects - required if using gitlab") createCmd.Flags().StringVar(&gitopsTemplateBranchFlag, "gitops-template-branch", "", "the branch to clone for the gitops-template repository") 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(&gitopsRepoName,"gitopsRepoName","gitops","the custom gitops name") - createCmd.Flags().StringVar(&metaphorRepoName,"metaphorRepoName","metaphor","the custom metpahor name") - 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(&gitopsRepoName, "gitopsRepoName", "gitops", "the custom gitops name") + createCmd.Flags().StringVar(&metaphorRepoName, "metaphorRepoName", "metaphor", "the custom metpahor name") + 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().BoolVar(&useTelemetryFlag, "use-telemetry", true, "whether to emit telemetry") createCmd.Flags().BoolVar(&installKubefirstProFlag, "install-kubefirst-pro", true, "whether or not to install kubefirst pro") diff --git a/cmd/google/command.go b/cmd/google/command.go index 4f78f1057..193494d2c 100644 --- a/cmd/google/command.go +++ b/cmd/google/command.go @@ -32,10 +32,10 @@ var ( gitProtocolFlag string gitopsTemplateURLFlag string gitopsTemplateBranchFlag string - gitopsRepoName string - metaphorRepoName string - adminTeamName string - developerTeamName string + gitopsRepoName string + metaphorRepoName string + adminTeamName string + developerTeamName string useTelemetryFlag bool forceDestroyFlag bool nodeTypeFlag string @@ -112,10 +112,10 @@ func Create() *cobra.Command { createCmd.Flags().StringVar(&gitlabGroupFlag, "gitlab-group", "", "the GitLab group for the new gitops and metaphor projects - required if using gitlab") createCmd.Flags().StringVar(&gitopsTemplateBranchFlag, "gitops-template-branch", "", "the branch to clone for the gitops-template repository") 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(&gitopsRepoName,"gitopsRepoName","gitops","the custom gitops name") - createCmd.Flags().StringVar(&metaphorRepoName,"metaphorRepoName","metaphor","the custom metpahor name") - 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(&gitopsRepoName, "gitopsRepoName", "gitops", "the custom gitops name") + createCmd.Flags().StringVar(&metaphorRepoName, "metaphorRepoName", "metaphor", "the custom metpahor name") + 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().BoolVar(&useTelemetryFlag, "use-telemetry", true, "whether to emit telemetry") createCmd.Flags().BoolVar(&forceDestroyFlag, "force-destroy", false, "allows force destruction on objects (helpful for test environments, defaults to false)") diff --git a/cmd/k3d/command.go b/cmd/k3d/command.go index 7a1c3abc4..ddbd4ef38 100644 --- a/cmd/k3d/command.go +++ b/cmd/k3d/command.go @@ -30,8 +30,8 @@ var ( gitopsTemplateBranchFlag string useTelemetryFlag bool installCatalogApps string - gitopsRepoName string - metaphorRepoName string + gitopsRepoName string + metaphorRepoName string adminTeamName string developerTeamName string @@ -102,10 +102,10 @@ 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 of catalog apps to install after provision") createCmd.Flags().BoolVar(&useTelemetryFlag, "use-telemetry", true, "whether to emit telemetry") - createCmd.Flags().StringVar(&gitopsRepoName,"gitopsRepoName","gitops","the custom gitops name") - createCmd.Flags().StringVar(&metaphorRepoName,"metaphorRepoName","metaphor","the custom metaphor name") - 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(&gitopsRepoName, "gitopsRepoName", "gitops", "the custom gitops name") + createCmd.Flags().StringVar(&metaphorRepoName, "metaphorRepoName", "metaphor", "the custom metaphor name") + createCmd.Flags().StringVar(&adminTeamName, "adminTeamName", "admins", "admin team name for this repo") + createCmd.Flags().StringVar(&developerTeamName, "developerTeamName", "developers", "developer team name for this repo") return createCmd } diff --git a/cmd/k3d/create.go b/cmd/k3d/create.go index aff1368ad..1d1bc7e10 100644 --- a/cmd/k3d/create.go +++ b/cmd/k3d/create.go @@ -12,7 +12,6 @@ import ( "fmt" "net/http" "os" - "os/exec" "strconv" "strings" "time" @@ -344,7 +343,7 @@ func runK3d(cmd *cobra.Command, args []string) error { //} // Instantiate K3d config - config := k3d.GetConfig(clusterNameFlag, gitProviderFlag, cGitOwner, gitProtocolFlag, gitopsRepoName,metaphorRepoName,adminTeamName,developerTeamName) + config := k3d.GetConfig(clusterNameFlag, gitProviderFlag, cGitOwner, gitProtocolFlag, gitopsRepoName, metaphorRepoName, adminTeamName, developerTeamName) switch gitProviderFlag { case "github": config.GithubToken = cGitToken @@ -396,7 +395,7 @@ func runK3d(cmd *cobra.Command, args []string) error { log.Info().Msgf("kubefirst version configs.K1Version: %s ", configs.K1Version) log.Info().Msgf("cloning gitops-template repo url: %s ", gitopsTemplateURLFlag) log.Info().Msgf("cloning gitops-template repo branch: %s ", gitopsTemplateBranchFlag) - log.Info().Msgf("branch %s\b",gitopsTemplateBranchFlag) + log.Info().Msgf("branch %s\b", gitopsTemplateBranchFlag) atlantisWebhookSecret := viper.GetString("secrets.atlantis-webhook") if atlantisWebhookSecret == "" { atlantisWebhookSecret = utils.Random(20) @@ -556,7 +555,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,config.GitopsRepoName,config.MetaphorRepoName,config.AdminTeamName,config.DeveloperTeamName) + err := k3d.DownloadTools(clusterNameFlag, config.GitProvider, cGitOwner, config.ToolsDir, config.GitProtocol, config.GitopsRepoName, config.MetaphorRepoName, config.AdminTeamName, config.DeveloperTeamName) if err != nil { return err } @@ -572,7 +571,7 @@ func runK3d(cmd *cobra.Command, args []string) error { metaphorTemplateTokens := k3d.MetaphorTokenValues{ ClusterName: clusterNameFlag, CloudRegion: cloudRegionFlag, - ContainerRegistryURL: fmt.Sprintf("%s/%s/%s", containerRegistryHost, cGitOwner,config.MetaphorRepoName), + ContainerRegistryURL: fmt.Sprintf("%s/%s/%s", containerRegistryHost, cGitOwner, config.MetaphorRepoName), DomainName: k3d.DomainName, MetaphorDevelopmentIngressURL: fmt.Sprintf("metaphor-development.%s", k3d.DomainName), MetaphorStagingIngressURL: fmt.Sprintf("metaphor-staging.%s", k3d.DomainName), @@ -614,8 +613,8 @@ func runK3d(cmd *cobra.Command, args []string) error { if err != nil { return err } - viper.Set("adminTeamName",config.AdminTeamName) - viper.Set("developerTeamName",config.DeveloperTeamName) + viper.Set("adminTeamName", config.AdminTeamName) + viper.Set("developerTeamName", config.DeveloperTeamName) // todo emit init telemetry end viper.Set("kubefirst-checks.gitops-ready-to-push", true) @@ -1070,16 +1069,10 @@ func runK3d(cmd *cobra.Command, args []string) error { log.Info().Msg("applying the registry application to argocd") registryApplicationObject := argocd.GetArgoCDApplicationObject(gitopsRepoURL, fmt.Sprintf("registry/%s", clusterNameFlag)) - cmdStr := fmt.Sprintf("kubectl --kubeconfig=%s rollout restart -n argocd deploy/argocd-applicationset-controller", config.Kubeconfig) - - cmd := exec.Command("/bin/sh", "-c", cmdStr) - - err = cmd.Run() + err = k3d.Restart(config.Kubeconfig) if err != nil { - log.Info().Msgf("Error executing kubectl command: %v\n", err) - return err + return fmt.Errorf("Error in Restarting argocd controller %w", err) } - retryAttempts := 2 for attempt := 1; attempt <= retryAttempts; attempt++ { log.Info().Msgf("Attempt #%d to create Argo CD application...\n", attempt) @@ -1091,11 +1084,11 @@ func runK3d(cmd *cobra.Command, args []string) error { } log.Info().Msgf("Error creating Argo CD application on attempt number #%d: %v\n", attempt, err) time.Sleep(5 * time.Second) - continue + continue } log.Info().Msgf("Argo CD application created successfully on attempt #%d: %s\n", attempt, app.Name) - break + break } viper.Set("kubefirst-checks.argocd-create-registry", true) @@ -1203,7 +1196,7 @@ func runK3d(cmd *cobra.Command, args []string) error { //define upload object objectName := fmt.Sprintf("terraform/%s/terraform.tfstate", config.GitProvider) - filePath := config.K1Dir + fmt.Sprintf("/%s/%s", objectName,config.GitopsRepoName) + filePath := config.K1Dir + fmt.Sprintf("/%s/%s", objectName, config.GitopsRepoName) contentType := "xl.meta" bucketName := "kubefirst-state-store" log.Info().Msgf("BucketName: %s", bucketName) @@ -1472,4 +1465,4 @@ func runK3d(cmd *cobra.Command, args []string) error { } return nil -} \ No newline at end of file +} diff --git a/cmd/k3d/destroy.go b/cmd/k3d/destroy.go index b4593537a..3fdac3dd6 100644 --- a/cmd/k3d/destroy.go +++ b/cmd/k3d/destroy.go @@ -21,8 +21,8 @@ import ( "github.com/kubefirst/kubefirst-api/pkg/k8s" "github.com/kubefirst/kubefirst-api/pkg/progressPrinter" "github.com/kubefirst/kubefirst-api/pkg/terraform" - "github.com/kubefirst/kubefirst/internal/progress" "github.com/kubefirst/kubefirst/internal/common" + "github.com/kubefirst/kubefirst/internal/progress" "github.com/rs/zerolog/log" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -43,8 +43,8 @@ func destroyK3d(cmd *cobra.Command, args []string) error { // Check for existing port forwards before continuing - gitopsRepoName,metaphorRepoName := common.Getgitmeta(clusterName) - + gitopsRepoName, metaphorRepoName, _ := common.GetGitmeta(clusterName) + err := k8s.CheckForExistingPortForwards(9000) if err != nil { log.Error().Msgf("%s - this port is required to tear down your kubefirst environment - please close any existing port forwards before continuing", err.Error()) @@ -73,7 +73,7 @@ func destroyK3d(cmd *cobra.Command, args []string) error { } // Instantiate K3d config - config := k3d.GetConfig(clusterName, gitProvider, cGitOwner, gitProtocol,gitopsRepoName,metaphorRepoName,viper.GetString("adminTeamName"),viper.GetString("developerTeamName")) + config := k3d.GetConfig(clusterName, gitProvider, cGitOwner, gitProtocol, gitopsRepoName, metaphorRepoName, viper.GetString("adminTeamName"), viper.GetString("developerTeamName")) switch gitProvider { case "github": config.GithubToken = cGitToken @@ -234,7 +234,7 @@ func destroyK3d(cmd *cobra.Command, args []string) error { if !viper.GetBool(fmt.Sprintf("kubefirst-checks.terraform-apply-%s", gitProvider)) && !viper.GetBool("kubefirst-checks.create-k3d-cluster") { log.Info().Msg("removing previous platform content") - err := utils.ResetK1Dir(config.K1Dir,"gitops","metaphor") + err := utils.ResetK1Dir(config.K1Dir, "gitops", "metaphor") if err != nil { return err } @@ -262,4 +262,4 @@ func destroyK3d(cmd *cobra.Command, args []string) error { progress.Progress.Quit() return nil -} \ No newline at end of file +} diff --git a/cmd/k3d/mkcert.go b/cmd/k3d/mkcert.go index 4ecbfe6c3..34f49421c 100644 --- a/cmd/k3d/mkcert.go +++ b/cmd/k3d/mkcert.go @@ -12,8 +12,8 @@ import ( "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/progress" "github.com/kubefirst/kubefirst/internal/common" + "github.com/kubefirst/kubefirst/internal/progress" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -32,8 +32,8 @@ func mkCert(cmd *cobra.Command, args []string) error { if err != nil { return err } - - gitopsRepoName,metaphorRepoName := common.Getgitmeta(viper.GetString("flags.cluster-name")) + + gitopsRepoName, metaphorRepoName, _ := common.GetGitmeta(viper.GetString("flags.cluster-name")) flags := utils.GetClusterStatusFlags() if !flags.SetupComplete { return fmt.Errorf("there doesn't appear to be an active k3d cluster") @@ -45,7 +45,7 @@ func mkCert(cmd *cobra.Command, args []string) error { flags.GitProtocol, gitopsRepoName, metaphorRepoName, - viper.GetString("adminTeamName"), + viper.GetString("adminTeamName"), viper.GetString("developerTeamName"), ) kcfg := k8s.CreateKubeConfig(false, config.Kubeconfig) @@ -61,4 +61,4 @@ func mkCert(cmd *cobra.Command, args []string) error { progress.Progress.Quit() return nil -} \ No newline at end of file +} diff --git a/cmd/k3d/root-credentials.go b/cmd/k3d/root-credentials.go index 219c5b8bc..5bbc417f1 100644 --- a/cmd/k3d/root-credentials.go +++ b/cmd/k3d/root-credentials.go @@ -12,8 +12,8 @@ import ( "github.com/kubefirst/kubefirst-api/pkg/credentials" "github.com/kubefirst/kubefirst-api/pkg/k3d" "github.com/kubefirst/kubefirst-api/pkg/k8s" - "github.com/kubefirst/kubefirst/internal/progress" "github.com/kubefirst/kubefirst/internal/common" + "github.com/kubefirst/kubefirst/internal/progress" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -24,7 +24,7 @@ func getK3dRootCredentials(cmd *cobra.Command, args []string) error { gitProvider := viper.GetString("flags.git-provider") gitProtocol := viper.GetString("flags.git-protocol") gitOwner := viper.GetString(fmt.Sprintf("flags.%s-owner", gitProvider)) - gitopsRepoName,metaphorRepoName := common.Getgitmeta(clusterName) + gitopsRepoName, metaphorRepoName, _ := common.GetGitmeta(clusterName) // Parse flags a, err := cmd.Flags().GetBool("argocd") if err != nil { @@ -56,7 +56,7 @@ func getK3dRootCredentials(cmd *cobra.Command, args []string) error { } // Instantiate kubernetes client - config := k3d.GetConfig(clusterName, gitProvider, gitOwner, gitProtocol,gitopsRepoName,metaphorRepoName,viper.GetString("adminTeamName"),viper.GetString("developerTeamName")) + config := k3d.GetConfig(clusterName, gitProvider, gitOwner, gitProtocol, gitopsRepoName, metaphorRepoName, viper.GetString("adminTeamName"), viper.GetString("developerTeamName")) kcfg := k8s.CreateKubeConfig(false, config.Kubeconfig) diff --git a/cmd/k3d/vault.go b/cmd/k3d/vault.go index d8e38531c..ce5c4daad 100644 --- a/cmd/k3d/vault.go +++ b/cmd/k3d/vault.go @@ -17,8 +17,8 @@ import ( "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/progress" "github.com/kubefirst/kubefirst/internal/common" + "github.com/kubefirst/kubefirst/internal/progress" "github.com/rs/zerolog/log" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -40,7 +40,7 @@ func unsealVault(cmd *cobra.Command, args []string) error { if !flags.SetupComplete { return fmt.Errorf("there doesn't appear to be an active k3d cluster") } - gitopsRepoName,metaphorRepoName := common.Getgitmeta(viper.GetString("flags.cluster-name")) + gitopsRepoName, metaphorRepoName, _ := common.GetGitmeta(viper.GetString("flags.cluster-name")) config := k3d.GetConfig( viper.GetString("flags.cluster-name"), flags.GitProvider, diff --git a/cmd/k3s/command.go b/cmd/k3s/command.go index 2fbff95a2..f99b45c4c 100644 --- a/cmd/k3s/command.go +++ b/cmd/k3s/command.go @@ -37,10 +37,10 @@ var ( gitProtocolFlag string gitopsTemplateURLFlag string gitopsTemplateBranchFlag string - gitopsRepoName string - metaphorRepoName string - adminTeamName string - developerTeamName string + gitopsRepoName string + metaphorRepoName string + adminTeamName string + developerTeamName string installCatalogApps string useTelemetryFlag bool forceDestroyFlag bool @@ -109,10 +109,10 @@ func Create() *cobra.Command { createCmd.Flags().StringVar(&gitlabGroupFlag, "gitlab-group", "", "the GitLab group for the new gitops and metaphor projects - required if using gitlab") createCmd.Flags().StringVar(&gitopsTemplateBranchFlag, "gitops-template-branch", "", "the branch to clone for the gitops-template repository") 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(&gitopsRepoName,"gitopsRepoName","gitops","the custom gitops name") - createCmd.Flags().StringVar(&metaphorRepoName,"metaphorRepoName","metaphor","the custom metpahor name") - 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(&gitopsRepoName, "gitopsRepoName", "gitops", "the custom gitops name") + createCmd.Flags().StringVar(&metaphorRepoName, "metaphorRepoName", "metaphor", "the custom metpahor name") + 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().BoolVar(&useTelemetryFlag, "use-telemetry", true, "whether to emit telemetry") createCmd.Flags().BoolVar(&forceDestroyFlag, "force-destroy", false, "allows force destruction on objects (helpful for test environments, defaults to false)") diff --git a/cmd/reset.go b/cmd/reset.go index 8b9da7abd..0a3bedaa5 100755 --- a/cmd/reset.go +++ b/cmd/reset.go @@ -104,7 +104,7 @@ func runReset() error { } k1Dir := fmt.Sprintf("%s/.k1", homePath) - err = utils.ResetK1Dir(k1Dir,"gitops","metaphor") + err = utils.ResetK1Dir(k1Dir, "gitops", "metaphor") if err != nil { return err } diff --git a/cmd/vultr/command.go b/cmd/vultr/command.go index 70a2d7e9f..c98091eb6 100644 --- a/cmd/vultr/command.go +++ b/cmd/vultr/command.go @@ -31,10 +31,10 @@ var ( gitProtocolFlag string gitopsTemplateURLFlag string gitopsTemplateBranchFlag string - gitopsRepoName string - metaphorRepoName string - adminTeamName string - developerTeamName string + gitopsRepoName string + metaphorRepoName string + adminTeamName string + developerTeamName string useTelemetryFlag bool nodeTypeFlag string nodeCountFlag string @@ -108,10 +108,10 @@ func Create() *cobra.Command { createCmd.Flags().StringVar(&gitlabGroupFlag, "gitlab-group", "", "the GitLab group for the new gitops and metaphor projects - required if using gitlab") createCmd.Flags().StringVar(&gitopsTemplateBranchFlag, "gitops-template-branch", "", "the branch to clone for the gitops-template repository") 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(&gitopsRepoName,"gitopsRepoName","gitops","the custom gitops name") - createCmd.Flags().StringVar(&metaphorRepoName,"metaphorRepoName","metaphor","the custom metpahor name") - 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(&gitopsRepoName, "gitopsRepoName", "gitops", "the custom gitops name") + createCmd.Flags().StringVar(&metaphorRepoName, "metaphorRepoName", "metaphor", "the custom metpahor name") + 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().BoolVar(&useTelemetryFlag, "use-telemetry", true, "whether to emit telemetry") createCmd.Flags().BoolVar(&installKubefirstProFlag, "install-kubefirst-pro", true, "whether or not to install kubefirst pro") diff --git a/internal/common/common.go b/internal/common/common.go index bb3da60ae..22f44bf1c 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -9,12 +9,13 @@ package common import ( "fmt" "io" + "io/fs" "net/http" "os" + "path/filepath" "regexp" "runtime" "strings" - "io/ioutil" "github.com/kubefirst/kubefirst-api/pkg/configs" "github.com/kubefirst/kubefirst-api/pkg/docker" @@ -114,53 +115,63 @@ func GetRootCredentials(cmd *cobra.Command, args []string) error { return nil } -func Getgitmeta(clusterName string) (gitopsRepoName string, metaphorRepoName string) { +func GetGitmeta(clusterName string) (string, string, error) { + var gitopsFound, metaphorFound bool - homePath,err := os.UserHomeDir() - dirs, err := ioutil.ReadDir(fmt.Sprintf("%s/.k1/%s", homePath, clusterName)) + var gitopsRepoName, metaphorRepoName string + + homePath, err := os.UserHomeDir() if err != nil { - log.Info().Msg("Error reading directory") - return "cantfindgit","cantfindmeta" + return "", "", fmt.Errorf("Error in homePath %w", err) } - for _, direc := range dirs { - if direc.IsDir() { - parentdir, err := ioutil.ReadDir(fmt.Sprintf("%s/.k1/%s/%s", homePath, clusterName, direc.Name())) - if err != nil { - log.Printf("Error reading directory %s: %v", direc.Name(), err) - continue - } + basePath := filepath.Join(homePath, ".k1", clusterName) + + err = filepath.WalkDir(basePath, func(path string, info fs.DirEntry, err error) error { + if err != nil { + return err + } - for _, dir := range parentdir { - if dir.IsDir() { - if dir.Name() == "registry" { - gitopsRepoName = direc.Name() + if info.IsDir() { + relPath, _ := filepath.Rel(basePath, path) + if relPath == "." || strings.Count(relPath, string(os.PathSeparator)) == 1 { + if info.Name() == "registry" { + if !gitopsFound { + gitopsRepoName = filepath.Dir(relPath) gitopsFound = true - } else - if dir.Name() == ".github" { - metaphorRepoName = direc.Name() + } + } + 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 "cantfindgit","cantfindmeta" + return "", "", fmt.Errorf("Gitopsrepo Not Found") } if !metaphorFound { log.Info().Msg("Metaphor Repo not found") - os.Exit(1) + return "", "", fmt.Errorf("MetaphorRepo Not Found") } - return gitopsRepoName, metaphorRepoName + return gitopsRepoName, metaphorRepoName, nil } func Destroy(cmd *cobra.Command, args []string) error { @@ -168,14 +179,17 @@ func Destroy(cmd *cobra.Command, args []string) error { gitProvider := viper.GetString("flags.git-provider") gitProtocol := viper.GetString("flags.git-protocol") cloudProvider := viper.GetString("kubefirst.cloud-provider") - + log.Info().Msg("destroying kubefirst platform") clusterName := viper.GetString("flags.cluster-name") domainName := viper.GetString("flags.domain-name") - gitopsRepoName,metaphorRepoName := Getgitmeta(clusterName) + gitopsRepoName, metaphorRepoName, err := GetGitmeta(clusterName) + if err != nil { + return err + } // Switch based on git provider, set params cGitOwner := "" switch gitProvider { diff --git a/internal/k3d/menu.go b/internal/k3d/menu.go index e549e2774..9d145a497 100644 --- a/internal/k3d/menu.go +++ b/internal/k3d/menu.go @@ -141,5 +141,3 @@ func MongoDestinationChooser(inCluster bool) string { } return result } - - diff --git a/internal/types/flags.go b/internal/types/flags.go index ed4beb988..e0835a2db 100644 --- a/internal/types/flags.go +++ b/internal/types/flags.go @@ -20,9 +20,9 @@ type CliFlags struct { GitProtocol string GithubOrg string GitlabGroup string - GitopsRepoName string + GitopsRepoName string MetaphorRepoName string - AdminTeamName string + AdminTeamName string DeveloperTeamName string GitopsTemplateBranch string GitopsTemplateURL string diff --git a/internal/utilities/flags.go b/internal/utilities/flags.go index b21f7536e..3c3c33855 100644 --- a/internal/utilities/flags.go +++ b/internal/utilities/flags.go @@ -83,25 +83,25 @@ func GetFlags(cmd *cobra.Command, cloudProvider string) (types.CliFlags, error) gitopsRepoNameFlag, err := cmd.Flags().GetString("gitopsRepoName") if err != nil { progress.Error(err.Error()) - return cliFlags,err + return cliFlags, err } metaphorRepoNameFlag, err := cmd.Flags().GetString("metaphorRepoName") if err != nil { progress.Error(err.Error()) - return cliFlags,err + return cliFlags, err } adminTeamNameFlag, err := cmd.Flags().GetString("adminTeamName") if err != nil { progress.Error(err.Error()) - return cliFlags,err + return cliFlags, err } developerTeamNameFlag, err := cmd.Flags().GetString("developerTeamName") if err != nil { progress.Error(err.Error()) - return cliFlags,err + return cliFlags, err } gitopsTemplateURLFlag, err := cmd.Flags().GetString("gitops-template-url") diff --git a/internal/utilities/utilities.go b/internal/utilities/utilities.go index 27b50d626..1a1e41182 100644 --- a/internal/utilities/utilities.go +++ b/internal/utilities/utilities.go @@ -158,10 +158,10 @@ func CreateClusterDefinitionRecordFromRaw(gitAuth apiTypes.GitAuth, cliFlags typ NodeCount: stringToIntNodeCount, GitopsTemplateURL: cliFlags.GitopsTemplateURL, GitopsTemplateBranch: cliFlags.GitopsTemplateBranch, - GitopsRepoName: cliFlags.GitopsRepoName, - MetaphorRepoName: cliFlags.MetaphorRepoName, - AdminTeamName: cliFlags.AdminTeamName, - DeveloperTeamName: cliFlags.DeveloperTeamName, + GitopsRepoName: cliFlags.GitopsRepoName, + MetaphorRepoName: cliFlags.MetaphorRepoName, + AdminTeamName: cliFlags.AdminTeamName, + DeveloperTeamName: cliFlags.DeveloperTeamName, GitProvider: gitProvider, GitProtocol: viper.GetString("flags.git-protocol"), DnsProvider: viper.GetString("flags.dns-provider"), diff --git a/tools/aws-assume-role.sh b/tools/aws-assume-role.sh index deaa9d10d..ee7478a07 100755 --- a/tools/aws-assume-role.sh +++ b/tools/aws-assume-role.sh @@ -19,7 +19,7 @@ # # Change the AWS account ID & role name # -ROLE="arn:aws:sts::126827061464:assumed-role/KubernetesAdmin/rishi-wins" +ROLE="arn:aws:iam::111111111111:role/AssumedAdmin" # # You can leave the rest of thre script as is