From 41f36ba9c29b8764469a85264448e545de3930eb Mon Sep 17 00:00:00 2001 From: Alon Girmonsky <1990761+alongir@users.noreply.github.com> Date: Fri, 11 Oct 2024 13:06:02 -0700 Subject: [PATCH] Added the scripting `console` command functionality to the `tap` command Added both the `scripting` and `console` commands to the `proxy` command Added a `scripting.console`, a boolean value indicating whether the `console` functionality should be part of the `tap` and `proxy` commands --- cmd/console.go | 39 +++++++++++++++++++++---- cmd/proxyRunner.go | 7 +++++ cmd/tapRunner.go | 4 +++ config/configStructs/scriptingConfig.go | 1 + 4 files changed, 46 insertions(+), 5 deletions(-) diff --git a/cmd/console.go b/cmd/console.go index 13d56e5d7..b27ea0814 100644 --- a/cmd/console.go +++ b/cmd/console.go @@ -41,13 +41,15 @@ func init() { consoleCmd.Flags().StringP(configStructs.ReleaseNamespaceLabel, "s", defaultTapConfig.Release.Namespace, "Release namespace of Kubeshark") } -func runConsole() { +func runConsoleWithoutProxy() { + time.Sleep(5 * time.Second) for { hubUrl := kubernetes.GetHubUrl() response, err := http.Get(fmt.Sprintf("%s/echo", hubUrl)) if err != nil || response.StatusCode != 200 { - log.Info().Msg(fmt.Sprintf(utils.Yellow, "Couldn't connect to Hub. Establishing proxy...")) - runProxy(false, true) + log.Info().Msg(fmt.Sprintf(utils.Yellow, "Couldn't connect to Hub.")) + time.Sleep(5 * time.Second) + continue } interrupt := make(chan os.Signal, 1) @@ -101,8 +103,6 @@ func runConsole() { time.Sleep(5 * time.Second) // Delay before reconnecting continue // Reconnect after error case <-interrupt: - log.Warn().Msg(fmt.Sprintf(utils.Yellow, "Received interrupt, exiting...")) - err := c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) if err != nil { log.Error().Err(err).Send() @@ -117,3 +117,32 @@ func runConsole() { } } } + +func runConsole() { + go runConsoleWithoutProxy() + for { + hubUrl := kubernetes.GetHubUrl() + response, err := http.Get(fmt.Sprintf("%s/echo", hubUrl)) + if err != nil || response.StatusCode != 200 { + log.Info().Msg(fmt.Sprintf(utils.Yellow, "Couldn't connect to Hub. Establishing proxy...")) + runProxy(false, true) + } + + interrupt := make(chan os.Signal, 1) + signal.Notify(interrupt, os.Interrupt) + done := make(chan struct{}) + + ticker := time.NewTicker(time.Second) + defer ticker.Stop() + + select { // Reconnect after error + case <-interrupt: + log.Warn().Msg(fmt.Sprintf(utils.Yellow, "Received interrupt, exiting...")) + select { + case <-done: + case <-time.After(time.Second): + } + return + } + } +} diff --git a/cmd/proxyRunner.go b/cmd/proxyRunner.go index 8e685c69b..0b7804b92 100644 --- a/cmd/proxyRunner.go +++ b/cmd/proxyRunner.go @@ -92,10 +92,17 @@ func runProxy(block bool, noBrowser bool) { establishedProxy = true okToOpen("Kubeshark", frontUrl, noBrowser) } + if config.Config.Scripting.Source != "" && config.Config.Scripting.WatchScripts { + watchScripts(kubernetesProvider, false) + } + if config.Config.Scripting.Console { + go runConsoleWithoutProxy() + } if establishedProxy && block { utils.WaitForTermination(ctx, cancel) } + } func okToOpen(name string, url string, noBrowser bool) { diff --git a/cmd/tapRunner.go b/cmd/tapRunner.go index 7c6dd5313..7f2c0f684 100644 --- a/cmd/tapRunner.go +++ b/cmd/tapRunner.go @@ -427,6 +427,10 @@ func postFrontStarted(ctx context.Context, kubernetesProvider *kubernetes.Provid if config.Config.Scripting.Source != "" && config.Config.Scripting.WatchScripts { watchScripts(kubernetesProvider, false) } + + if config.Config.Scripting.Console { + go runConsoleWithoutProxy() + } } func updateConfig(kubernetesProvider *kubernetes.Provider) { diff --git a/config/configStructs/scriptingConfig.go b/config/configStructs/scriptingConfig.go index fbeeed763..9dd6265d8 100644 --- a/config/configStructs/scriptingConfig.go +++ b/config/configStructs/scriptingConfig.go @@ -14,6 +14,7 @@ type ScriptingConfig struct { Env map[string]interface{} `yaml:"env" json:"env" default:"{}"` Source string `yaml:"source" json:"source" default:""` WatchScripts bool `yaml:"watchScripts" json:"watchScripts" default:"true"` + Console bool `yaml:"console" json:"console" default:"true"` } func (config *ScriptingConfig) GetScripts() (scripts []*misc.Script, err error) {