forked from linkerd/linkerd2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboard.go
117 lines (95 loc) · 3.68 KB
/
dashboard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package cmd
import (
"fmt"
"os"
"github.com/linkerd/linkerd2/pkg/k8s"
"github.com/pkg/browser"
"github.com/spf13/cobra"
)
// These constants are used by the `show` flag.
const (
// showLinkerd opens the Linkerd dashboard in a web browser (default).
showLinkerd = "linkerd"
// showGrafana opens the Grafana dashboard in a web browser.
showGrafana = "grafana"
// showURL displays dashboard URLs without opening a browser.
showURL = "url"
)
type dashboardOptions struct {
dashboardProxyPort int
dashboardShow string
wait bool
}
func newDashboardOptions() *dashboardOptions {
return &dashboardOptions{
dashboardProxyPort: 0,
dashboardShow: showLinkerd,
wait: false,
}
}
func newCmdDashboard() *cobra.Command {
options := newDashboardOptions()
cmd := &cobra.Command{
Use: "dashboard [flags]",
Short: "Open the Linkerd dashboard in a web browser",
RunE: func(cmd *cobra.Command, args []string) error {
if options.dashboardProxyPort < 0 {
return fmt.Errorf("port must be greater than or equal to zero, was %d", options.dashboardProxyPort)
}
if options.dashboardShow != showLinkerd && options.dashboardShow != showGrafana && options.dashboardShow != showURL {
return fmt.Errorf("unknown value for 'show' param, was: %s, must be one of: %s, %s, %s",
options.dashboardShow, showLinkerd, showGrafana, showURL)
}
kubernetesProxy, err := k8s.NewProxy(kubeconfigPath, options.dashboardProxyPort)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to initialize proxy: %s\n", err)
os.Exit(1)
}
url, err := kubernetesProxy.URLFor(controlPlaneNamespace, "/services/web:http/proxy/")
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to generate URL for dashboard: %s\n", err)
os.Exit(1)
}
grafanaUrl, err := kubernetesProxy.URLFor(controlPlaneNamespace, "/services/grafana:http/proxy/")
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to generate URL for Grafana: %s\n", err)
os.Exit(1)
}
// ensure we can connect to the public API before starting the proxy
validatedPublicAPIClient(options.wait)
fmt.Printf("Linkerd dashboard available at:\n%s\n", url.String())
fmt.Printf("Grafana dashboard available at:\n%s\n", grafanaUrl.String())
switch options.dashboardShow {
case showLinkerd:
fmt.Println("Opening Linkerd dashboard in the default browser")
err = browser.OpenURL(url.String())
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to open Linkerd URL %s in the default browser: %s", url, err)
os.Exit(1)
}
case showGrafana:
fmt.Println("Opening Grafana dashboard in the default browser")
err = browser.OpenURL(grafanaUrl.String())
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to open Grafana URL %s in the default browser: %s", grafanaUrl, err)
os.Exit(1)
}
case showURL:
// no-op, we already printed the URLs
}
// blocks until killed
err = kubernetesProxy.Run()
if err != nil {
fmt.Fprintf(os.Stderr, "Error running proxy: %s", err)
os.Exit(1)
}
return nil
},
}
cmd.Args = cobra.NoArgs
// This is identical to what `kubectl proxy --help` reports, `--port 0` indicates a random port.
cmd.PersistentFlags().IntVarP(&options.dashboardProxyPort, "port", "p", options.dashboardProxyPort, "The port on which to run the proxy (when set to 0, a random port will be used)")
cmd.PersistentFlags().StringVar(&options.dashboardShow, "show", options.dashboardShow, "Open a dashboard in a browser or show URLs in the CLI (one of: linkerd, grafana, url)")
cmd.PersistentFlags().BoolVar(&options.wait, "wait", false, "Wait for dashboard to become available if it's not available when the command is run")
return cmd
}