From ffbc824a197959521863b344c30cd967ca172e00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Paulo=20Vanzuita?= Date: Thu, 13 Oct 2022 08:30:07 -0300 Subject: [PATCH] fix: close/open port forward before connecting to gitlab and vault to call terraform users apply (#556) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: close port forward before checking liveness Signed-off-by: João Vanzuita --- cmd/createGithub.go | 2 +- cmd/createGitlab.go | 49 ++++++++++++++++++++++++++++++++++++--------- cmd/destroy.go | 2 +- 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/cmd/createGithub.go b/cmd/createGithub.go index b4465e41d..0a21fdd6f 100644 --- a/cmd/createGithub.go +++ b/cmd/createGithub.go @@ -45,7 +45,7 @@ var createGithubCmd = &cobra.Command{ var kPortForwardArgocd *exec.Cmd progressPrinter.AddTracker("step-0", "Process Parameters", 1) progressPrinter.AddTracker("step-github", "Setup gitops on github", 3) - progressPrinter.AddTracker("step-base", "Setup base cluster", 2) + progressPrinter.AddTracker("step-base", "Setup base cluster", 3) progressPrinter.AddTracker("step-apps", "Install apps to cluster", 6) progressPrinter.IncrementTracker("step-0", 1) diff --git a/cmd/createGitlab.go b/cmd/createGitlab.go index 557e0aaa3..072f93aa5 100644 --- a/cmd/createGitlab.go +++ b/cmd/createGitlab.go @@ -18,6 +18,7 @@ import ( "github.com/spf13/viper" "log" "net/http" + "os" "os/exec" "syscall" "time" @@ -178,9 +179,10 @@ var createGitlabCmd = &cobra.Command{ informUser("Waiting vault to be ready", globalFlags.SilentMode) waitVaultToBeRunning(globalFlags.DryRun) progressPrinter.IncrementTracker("step-gitlab", 1) + + var kPortForwardVault *exec.Cmd if !globalFlags.DryRun { - kPortForwardVault, err := k8s.PortForward(globalFlags.DryRun, "vault", "svc/vault", "8200:8200") - defer kPortForwardVault.Process.Signal(syscall.SIGTERM) + kPortForwardVault, err = k8s.PortForward(globalFlags.DryRun, "vault", "svc/vault", "8200:8200") if err != nil { log.Println("Error creating port-forward") return err @@ -199,9 +201,9 @@ var createGitlabCmd = &cobra.Command{ log.Println("gitlab is ready!") progressPrinter.IncrementTracker("step-gitlab", 1) + var kPortForwardGitlab *exec.Cmd if !globalFlags.DryRun { - kPortForwardGitlab, err := k8s.PortForward(globalFlags.DryRun, "gitlab", "svc/gitlab-webservice-default", "8888:8080") - defer kPortForwardGitlab.Process.Signal(syscall.SIGTERM) + kPortForwardGitlab, err = k8s.PortForward(globalFlags.DryRun, "gitlab", "svc/gitlab-webservice-default", "8888:8080") if err != nil { log.Println("Error creating port-forward") return err @@ -219,6 +221,7 @@ var createGitlabCmd = &cobra.Command{ informUser("Gitlab terraform", globalFlags.SilentMode) gitlab.ApplyGitlabTerraform(globalFlags.DryRun, directory) gitlab.GitlabKeyUpload(globalFlags.DryRun) + informUser("Gitlab ready", globalFlags.SilentMode) progressPrinter.IncrementTracker("step-gitlab", 1) } @@ -362,14 +365,40 @@ var createGitlabCmd = &cobra.Command{ progressPrinter.IncrementTracker("step-vault-be", 1) } - // enable GitLab port forward connection for Terraform - if !globalFlags.DryRun { - kPortForwardGitlab, err := k8s.PortForward(globalFlags.DryRun, "gitlab", "svc/gitlab-webservice-default", "8888:8080") - defer kPortForwardGitlab.Process.Signal(syscall.SIGTERM) + // force close port forward, Vault port forward is not available at this point anymore + if kPortForwardVault != nil { + err = kPortForwardVault.Process.Signal(syscall.SIGTERM) if err != nil { - log.Println("Error creating port-forward") - return err + 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) } + }(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) + } + } + + 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) + } + }(kPortForwardGitlab.Process, syscall.SIGTERM) + if err != nil { + log.Println("Error creating port-forward") + return err } // manage users via Terraform diff --git a/cmd/destroy.go b/cmd/destroy.go index d57d4d78e..6b7c94bcd 100644 --- a/cmd/destroy.go +++ b/cmd/destroy.go @@ -63,7 +63,7 @@ var destroyCmd = &cobra.Command{ kPortForward, _ := k8s.PortForward(globalFlags.DryRun, "gitlab", "svc/gitlab-webservice-default", "8888:8080") defer func() { if kPortForward != nil { - log.Println("Closed argo port forward") + log.Println("Closed GitLab port forward") _ = kPortForward.Process.Signal(syscall.SIGTERM) } }()