Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: port forward using go-client #669

Merged
merged 8 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 40 additions & 11 deletions cmd/local/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,75 @@ import (
"syscall"
)

// NewCommandConnect open local port to enable connection between your local computer, to the cluster applications.
func NewCommandConnect() *cobra.Command {
connectCmd := &cobra.Command{
Use: "connect",
Short: "local connect command enable port forwards",
Long: `local connect command enable local tunnels to enable local connection to the cluster application. Check
logs for details.`,
Short: "connect will open all Kubefirst services port forwards",
Long: "connect opens all Kubefirst service ports for local connection, it makes the services available to" +
"allow local request to the deployed services",
RunE: runConnect,
}

return connectCmd
}

// runConnect opens port forwards for the available Kubefirst applications.
func runConnect(cmd *cobra.Command, args []string) error {

log.Println("opening Port Forward for console...")

err := k8s.OpenPortForwardForKubeConConsole()
// every port forward has its own closing control. when a channel is closed, the port forward is close.
vaultStopChannel := make(chan struct{}, 1)
argoStopChannel := make(chan struct{}, 1)
argoCDStopChannel := make(chan struct{}, 1)
chartmuseumStopChannel := make(chan struct{}, 1)
minioStopChannel := make(chan struct{}, 1)
minioConsoleStopChannel := make(chan struct{}, 1)
kubefirstConsoleStopChannel := make(chan struct{}, 1)
AtlantisStopChannel := make(chan struct{}, 1)

// guarantee it will close the port forwards even on a process kill
defer func() {
close(vaultStopChannel)
close(argoStopChannel)
close(argoCDStopChannel)
close(chartmuseumStopChannel)
close(minioStopChannel)
close(minioConsoleStopChannel)
close(kubefirstConsoleStopChannel)
close(AtlantisStopChannel)
log.Println("leaving port-forward command, port forwards are now closed")
}()

err := k8s.OpenPortForwardForLocal(
vaultStopChannel,
argoStopChannel,
argoCDStopChannel,
chartmuseumStopChannel,
minioStopChannel,
minioConsoleStopChannel,
kubefirstConsoleStopChannel,
AtlantisStopChannel,
)
if err != nil {
return err
}

// style UI with local URLs
fmt.Println(reports.StyleMessage(reports.LocalConnectSummary()))

log.Println("Kubefirst port forward done")
log.Println("hanging port forwards until ctrl+c is called")

// managing termination signal from the terminal
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
var wg sync.WaitGroup
wg.Add(1)
go func() {
<-sigs
log.Println("leaving port-forward command, port forwards are now closed")
wg.Done()
}()
wg.Wait()

log.Println("Kubefirst port forward done")
log.Println("hanging port forwards until ctrl+c is called")
wg.Wait()

return nil
}
97 changes: 60 additions & 37 deletions cmd/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"context"
"fmt"
"log"
"os/exec"
"sync"
"syscall"
"time"

"github.com/go-git/go-git/v5/plumbing"
Expand Down Expand Up @@ -73,6 +71,9 @@ func NewCommand() *cobra.Command {
// on error, doesnt show helper/usage
localCmd.SilenceUsage = true

// wire up new commands
localCmd.AddCommand(NewCommandConnect())

return localCmd
}

Expand Down Expand Up @@ -143,7 +144,6 @@ func runLocal(cmd *cobra.Command, args []string) error {
viper.WriteConfig()
}

var kPortForwardArgocd *exec.Cmd
progressPrinter.SetupProgress(progressPrinter.TotalOfTrackers(), silentMode)

executionControl := viper.GetBool("terraform.github.apply.complete")
Expand Down Expand Up @@ -249,14 +249,18 @@ func runLocal(cmd *cobra.Command, args []string) error {
log.Println("already waited for argocd to be ready")
}

// establish port-forward
kPortForwardArgocd, err := k8s.PortForward(dryRun, "argocd", "svc/argocd-server", "8080:80")
// ArgoCD port-forward
argoCDStopChannel := make(chan struct{}, 1)
defer func() {
err = kPortForwardArgocd.Process.Signal(syscall.SIGTERM)
if err != nil {
log.Println("Error closing kPortForwardArgocd")
}
close(argoCDStopChannel)
}()
k8s.OpenPortForwardWrapper(
pkg.ArgoCDPodName,
pkg.ArgoCDNamespace,
pkg.ArgoCDPodPort,
pkg.ArgoCDPodLocalPort,
argoCDStopChannel,
)
pkg.InformUser(fmt.Sprintf("port-forward to argocd is available at %s", viper.GetString("argocd.local.service")), silentMode)

