Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add egctl experimental dashboard envoy-proxy to render the admin dashboard for the proxy #2669

Merged
merged 3 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/cmd/egctl/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ func (fw *fakePortForwarder) Address() string {
return fmt.Sprintf("localhost:%d", fw.localPort)
}

func (fw *fakePortForwarder) WaitForStop() {}

func TestExtractAllConfigDump(t *testing.T) {
input, err := readInputConfig("in.all.json")
require.NoError(t, err)
Expand Down
23 changes: 23 additions & 0 deletions internal/cmd/egctl/dashboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright Envoy Gateway Authors
// SPDX-License-Identifier: Apache-2.0
// The full text of the Apache license is available in the LICENSE file at
// the root of the repo.

package egctl

import (
"github.com/spf13/cobra"
)

func newDashboardCommand() *cobra.Command {
c := &cobra.Command{
Use: "dashboard",
Aliases: []string{"d"},
Long: "Retrieve the dashboard.",
Short: "Retrieve the dashboard.",
}

c.AddCommand(newEnvoyDashboardCmd())

return c
}
138 changes: 138 additions & 0 deletions internal/cmd/egctl/envoy_dashboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Copyright Envoy Gateway Authors
// SPDX-License-Identifier: Apache-2.0
// The full text of the Apache license is available in the LICENSE file at
// the root of the repo.

package egctl

import (
"errors"
"fmt"
"io"
"os"
"os/exec"
"os/signal"
"runtime"

"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/types"

kube "github.com/envoyproxy/gateway/internal/kubernetes"
)

func newEnvoyDashboardCmd() *cobra.Command {
var podName, podNamespace string
var listenPort int

dashboardCmd := &cobra.Command{
Use: "envoy-proxy <name> -n <namespace>",
Short: "Retrieve Envoy admin dashboard for the specified pod",
Long: `Retrieve Envoy admin dashboard for the specified pod.`,
Example: ` # Retrieve Envoy admin dashboard for the specified pod.
egctl experimental dashboard envoy-proxy <pod-name> -n <namespace>

# short syntax
egctl experimental d envoy-proxy <pod-name> -n <namespace>
hanxiaop marked this conversation as resolved.
Show resolved Hide resolved
`,
Aliases: []string{"d"},
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 && len(labelSelectors) == 0 {
cmd.Println(cmd.UsageString())
return fmt.Errorf("dashboard requires pod name or label selector")
}
if len(args) > 0 && len(labelSelectors) > 0 {
cmd.Println(cmd.UsageString())
return fmt.Errorf("name cannot be provided when a selector is specified")
}
return nil
},
RunE: func(c *cobra.Command, args []string) error {
if listenPort > 65535 || listenPort < 0 {
return fmt.Errorf("invalid port number range")
}

Check warning on line 52 in internal/cmd/egctl/envoy_dashboard.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/envoy_dashboard.go#L49-L52

Added lines #L49 - L52 were not covered by tests

kubeClient, err := getCLIClient()
if err != nil {
return err
}
if len(args) != 0 {
podName = args[0]
}
if len(labelSelectors) > 0 {
pl, err := kubeClient.PodsForSelector(podNamespace, labelSelectors...)
if err != nil {
return fmt.Errorf("not able to locate pod with selector %s: %w", labelSelectors, err)
}
if len(pl.Items) < 1 {
return errors.New("no pods found")
}
podName = pl.Items[0].Name
podNamespace = pl.Items[0].Namespace

Check warning on line 70 in internal/cmd/egctl/envoy_dashboard.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/envoy_dashboard.go#L54-L70

Added lines #L54 - L70 were not covered by tests
}

return portForward(podName, podNamespace, "http://%s", listenPort, kubeClient, c.OutOrStdout())

Check warning on line 73 in internal/cmd/egctl/envoy_dashboard.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/envoy_dashboard.go#L73

Added line #L73 was not covered by tests
},
}
dashboardCmd.PersistentFlags().StringArrayVarP(&labelSelectors, "labels", "l", nil, "Labels to select the envoy proxy pod.")
dashboardCmd.PersistentFlags().StringVarP(&podNamespace, "namespace", "n", "envoy-gateway-system", "Namespace where envoy proxy pod are installed.")
dashboardCmd.PersistentFlags().IntVarP(&listenPort, "port", "p", 0, "Local port to listen to.")

return dashboardCmd

Check warning on line 80 in internal/cmd/egctl/envoy_dashboard.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/envoy_dashboard.go#L76-L80

Added lines #L76 - L80 were not covered by tests
}

