Skip to content

Commit

Permalink
feat: add hgctl dashboard controller subcommand to open higress contr…
Browse files Browse the repository at this point in the history
…oller debug web ui (alibaba#564)

Co-authored-by: Xunzhuo <[email protected]>
  • Loading branch information
2456868764 and Xunzhuo authored Sep 26, 2023
1 parent fcf1953 commit 5b663ae
Showing 1 changed file with 45 additions and 4 deletions.
49 changes: 45 additions & 4 deletions pkg/cmd/hgctl/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -54,6 +55,7 @@ const (
defaultPrometheusPort = 9090
defaultGrafanaPort = 3000
defaultConsolePort = 8080
defaultControllerPort = 8888
)

func newDashboardCmd() *cobra.Command {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 5b663ae

Please sign in to comment.