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 2 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
}
134 changes: 134 additions & 0 deletions internal/cmd/egctl/envoy_dashboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// 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: "Retrieves Envoy admin dashboard for the specified pod",
hanxiaop marked this conversation as resolved.
Show resolved Hide resolved
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 {
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 66 in internal/cmd/egctl/envoy_dashboard.go

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L49 - L66 were not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/envoy_dashboard.go#L69

Added line #L69 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 76 in internal/cmd/egctl/envoy_dashboard.go

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L72 - L76 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 89 in internal/cmd/egctl/envoy_dashboard.go

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L80 - L89 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 94 in internal/cmd/egctl/envoy_dashboard.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/envoy_dashboard.go#L91-L94

Added lines #L91 - L94 were not covered by tests

ClosePortForwarderOnInterrupt(fw)

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

fw.WaitForStop()

return nil

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

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/envoy_dashboard.go#L96-L102

Added lines #L96 - L102 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 112 in internal/cmd/egctl/envoy_dashboard.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/envoy_dashboard.go#L105-L112

Added lines #L105 - L112 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 128 in internal/cmd/egctl/envoy_dashboard.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/envoy_dashboard.go#L115-L128

Added lines #L115 - L128 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 133 in internal/cmd/egctl/envoy_dashboard.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/envoy_dashboard.go#L131-L133

Added lines #L131 - L133 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)
}
Loading