// portForward first tries to forward localhost:remotePort to podName:remotePort, falls back to dynamic local port
func portForward(podName, namespace, urlFormat string, listenPort int, client kube.CLIClient, writer io.Writer) error {
hanxiaop marked this conversation as resolved.
Show resolved Hide resolved
var fw kube.PortForwarder
meta := types.NamespacedName{
Namespace: namespace,
Name: podName,
}
fw, err := kube.NewLocalPortForwarder(client, meta, listenPort, adminPort)
hanxiaop marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return fmt.Errorf("could not build port forwarder for envoy proxy: %w", err)
}

Check warning on line 93 in internal/cmd/egctl/envoy_dashboard.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/envoy_dashboard.go#L84-L93

Added lines #L84 - L93 were not covered by tests

if err = fw.Start(); err != nil {
fw.Stop()
return fmt.Errorf("could not start port forwarder for envoy proxy: %w", err)
}

Check warning on line 98 in internal/cmd/egctl/envoy_dashboard.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/envoy_dashboard.go#L95-L98

Added lines #L95 - L98 were not covered by tests

ClosePortForwarderOnInterrupt(fw)

openBrowser(fmt.Sprintf(urlFormat, fw.Address()), writer)

fw.WaitForStop()

return nil

Check warning on line 106 in internal/cmd/egctl/envoy_dashboard.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/envoy_dashboard.go#L100-L106

Added lines #L100 - L106 were not covered by tests
}

func ClosePortForwarderOnInterrupt(fw kube.PortForwarder) {
go func() {
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)
defer signal.Stop(signals)
<-signals
fw.Stop()
}()

Check warning on line 116 in internal/cmd/egctl/envoy_dashboard.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/envoy_dashboard.go#L109-L116

Added lines #L109 - L116 were not covered by tests
}

func openBrowser(url string, writer io.Writer) {
var err error

fmt.Fprintf(writer, "%s\n", url)

switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
fmt.Fprintf(writer, "Unsupported platform %q; open %s in your browser.\n", runtime.GOOS, url)

Check warning on line 132 in internal/cmd/egctl/envoy_dashboard.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/envoy_dashboard.go#L119-L132

Added lines #L119 - L132 were not covered by tests
}

if err != nil {
fmt.Fprintf(writer, "Failed to open browser; open %s in your browser.\n", url)
}

Check warning on line 137 in internal/cmd/egctl/envoy_dashboard.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/envoy_dashboard.go#L135-L137

Added lines #L135 - L137 were not covered by tests
}
1 change: 1 addition & 0 deletions internal/cmd/egctl/experimental.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
experimentalCommand.AddCommand(newTranslateCommand())
experimentalCommand.AddCommand(newStatsCommand())
experimentalCommand.AddCommand(newStatusCommand())
experimentalCommand.AddCommand(newDashboardCommand())

Check warning on line 28 in internal/cmd/egctl/experimental.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/experimental.go#L28

Added line #L28 was not covered by tests

return experimentalCommand
}
6 changes: 6 additions & 0 deletions internal/kubernetes/port-forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type PortForwarder interface {

Stop()

WaitForStop()

// Address returns the address of the local forwarded address.
Address() string
}
Expand Down Expand Up @@ -128,6 +130,10 @@ func (f *localForwarder) Stop() {
close(f.stopCh)
}

func (f *localForwarder) WaitForStop() {
<-f.stopCh
}

func (f *localForwarder) Address() string {
return fmt.Sprintf("%s:%d", netutil.DefaultLocalAddress, f.localPort)
}
18 changes: 18 additions & 0 deletions site/content/en/latest/user/egctl.md
Original file line number Diff line number Diff line change
Expand Up @@ -795,3 +795,21 @@ product backend ResolvedRefs True ResolvedRefs

[Multi-tenancy]: ../deployment-mode#multi-tenancy
[EnvoyProxy]: ../../api/extension_types#envoyproxy


## egctl experimental dashboard

This subcommand streamlines the process for users to access the Envoy admin dashboard. By executing the following command:

```bash
egctl x dashboard envoy-proxy -n envoy-gateway-system envoy-engw-eg-a9c23fbb-558f94486c-82wh4
```

You will see the following output:

```bash
egctl x dashboard envoy-proxy -n envoy-gateway-system envoy-engw-eg-a9c23fbb-558f94486c-82wh4
http://localhost:19000
```

the Envoy admin dashboard will automatically open in your default web browser. This eliminates the need to manually locate and expose the admin port.
Loading