diff --git a/cmd/open.go b/cmd/open.go new file mode 100644 index 000000000..b56ba2a77 --- /dev/null +++ b/cmd/open.go @@ -0,0 +1,18 @@ +package cmd + +import ( + "github.com/spf13/cobra" +) + +var openCmd = &cobra.Command{ + Use: "open", + Short: "Open the web UI in the browser", + RunE: func(cmd *cobra.Command, args []string) error { + runOpen() + return nil + }, +} + +func init() { + rootCmd.AddCommand(openCmd) +} diff --git a/cmd/openRunner.go b/cmd/openRunner.go new file mode 100644 index 000000000..5a2acdc06 --- /dev/null +++ b/cmd/openRunner.go @@ -0,0 +1,75 @@ +package cmd + +import ( + "context" + "fmt" + "net/http" + + "github.com/kubeshark/kubeshark/config" + "github.com/kubeshark/kubeshark/internal/connect" + "github.com/kubeshark/kubeshark/kubernetes" + "github.com/kubeshark/kubeshark/utils" + "github.com/rs/zerolog/log" +) + +func runOpen() { + kubernetesProvider, err := getKubernetesProviderForCli() + if err != nil { + return + } + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + exists, err := kubernetesProvider.DoesServiceExist(ctx, config.Config.ResourcesNamespace, kubernetes.FrontServiceName) + if err != nil { + log.Error(). + Str("service", "kubeshark"). + Err(err). + Msg("Failed to found service!") + cancel() + return + } + + if !exists { + log.Error(). + Str("service", kubernetes.FrontServiceName). + Str("command", "kubeshark tap"). + Msg("Service not found! You should run the command first:") + cancel() + return + } + + url := kubernetes.GetLocalhostOnPort(config.Config.Front.PortForward.SrcPort) + + response, err := http.Get(fmt.Sprintf("%s/", url)) + if err == nil && response.StatusCode == 200 { + log.Info(). + Str("service", kubernetes.FrontServiceName). + Int("port", int(config.Config.Front.PortForward.SrcPort)). + Msg("Found a running service.") + + okToOpen(url) + return + } + log.Info().Msg("Establishing connection to K8s cluster...") + startProxyReportErrorIfAny(kubernetesProvider, ctx, cancel, kubernetes.FrontServiceName, config.Config.Front.PortForward.SrcPort, config.Config.Front.PortForward.DstPort, "") + + 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 + } + + okToOpen(url) + + utils.WaitForFinish(ctx, cancel) +} + +func okToOpen(url string) { + log.Info().Str("url", url).Msg(fmt.Sprintf(utils.Green, "Kubeshark is available at:")) + + if !config.Config.HeadlessMode { + utils.OpenBrowser(url) + } +} diff --git a/cmd/tapRunner.go b/cmd/tapRunner.go index 1f5452290..600050002 100644 --- a/cmd/tapRunner.go +++ b/cmd/tapRunner.go @@ -464,6 +464,7 @@ func postFrontStarted(ctx context.Context, kubernetesProvider *kubernetes.Provid url := kubernetes.GetLocalhostOnPort(config.Config.Front.PortForward.SrcPort) log.Info().Str("url", url).Msg(fmt.Sprintf(utils.Green, "Kubeshark is available at:")) + if !config.Config.HeadlessMode { utils.OpenBrowser(url) } diff --git a/cmd/view.go b/cmd/view.go deleted file mode 100644 index 3177fb50e..000000000 --- a/cmd/view.go +++ /dev/null @@ -1,33 +0,0 @@ -package cmd - -import ( - "github.com/creasty/defaults" - "github.com/kubeshark/kubeshark/config/configStructs" - "github.com/rs/zerolog/log" - "github.com/spf13/cobra" -) - -var viewCmd = &cobra.Command{ - Use: "view", - Short: "Open GUI in browser", - RunE: func(cmd *cobra.Command, args []string) error { - runKubesharkView() - return nil - }, -} - -func init() { - rootCmd.AddCommand(viewCmd) - - defaultViewConfig := configStructs.ViewConfig{} - if err := defaults.Set(&defaultViewConfig); err != nil { - log.Error().Err(err).Send() - } - - viewCmd.Flags().Uint16P(configStructs.GuiPortViewName, "p", defaultViewConfig.GuiPort, "Provide a custom port for the web interface webserver") - viewCmd.Flags().StringP(configStructs.UrlViewName, "u", defaultViewConfig.Url, "Provide a custom host") - - if err := viewCmd.Flags().MarkHidden(configStructs.UrlViewName); err != nil { - log.Error().Err(err).Send() - } -} diff --git a/cmd/viewRunner.go b/cmd/viewRunner.go deleted file mode 100644 index 7af0de72c..000000000 --- a/cmd/viewRunner.go +++ /dev/null @@ -1,73 +0,0 @@ -package cmd - -import ( - "context" - "fmt" - "net/http" - - "github.com/kubeshark/kubeshark/config" - "github.com/kubeshark/kubeshark/internal/connect" - "github.com/kubeshark/kubeshark/kubernetes" - "github.com/kubeshark/kubeshark/utils" - "github.com/rs/zerolog/log" -) - -func runKubesharkView() { - kubernetesProvider, err := getKubernetesProviderForCli() - if err != nil { - return - } - - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - url := config.Config.View.Url - - if url == "" { - exists, err := kubernetesProvider.DoesServiceExist(ctx, config.Config.ResourcesNamespace, kubernetes.HubServiceName) - if err != nil { - log.Error(). - Str("name", "kubeshark"). - Err(err). - Msg("Failed to found service!") - cancel() - return - } - if !exists { - log.Printf("%s service not found, you should run `kubeshark tap` command first", kubernetes.HubServiceName) - log.Error(). - Str("name", kubernetes.HubServiceName). - Str("tap-command", "kubeshark tap"). - Msg("Service not found! You should run the tap command first:") - cancel() - return - } - - url = kubernetes.GetLocalhostOnPort(config.Config.Front.PortForward.SrcPort) - - response, err := http.Get(fmt.Sprintf("%s/", url)) - if err == nil && response.StatusCode == 200 { - log.Info(). - Str("name", kubernetes.HubServiceName). - Int("port", int(config.Config.Front.PortForward.SrcPort)). - Msg("Found a running service.") - return - } - log.Info().Msg("Establishing connection to k8s cluster...") - startProxyReportErrorIfAny(kubernetesProvider, ctx, cancel, kubernetes.FrontServiceName, config.Config.Front.PortForward.SrcPort, config.Config.Front.PortForward.DstPort, "") - } - - 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 Hub.")) - return - } - - log.Info().Msg(fmt.Sprintf("Kubeshark is available at %s", url)) - - if !config.Config.HeadlessMode { - utils.OpenBrowser(url) - } - - utils.WaitForFinish(ctx, cancel) -} diff --git a/config/configStruct.go b/config/configStruct.go index f4a5cae79..c6f84cd42 100644 --- a/config/configStruct.go +++ b/config/configStruct.go @@ -58,7 +58,6 @@ type ConfigStruct struct { Hub HubConfig `yaml:"hub"` Front FrontConfig `yaml:"front"` Tap configStructs.TapConfig `yaml:"tap"` - View configStructs.ViewConfig `yaml:"view"` Logs configStructs.LogsConfig `yaml:"logs"` Config configStructs.ConfigConfig `yaml:"config,omitempty"` ImagePullPolicyStr string `yaml:"image-pull-policy" default:"Always"` diff --git a/config/configStructs/viewConfig.go b/config/configStructs/viewConfig.go deleted file mode 100644 index 0337680c8..000000000 --- a/config/configStructs/viewConfig.go +++ /dev/null @@ -1,11 +0,0 @@ -package configStructs - -const ( - GuiPortViewName = "gui-port" - UrlViewName = "url" -) - -type ViewConfig struct { - GuiPort uint16 `yaml:"gui-port" default:"8899"` - Url string `yaml:"url,omitempty" readonly:""` -}