// argocd pods are ready, get and set credentials
Expand All @@ -278,7 +282,7 @@ func runLocal(cmd *cobra.Command, args []string) error {
executionControl = viper.GetBool("argocd.registry.applied")
if !executionControl {
pkg.InformUser("applying the registry application to argocd", silentMode)
err = argocd.ApplyRegistryLocal(dryRun)
err := argocd.ApplyRegistryLocal(dryRun)
if err != nil {
log.Println("Error applying registry application to argocd")
return err
Expand All @@ -292,28 +296,36 @@ func runLocal(cmd *cobra.Command, args []string) error {
if !executionControl {
pkg.InformUser("Waiting for vault to be ready", silentMode)
vault.WaitVaultToBeRunning(dryRun)
if err != nil {
log.Println("error waiting for vault to become running")
return err
}
}
kPortForwardVault, err := k8s.PortForward(dryRun, "vault", "svc/vault", "8200:8200")

// Vault port-forward
vaultStopChannel := make(chan struct{}, 1)
defer func() {
err = kPortForwardVault.Process.Signal(syscall.SIGTERM)
if err != nil {
log.Println("Error closing kPortForwardVault")
}
close(vaultStopChannel)
}()
k8s.OpenPortForwardWrapper(
pkg.VaultPodName,
pkg.VaultNamespace,
pkg.VaultPodPort,
pkg.VaultPodLocalPort,
vaultStopChannel,
)

k8s.LoopUntilPodIsReady(dryRun)
kPortForwardMinio, err := k8s.PortForward(dryRun, "minio", "svc/minio", "9000:9000")

minioStopChannel := make(chan struct{}, 1)
defer func() {
err = kPortForwardMinio.Process.Signal(syscall.SIGTERM)
if err != nil {
log.Println("Error closing kPortForwardMinio")
}
close(minioStopChannel)
}()
k8s.OpenPortForwardWrapper(
pkg.MinioPodName,
pkg.MinioNamespace,
pkg.MinioPodPort,
pkg.MinioPodLocalPort,
minioStopChannel,
)

// todo: can I remove it?
time.Sleep(20 * time.Second)

// configure vault with terraform
Expand Down Expand Up @@ -370,16 +382,19 @@ func runLocal(cmd *cobra.Command, args []string) error {
progressPrinter.IncrementTracker("step-apps", 1)

if !viper.GetBool("chartmuseum.host.resolved") {

//* establish port-forward
var kPortForwardChartMuseum *exec.Cmd
kPortForwardChartMuseum, err = k8s.PortForward(dryRun, "chartmuseum", "svc/chartmuseum", "8181:8080")
// Chartmuseum port-forward
chartmuseumStopChannel := make(chan struct{}, 1)
defer func() {
err = kPortForwardChartMuseum.Process.Signal(syscall.SIGTERM)
if err != nil {
log.Println("Error closing kPortForwardChartMuseum")
}
close(chartmuseumStopChannel)
}()
k8s.OpenPortForwardWrapper(
pkg.ChartmuseumPodName,
pkg.ChartmuseumNamespace,
pkg.ChartmuseumPodPort,
pkg.ChartmuseumPodLocalPort,
chartmuseumStopChannel,
)

pkg.AwaitHostNTimes("http://localhost:8181/health", 5, 5)
viper.Set("chartmuseum.host.resolved", true)
viper.WriteConfig()
Expand All @@ -388,7 +403,7 @@ func runLocal(cmd *cobra.Command, args []string) error {
}

pkg.InformUser("Deploying metaphor applications", silentMode)
err = metaphor.DeployMetaphorGithubLocal(dryRun, githubOwner, metaphorBranch, "")
err := metaphor.DeployMetaphorGithubLocal(dryRun, githubOwner, metaphorBranch, "")
if err != nil {
pkg.InformUser("Error deploy metaphor applications", silentMode)
log.Println("Error running deployMetaphorCmd")
Expand Down Expand Up @@ -425,10 +440,18 @@ func runLocal(cmd *cobra.Command, args []string) error {
var wg sync.WaitGroup
wg.Add(1)
go func() {
err := k8s.OpenAtlantisPortForward()
if err != nil {
log.Println(err)
}
// Atlantis port-forward
atlantisStopChannel := make(chan struct{}, 1)
defer func() {
close(atlantisStopChannel)
}()
k8s.OpenPortForwardWrapper(
pkg.AtlantisPodName,
pkg.AtlantisNamespace,
pkg.AtlantisPodPort,
pkg.AtlantisPodLocalPort,
atlantisStopChannel,
)

gitHubClient := githubWrapper.New()
err = gitHubClient.CreatePR(branchName)
Expand Down
61 changes: 51 additions & 10 deletions cmd/local/postrun.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package local

import (
"log"
"time"

"github.com/kubefirst/kubefirst/internal/k8s"
"github.com/kubefirst/kubefirst/internal/reports"
"github.com/kubefirst/kubefirst/pkg"
"github.com/spf13/cobra"
"log"
"os"
"os/signal"
"sync"
"syscall"
)

func runPostLocal(cmd *cobra.Command, args []string) error {
Expand All @@ -17,21 +19,50 @@ func runPostLocal(cmd *cobra.Command, args []string) error {
return nil
}

// open all port forwards, wait console ui be ready, and open console ui in the browser
err := k8s.OpenPortForwardForKubeConConsole()
// every port forward has its own closing control. when a channel is closed, the port forward is close.
vaultStopChannel := make(chan struct{}, 1)
argoStopChannel := make(chan struct{}, 1)
argoCDStopChannel := make(chan struct{}, 1)
chartmuseumStopChannel := make(chan struct{}, 1)
minioStopChannel := make(chan struct{}, 1)
minioConsoleStopChannel := make(chan struct{}, 1)
kubefirstConsoleStopChannel := make(chan struct{}, 1)
AtlantisStopChannel := make(chan struct{}, 1)

// guarantee it will close the port forwards even on a process kill
defer func() {
close(vaultStopChannel)
close(argoStopChannel)
close(argoCDStopChannel)
close(chartmuseumStopChannel)
close(minioStopChannel)
close(minioConsoleStopChannel)
close(kubefirstConsoleStopChannel)
close(AtlantisStopChannel)
log.Println("leaving port-forward command, port forwards are now closed")
}()

err := k8s.OpenPortForwardForLocal(
vaultStopChannel,
argoStopChannel,
argoCDStopChannel,
chartmuseumStopChannel,
minioStopChannel,
minioConsoleStopChannel,
kubefirstConsoleStopChannel,
AtlantisStopChannel,
)
if err != nil {
log.Println(err)
return err
}

time.Sleep(time.Millisecond * 2000)

log.Println("Starting the presentation of console and api for the handoff screen")

err = pkg.IsConsoleUIAvailable(pkg.ConsoleUILocalURL)
err = pkg.IsConsoleUIAvailable(pkg.KubefirstConsoleLocalURL)
if err != nil {
log.Println(err)
}
err = pkg.OpenBrowser(pkg.ConsoleUILocalURL)
err = pkg.OpenBrowser(pkg.KubefirstConsoleLocalURL)
if err != nil {
log.Println(err)
}
Expand All @@ -40,5 +71,15 @@ func runPostLocal(cmd *cobra.Command, args []string) error {

log.Println("Kubefirst Console available at: http://localhost:9094", silentMode)

// managing termination signal from the terminal
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
var wg sync.WaitGroup
wg.Add(1)
go func() {
<-sigs
wg.Done()
}()

return nil
}
10 changes: 5 additions & 5 deletions cmd/postInstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ var postInstallCmd = &cobra.Command{
log.Println(err)
}

err = pkg.IsConsoleUIAvailable(pkg.ConsoleUILocalURL)
err = pkg.IsConsoleUIAvailable(pkg.KubefirstConsoleLocalURL)
if err != nil {
log.Println(err)
}

err = pkg.OpenBrowser(pkg.ConsoleUILocalURL)
err = pkg.OpenBrowser(pkg.KubefirstConsoleLocalURL)
if err != nil {
log.Println(err)
}
Expand All @@ -64,12 +64,12 @@ var postInstallCmd = &cobra.Command{
log.Println(err)
}

err = pkg.IsConsoleUIAvailable(pkg.ConsoleUILocalURL)
err = pkg.IsConsoleUIAvailable(pkg.KubefirstConsoleLocalURL)
if err != nil {
log.Println(err)
}
err = pkg.OpenBrowser(pkg.ConsoleUILocalURL)
if pkg.OpenBrowser(pkg.ConsoleUILocalURL) != nil {
err = pkg.OpenBrowser(pkg.KubefirstConsoleLocalURL)
if pkg.OpenBrowser(pkg.KubefirstConsoleLocalURL) != nil {
log.Println(err)
}
}
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ func Execute() {
func init() {
cobra.OnInitialize()
rootCmd.AddCommand(local.NewCommand())
rootCmd.AddCommand(NewDevCommand())
}
Loading