From 5f4a856c5e8a002caac38fa392355c64b75f5dab Mon Sep 17 00:00:00 2001 From: "M. Mert Yildiran" Date: Sun, 22 Jan 2023 04:25:47 +0300 Subject: [PATCH] :bug: Fix the `proxy` command --- cmd/cleanRunner.go | 2 +- cmd/common.go | 6 ++- cmd/proxyRunner.go | 96 ++++++++++++++++++++++++++++++++++++++-------- cmd/tapRunner.go | 2 +- 4 files changed, 85 insertions(+), 21 deletions(-) diff --git a/cmd/cleanRunner.go b/cmd/cleanRunner.go index 8d0675073..befff79eb 100644 --- a/cmd/cleanRunner.go +++ b/cmd/cleanRunner.go @@ -10,5 +10,5 @@ func performCleanCommand() { return } - finishSelfExecution(kubernetesProvider, config.Config.IsNsRestrictedMode(), config.Config.SelfNamespace) + finishSelfExecution(kubernetesProvider, config.Config.IsNsRestrictedMode(), config.Config.SelfNamespace, false) } diff --git a/cmd/common.go b/cmd/common.go index 61199b9fc..85b54699b 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -99,11 +99,13 @@ func handleKubernetesProviderError(err error) { } } -func finishSelfExecution(kubernetesProvider *kubernetes.Provider, isNsRestrictedMode bool, selfNamespace string) { +func finishSelfExecution(kubernetesProvider *kubernetes.Provider, isNsRestrictedMode bool, selfNamespace string, withoutCleanup bool) { removalCtx, cancel := context.WithTimeout(context.Background(), cleanupTimeout) defer cancel() dumpLogsIfNeeded(removalCtx, kubernetesProvider) - resources.CleanUpSelfResources(removalCtx, cancel, kubernetesProvider, isNsRestrictedMode, selfNamespace) + if !withoutCleanup { + resources.CleanUpSelfResources(removalCtx, cancel, kubernetesProvider, isNsRestrictedMode, selfNamespace) + } } func dumpLogsIfNeeded(ctx context.Context, kubernetesProvider *kubernetes.Provider) { diff --git a/cmd/proxyRunner.go b/cmd/proxyRunner.go index 82750016c..64da180f6 100644 --- a/cmd/proxyRunner.go +++ b/cmd/proxyRunner.go @@ -26,7 +26,7 @@ func runProxy() { exists, err := kubernetesProvider.DoesServiceExist(ctx, config.Config.SelfNamespace, kubernetes.FrontServiceName) if err != nil { log.Error(). - Str("service", misc.Program). + Str("service", kubernetes.FrontServiceName). Err(err). Msg("Failed to found service!") cancel() @@ -42,34 +42,96 @@ func runProxy() { return } - url := kubernetes.GetLocalhostOnPort(config.Config.Tap.Proxy.Front.SrcPort) + exists, err = kubernetesProvider.DoesServiceExist(ctx, config.Config.SelfNamespace, kubernetes.HubServiceName) + if err != nil { + log.Error(). + Str("service", kubernetes.HubServiceName). + Err(err). + Msg("Failed to found service!") + cancel() + return + } - response, err := http.Get(fmt.Sprintf("%s/", url)) + if !exists { + log.Error(). + Str("service", kubernetes.HubServiceName). + Str("command", fmt.Sprintf("%s %s", misc.Program, tapCmd.Use)). + Msg("Service not found! You should run the command first:") + cancel() + return + } + + var establishedProxy bool + + hubUrl := kubernetes.GetLocalhostOnPort(config.Config.Tap.Proxy.Hub.SrcPort) + response, err := http.Get(fmt.Sprintf("%s/", hubUrl)) + if err == nil && response.StatusCode == 200 { + log.Info(). + Str("service", kubernetes.HubServiceName). + Int("port", int(config.Config.Tap.Proxy.Hub.SrcPort)). + Msg("Found a running service.") + + okToOpen("Hub", hubUrl) + } else { + startProxyReportErrorIfAny( + kubernetesProvider, + ctx, + cancel, + kubernetes.HubServiceName, + kubernetes.HubPodName, + configStructs.ProxyHubPortLabel, + config.Config.Tap.Proxy.Hub.SrcPort, + config.Config.Tap.Proxy.Hub.DstPort, + "/echo", + ) + connector := connect.NewConnector(hubUrl, connect.DefaultRetries, connect.DefaultTimeout) + if err := connector.TestConnection("/echo"); err != nil { + log.Error().Msg(fmt.Sprintf(utils.Red, "Couldn't connect to Hub.")) + return + } + + establishedProxy = true + okToOpen("Hub", hubUrl) + } + + frontUrl := kubernetes.GetLocalhostOnPort(config.Config.Tap.Proxy.Front.SrcPort) + response, err = http.Get(fmt.Sprintf("%s/", frontUrl)) if err == nil && response.StatusCode == 200 { log.Info(). Str("service", kubernetes.FrontServiceName). Int("port", int(config.Config.Tap.Proxy.Front.SrcPort)). Msg("Found a running service.") - okToOpen(url) - return - } - log.Info().Msg("Establishing connection to K8s cluster...") - startProxyReportErrorIfAny(kubernetesProvider, ctx, cancel, kubernetes.FrontServiceName, kubernetes.FrontPodName, configStructs.ProxyFrontPortLabel, config.Config.Tap.Proxy.Front.SrcPort, config.Config.Tap.Proxy.Front.DstPort, "") + okToOpen("Front", frontUrl) + } else { + startProxyReportErrorIfAny( + kubernetesProvider, + ctx, + cancel, + kubernetes.FrontServiceName, + kubernetes.FrontPodName, + configStructs.ProxyFrontPortLabel, + config.Config.Tap.Proxy.Front.SrcPort, + config.Config.Tap.Proxy.Front.DstPort, + "", + ) + connector := connect.NewConnector(frontUrl, connect.DefaultRetries, connect.DefaultTimeout) + if err := connector.TestConnection(""); err != nil { + log.Error().Msg(fmt.Sprintf(utils.Red, "Couldn't connect to Front.")) + return + } - connector := connect.NewConnector(url, connect.DefaultRetries, connect.DefaultTimeout) - if err := connector.TestConnection(""); err != nil { - log.Error().Msg(fmt.Sprintf(utils.Red, "Couldn't connect to Front.")) - return + establishedProxy = true + okToOpen("Front", frontUrl) } - okToOpen(url) - - utils.WaitForTermination(ctx, cancel) + if establishedProxy { + utils.WaitForTermination(ctx, cancel) + } } -func okToOpen(url string) { - log.Info().Str("url", url).Msg(fmt.Sprintf(utils.Green, fmt.Sprintf("%s is available at:", misc.Software))) +func okToOpen(name string, url string) { + log.Info().Str("url", url).Msg(fmt.Sprintf(utils.Green, fmt.Sprintf("%s is available at:", name))) if !config.Config.HeadlessMode { utils.OpenBrowser(url) diff --git a/cmd/tapRunner.go b/cmd/tapRunner.go index 25b0d9a7d..c7a5e32c9 100644 --- a/cmd/tapRunner.go +++ b/cmd/tapRunner.go @@ -105,7 +105,7 @@ func tap() { } func finishTapExecution(kubernetesProvider *kubernetes.Provider) { - finishSelfExecution(kubernetesProvider, config.Config.IsNsRestrictedMode(), config.Config.SelfNamespace) + finishSelfExecution(kubernetesProvider, config.Config.IsNsRestrictedMode(), config.Config.SelfNamespace, true) } /*