From e63e3378da97dfdb2a4d591d333afede5adbd2fe Mon Sep 17 00:00:00 2001 From: Cesar Filho <53096417+6za@users.noreply.github.com> Date: Thu, 13 Oct 2022 16:06:39 -0300 Subject: [PATCH] fix #514 (#561) Signed-off-by: 6za <53096417+6za@users.noreply.github.com> Signed-off-by: 6za <53096417+6za@users.noreply.github.com> --- cmd/createGitlab.go | 97 +++++++++++++++++++++++++++------------------ 1 file changed, 58 insertions(+), 39 deletions(-) diff --git a/cmd/createGitlab.go b/cmd/createGitlab.go index 072f93aa5..2d5de7099 100644 --- a/cmd/createGitlab.go +++ b/cmd/createGitlab.go @@ -3,6 +3,12 @@ package cmd import ( "crypto/tls" "fmt" + "log" + "net/http" + "os/exec" + "syscall" + "time" + "github.com/kubefirst/kubefirst/configs" "github.com/kubefirst/kubefirst/internal/argocd" "github.com/kubefirst/kubefirst/internal/flagset" @@ -16,12 +22,6 @@ import ( "github.com/kubefirst/kubefirst/pkg" "github.com/spf13/cobra" "github.com/spf13/viper" - "log" - "net/http" - "os" - "os/exec" - "syscall" - "time" ) // createGitlabCmd represents the createGitlab command @@ -372,42 +372,61 @@ var createGitlabCmd = &cobra.Command{ log.Println(err) } } - - kPortForwardVault, err = k8s.PortForward(globalFlags.DryRun, "vault", "svc/vault", "8200:8200") - defer func(Process *os.Process, sig os.Signal) { - err := Process.Signal(sig) - if err != nil { - log.Println(err) + // enable GitLab port forward connection for Terraform + if !globalFlags.DryRun { + // force close port forward, GitLab port forward is not available at this point anymore + //Close PF before creating new ones in the loop + if kPortForwardGitlab != nil { + err = kPortForwardGitlab.Process.Signal(syscall.SIGTERM) + if err != nil { + log.Println(err) + } } - }(kPortForwardVault.Process, syscall.SIGTERM) - - // force close port forward, GitLab port forward is not available at this point anymore - if kPortForwardGitlab != nil { - err = kPortForwardGitlab.Process.Signal(syscall.SIGTERM) - if err != nil { - log.Println(err) + if kPortForwardVault != nil { + err = kPortForwardVault.Process.Signal(syscall.SIGTERM) + if err != nil { + log.Println(err) + } } - } - - kPortForwardGitlab, err = k8s.PortForward(globalFlags.DryRun, "gitlab", "svc/gitlab-webservice-default", "8888:8080") - defer func(Process *os.Process, sig os.Signal) { - err := Process.Signal(sig) - if err != nil { - log.Println(err) + if !viper.GetBool("create.terraformapplied.users") { + for i := 0; i < totalAttempts; i++ { + kPortForwardVault, err = k8s.PortForward(globalFlags.DryRun, "vault", "svc/vault", "8200:8200") + defer func() { + _ = kPortForwardVault.Process.Signal(syscall.SIGTERM) + }() + kPortForwardGitlab, err = k8s.PortForward(globalFlags.DryRun, "gitlab", "svc/gitlab-webservice-default", "8888:8080") + defer func() { + _ = kPortForwardGitlab.Process.Signal(syscall.SIGTERM) + }() + if err != nil { + log.Println("Error creating port-forward") + return err + } + //Wait the universe to sort itself out before creating more changes + time.Sleep(20 * time.Second) + + // manage users via Terraform + directory = fmt.Sprintf("%s/gitops/terraform/users", config.K1FolderPath) + informUser("applying users terraform", globalFlags.SilentMode) + gitProvider := viper.GetString("git.mode") + err = terraform.ApplyUsersTerraform(globalFlags.DryRun, directory, gitProvider) + if err != nil { + log.Println("Error applying users") + log.Println(err) + } + if viper.GetBool("create.terraformapplied.users") || err == nil { + log.Println("Users configured") + break + } else { + log.Println("Users not configured - waiting before trying again") + time.Sleep(20 * time.Second) + _ = kPortForwardGitlab.Process.Signal(syscall.SIGTERM) + _ = kPortForwardVault.Process.Signal(syscall.SIGTERM) + } + } + } else { + log.Println("Skipped - Users configured") } - }(kPortForwardGitlab.Process, syscall.SIGTERM) - if err != nil { - log.Println("Error creating port-forward") - return err - } - - // manage users via Terraform - directory = fmt.Sprintf("%s/gitops/terraform/users", config.K1FolderPath) - informUser("applying users terraform", globalFlags.SilentMode) - gitProvider := viper.GetString("git.mode") - err = terraform.ApplyUsersTerraform(globalFlags.DryRun, directory, gitProvider) - if err != nil { - return err } return nil