From 5b663ae4121f22069523662beefa23fcd5b379e8 Mon Sep 17 00:00:00 2001 From: Jun <108045855+2456868764@users.noreply.github.com> Date: Tue, 26 Sep 2023 20:27:57 +0800 Subject: [PATCH] feat: add hgctl dashboard controller subcommand to open higress controller debug web ui (#564) Co-authored-by: Xunzhuo --- pkg/cmd/hgctl/dashboard.go | 49 ++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/pkg/cmd/hgctl/dashboard.go b/pkg/cmd/hgctl/dashboard.go index a1410e405..04eb4d59a 100644 --- a/pkg/cmd/hgctl/dashboard.go +++ b/pkg/cmd/hgctl/dashboard.go @@ -30,10 +30,11 @@ import ( ) var ( - listenPort = 0 - promPort = 0 - grafanaPort = 0 - consolePort = 0 + listenPort = 0 + promPort = 0 + grafanaPort = 0 + consolePort = 0 + controllerPort = 0 bindAddress = "localhost" @@ -54,6 +55,7 @@ const ( defaultPrometheusPort = 9090 defaultGrafanaPort = 3000 defaultConsolePort = 8080 + defaultControllerPort = 8888 ) func newDashboardCmd() *cobra.Command { @@ -99,6 +101,10 @@ func newDashboardCmd() *cobra.Command { consoleCmd.PersistentFlags().IntVar(&consolePort, "ui-port", defaultConsolePort, "The component dashboard UI port.") dashboardCmd.AddCommand(consoleCmd) + controllerDebugCmd := controllerDebugCmd() + controllerDebugCmd.PersistentFlags().IntVar(&controllerPort, "ui-port", defaultControllerPort, "The component dashboard UI port.") + dashboardCmd.AddCommand(controllerDebugCmd) + return dashboardCmd } @@ -265,6 +271,41 @@ func envoyDashCmd() *cobra.Command { return cmd } +// port-forward to Higress System Console; open browser +func controllerDebugCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "controller", + Short: "Open Controller debug web UI", + Long: `Open Higress Controller`, + Example: ` hgctl dashboard controller + + # with short syntax + hgctl dash controller + hgctl d controller`, + RunE: func(cmd *cobra.Command, args []string) error { + client, err := kubernetes.NewCLIClient(options.DefaultConfigFlags.ToRawKubeConfigLoader()) + if err != nil { + return fmt.Errorf("build CLI client fail: %w", err) + } + + pl, err := client.PodsForSelector(addonNamespace, "app=higress-controller") + if err != nil { + return fmt.Errorf("not able to locate controller pod: %v", err) + } + + if len(pl.Items) < 1 { + return errors.New("no higress controller pods found") + } + + // only use the first pod in the list + return portForward(pl.Items[0].Name, addonNamespace, "Controller", + "http://%s/debug", bindAddress, controllerPort, client, cmd.OutOrStdout(), browser) + }, + } + + return cmd +} + // portForward first tries to forward localhost:remotePort to podName:remotePort, falls back to dynamic local port func portForward(podName, namespace, flavor, urlFormat, localAddress string, remotePort int, client kubernetes.CLIClient, writer io.Writer, browser bool,