From a9db76b3f2307a049dab370fab6fc67a2a65419e Mon Sep 17 00:00:00 2001 From: Jessica Marinho Date: Wed, 19 Oct 2022 18:45:11 -0300 Subject: [PATCH 1/2] Active github flavor on gitlab Signed-off-by: Jessica Marinho --- internal/ciTools/ci.go | 40 +++++++++++++++++++++++++++++------ internal/ciTools/terraform.go | 39 ++++++++++++++++++++++++++++++++++ internal/flagset/ci.go | 40 ++++++++++++++++++++++++++++++----- 3 files changed, 108 insertions(+), 11 deletions(-) diff --git a/internal/ciTools/ci.go b/internal/ciTools/ci.go index 0535b2770..bb04b5db7 100644 --- a/internal/ciTools/ci.go +++ b/internal/ciTools/ci.go @@ -24,13 +24,29 @@ func DeployOnGitlab(globalFlags flagset.GlobalFlags, bucketName string) error { repo.PrepareKubefirstTemplateRepo(globalFlags.DryRun, config, viper.GetString("gitops.owner"), "ci", viper.GetString("ci.branch"), viper.GetString("template.tag")) log.Println("clone and detokenization of ci-template repository complete") - err := SedBucketName("", bucketName) + secretProviderFile := fmt.Sprintf("%s/ci/terraform/secret/provider.tf", config.K1FolderPath) + baseProviderFile := fmt.Sprintf("%s/ci/terraform/base/provider.tf", config.K1FolderPath) + + err := SedBucketName("", bucketName, secretProviderFile) if err != nil { log.Panicf("Error sed bucket name on CI repository: %s", err) return err } - ciLocation := fmt.Sprintf("%s/ci/components/argo-gitlab/ci.yaml", config.K1FolderPath) + err = SedBucketName("", bucketName, baseProviderFile) + if err != nil { + log.Panicf("Error sed bucket name on CI repository: %s", err) + return err + } + + ciLocation := "" + workflowLocation := fmt.Sprintf("%s/ci/.gitlab-ci.yml", config.K1FolderPath) + + if viper.GetString("ci.flavor") == "github" { + ciLocation = fmt.Sprintf("%s/ci/components/argo-github/ci.yaml", config.K1FolderPath) + } else { + ciLocation = fmt.Sprintf("%s/ci/components/argo-gitlab/ci.yaml", config.K1FolderPath) + } err = DetokenizeCI("", viper.GetString("ci.cluster.name"), ciLocation) if err != nil { @@ -44,6 +60,21 @@ func DeployOnGitlab(globalFlags flagset.GlobalFlags, bucketName string) error { if err != nil { log.Println(err) } + err = DetokenizeCI("", viper.GetString("ci.flavor"), workflowLocation) + if err != nil { + log.Println(err) + } + + if viper.GetString("ci.flavor") == "github" { + err = DetokenizeCI("", viper.GetString("ci.github.user"), ciLocation) + if err != nil { + log.Println(err) + } + err = DetokenizeCI("", viper.GetString("ci.github.organization"), ciLocation) + if err != nil { + log.Println(err) + } + } if !viper.GetBool("gitlab.ci-pushed") { log.Println("Pushing ci repo to origin gitlab") @@ -56,10 +87,7 @@ func DeployOnGitlab(globalFlags flagset.GlobalFlags, bucketName string) error { return nil } -func SedBucketName(old, new string) error { - cfg := configs.ReadConfig() - providerFile := fmt.Sprintf("%s/ci/terraform/base/provider.tf", cfg.K1FolderPath) - +func SedBucketName(old string, new string, providerFile string) error { fileData, err := os.ReadFile(providerFile) if err != nil { return err diff --git a/internal/ciTools/terraform.go b/internal/ciTools/terraform.go index 58d631b65..67bce27be 100644 --- a/internal/ciTools/terraform.go +++ b/internal/ciTools/terraform.go @@ -45,6 +45,25 @@ func ApplyCITerraform(dryRun bool, bucketName string) { } os.RemoveAll(fmt.Sprintf("%s/.terraform", directory)) + if viper.GetString("ci.flavor") == "github" { + envs["TF_VAR_github_token"] = os.Getenv("GITHUB_AUTH_TOKEN") + secretDirectory := fmt.Sprintf("%s/ci/terraform/secret", config.K1FolderPath) + err := os.Chdir(secretDirectory) + if err != nil { + log.Panic("error: could not change directory to " + secretDirectory) + } + err = pkg.ExecShellWithVars(envs, config.TerraformPath, "init") + if err != nil { + log.Panicf("error: terraform init for ci secret failed %s", err) + } + + err = pkg.ExecShellWithVars(envs, config.TerraformPath, "apply", "-auto-approve") + if err != nil { + log.Panicf("error: terraform apply for ci secret failed %s", err) + } + os.RemoveAll(fmt.Sprintf("%s/.terraform", secretDirectory)) + } + } else { log.Println("Skipping: applyCITerraform") } @@ -72,6 +91,26 @@ func DestroyCITerraform(skipCITerraform bool) { if err != nil { log.Printf("[WARN]: failed to terraform destroy CI, was the CI not created (check AWS)?: %s", err) } + + if viper.GetString("ci.flavor") == "github" { + envs["TF_VAR_github_token"] = os.Getenv("GITHUB_AUTH_TOKEN") + secretDirectory := fmt.Sprintf("%s/ci/terraform/secret", config.K1FolderPath) + err := os.Chdir(secretDirectory) + if err != nil { + log.Panic("error: could not change directory to " + secretDirectory) + } + err = pkg.ExecShellWithVars(envs, config.TerraformPath, "init") + if err != nil { + log.Panicf("error: terraform init for ci secret failed %s", err) + } + + err = pkg.ExecShellWithVars(envs, config.TerraformPath, "destroy", "-auto-approve") + if err != nil { + log.Panicf("error: terraform destroy for ci secret failed %s", err) + } + os.RemoveAll(fmt.Sprintf("%s/.terraform", secretDirectory)) + } + viper.Set("gitlab.ci-pushed", false) viper.WriteConfig() } else { diff --git a/internal/flagset/ci.go b/internal/flagset/ci.go index 4870c4aa3..6e059c617 100644 --- a/internal/flagset/ci.go +++ b/internal/flagset/ci.go @@ -9,11 +9,14 @@ import ( // CIFlags - Global flags type CIFlags struct { - BranchCI string - DestroyBucket bool - CIClusterName string - CIS3Suffix string - CIHostedZoneName string + BranchCI string + DestroyBucket bool + CIClusterName string + CIS3Suffix string + CIHostedZoneName string + CIFlavor string + CIGithubUser string + CIGithubOrganization string } // DefineCIFlags - Define global flags @@ -23,6 +26,9 @@ func DefineCIFlags(currentCommand *cobra.Command) { currentCommand.Flags().String("ci-cluster-name", "", "the ci cluster name, used to identify resources on cloud provider") currentCommand.Flags().String("ci-s3-suffix", "", "unique identifier for s3 buckets") currentCommand.Flags().String("ci-hosted-zone-name", "", "the ci domain to provision the kubefirst platform in") + currentCommand.Flags().String("ci-flavor", "", "inform which flavor will be installed") + currentCommand.Flags().String("ci-github-user", "", "inform which github user will be used") + currentCommand.Flags().String("ci-github-organization", "", "inform which github organization will be used") } // ProcessCIFlags - process global flags shared between commands like silent, dry-run and use-telemetry @@ -69,6 +75,30 @@ func ProcessCIFlags(cmd *cobra.Command) (CIFlags, error) { flags.CIHostedZoneName = ciHostedZoneName viper.Set("ci.hosted.zone.name", ciHostedZoneName) + ciFlavor, err := ReadConfigString(cmd, "ci-flavor") + if err != nil { + log.Printf("Error Processing - ci-flavor flag, error: %v", err) + return flags, err + } + flags.CIFlavor = ciFlavor + viper.Set("ci.flavor", ciFlavor) + + ciGithubUser, err := ReadConfigString(cmd, "ci-github-user") + if err != nil { + log.Printf("Error Processing - ci-github-user flag, error: %v", err) + return flags, err + } + flags.CIGithubUser = ciGithubUser + viper.Set("ci.github.user", ciGithubUser) + + ciGithubOrganization, err := ReadConfigString(cmd, "ci-github-organization") + if err != nil { + log.Printf("Error Processing - ci-github-organization flag, error: %v", err) + return flags, err + } + flags.CIGithubOrganization = ciGithubOrganization + viper.Set("ci.github.organization", ciGithubOrganization) + return flags, nil } From 953fe1f9f6340f7bbb2c2149d46e165b6a0f6dc9 Mon Sep 17 00:00:00 2001 From: Cesar Filho <53096417+6za@users.noreply.github.com> Date: Mon, 7 Nov 2022 10:30:35 -0300 Subject: [PATCH 2/2] Issue 638 (#667) * fix flag reading Signed-off-by: 6za <53096417+6za@users.noreply.github.com> * fix install Signed-off-by: 6za <53096417+6za@users.noreply.github.com> Signed-off-by: 6za <53096417+6za@users.noreply.github.com> --- cmd/postInstall.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cmd/postInstall.go b/cmd/postInstall.go index 4f5123f83..eedcc322c 100644 --- a/cmd/postInstall.go +++ b/cmd/postInstall.go @@ -1,10 +1,11 @@ package cmd import ( - "github.com/kubefirst/kubefirst/internal/k8s" "log" "time" + "github.com/kubefirst/kubefirst/internal/k8s" + "github.com/kubefirst/kubefirst/internal/flagset" "github.com/kubefirst/kubefirst/internal/reports" @@ -56,7 +57,7 @@ var postInstallCmd = &cobra.Command{ err := pkg.OpenBrowser(pkg.LocalConsoleUI) if err != nil { - return err + log.Println(err) } } else { @@ -75,8 +76,8 @@ var postInstallCmd = &cobra.Command{ log.Println(err) } err = pkg.OpenBrowser(pkg.LocalConsoleUI) - if err != nil { - return err + if pkg.OpenBrowser(pkg.LocalConsoleUI) != nil { + log.Println(err) } }