mirror of
https://github.com/kubeshark/kubeshark.git
synced 2025-08-18 16:38:38 +00:00
🐛 Fix the open
command
This commit is contained in:
parent
0e31fce585
commit
5e579ddfb7
18
cmd/open.go
Normal file
18
cmd/open.go
Normal file
@ -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)
|
||||||
|
}
|
75
cmd/openRunner.go
Normal file
75
cmd/openRunner.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
@ -464,6 +464,7 @@ func postFrontStarted(ctx context.Context, kubernetesProvider *kubernetes.Provid
|
|||||||
|
|
||||||
url := kubernetes.GetLocalhostOnPort(config.Config.Front.PortForward.SrcPort)
|
url := kubernetes.GetLocalhostOnPort(config.Config.Front.PortForward.SrcPort)
|
||||||
log.Info().Str("url", url).Msg(fmt.Sprintf(utils.Green, "Kubeshark is available at:"))
|
log.Info().Str("url", url).Msg(fmt.Sprintf(utils.Green, "Kubeshark is available at:"))
|
||||||
|
|
||||||
if !config.Config.HeadlessMode {
|
if !config.Config.HeadlessMode {
|
||||||
utils.OpenBrowser(url)
|
utils.OpenBrowser(url)
|
||||||
}
|
}
|
||||||
|
33
cmd/view.go
33
cmd/view.go
@ -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()
|
|
||||||
}
|
|
||||||
}
|
|
@ -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)
|
|
||||||
}
|
|
@ -58,7 +58,6 @@ type ConfigStruct struct {
|
|||||||
Hub HubConfig `yaml:"hub"`
|
Hub HubConfig `yaml:"hub"`
|
||||||
Front FrontConfig `yaml:"front"`
|
Front FrontConfig `yaml:"front"`
|
||||||
Tap configStructs.TapConfig `yaml:"tap"`
|
Tap configStructs.TapConfig `yaml:"tap"`
|
||||||
View configStructs.ViewConfig `yaml:"view"`
|
|
||||||
Logs configStructs.LogsConfig `yaml:"logs"`
|
Logs configStructs.LogsConfig `yaml:"logs"`
|
||||||
Config configStructs.ConfigConfig `yaml:"config,omitempty"`
|
Config configStructs.ConfigConfig `yaml:"config,omitempty"`
|
||||||
ImagePullPolicyStr string `yaml:"image-pull-policy" default:"Always"`
|
ImagePullPolicyStr string `yaml:"image-pull-policy" default:"Always"`
|
||||||
|
@ -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:""`
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user