feat: add hgctl dashboard controller subcommand to open higress controller debug web ui (#564)

Co-authored-by: Xunzhuo <bitliu@tencent.com>
This commit is contained in:
Jun
2023-09-26 20:27:57 +08:00
committed by GitHub
parent fcf19535f9
commit 5b663ae412

View File

@@ -30,10 +30,11 @@ import (
) )
var ( var (
listenPort = 0 listenPort = 0
promPort = 0 promPort = 0
grafanaPort = 0 grafanaPort = 0
consolePort = 0 consolePort = 0
controllerPort = 0
bindAddress = "localhost" bindAddress = "localhost"
@@ -54,6 +55,7 @@ const (
defaultPrometheusPort = 9090 defaultPrometheusPort = 9090
defaultGrafanaPort = 3000 defaultGrafanaPort = 3000
defaultConsolePort = 8080 defaultConsolePort = 8080
defaultControllerPort = 8888
) )
func newDashboardCmd() *cobra.Command { func newDashboardCmd() *cobra.Command {
@@ -99,6 +101,10 @@ func newDashboardCmd() *cobra.Command {
consoleCmd.PersistentFlags().IntVar(&consolePort, "ui-port", defaultConsolePort, "The component dashboard UI port.") consoleCmd.PersistentFlags().IntVar(&consolePort, "ui-port", defaultConsolePort, "The component dashboard UI port.")
dashboardCmd.AddCommand(consoleCmd) dashboardCmd.AddCommand(consoleCmd)
controllerDebugCmd := controllerDebugCmd()
controllerDebugCmd.PersistentFlags().IntVar(&controllerPort, "ui-port", defaultControllerPort, "The component dashboard UI port.")
dashboardCmd.AddCommand(controllerDebugCmd)
return dashboardCmd return dashboardCmd
} }
@@ -265,6 +271,41 @@ func envoyDashCmd() *cobra.Command {
return cmd 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 // 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, func portForward(podName, namespace, flavor, urlFormat, localAddress string, remotePort int,
client kubernetes.CLIClient, writer io.Writer, browser bool, client kubernetes.CLIClient, writer io.Writer, browser bool,