-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathdashboard.go
More file actions
146 lines (120 loc) · 4.46 KB
/
dashboard.go
File metadata and controls
146 lines (120 loc) · 4.46 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package cmd
import (
"context"
"fmt"
"os"
"github.com/pkg/browser"
healthcheckPb "github.com/runconduit/conduit/controller/gen/common/healthcheck"
pb "github.com/runconduit/conduit/controller/gen/public"
"github.com/runconduit/conduit/pkg/k8s"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
// These constants are used by the `show` flag.
const (
// showConduit opens the Conduit dashboard in a web browser (default).
showConduit = "conduit"
// 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
}
func newDashboardOptions() *dashboardOptions {
return &dashboardOptions{
dashboardProxyPort: 0,
dashboardShow: "conduit",
}
}
func newCmdDashboard() *cobra.Command {
options := newDashboardOptions()
cmd := &cobra.Command{
Use: "dashboard [flags]",
Short: "Open the Conduit 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 != showConduit && 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, showConduit, 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)
}
client, err := newPublicAPIClient()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to initialize Conduit API client: %+v\n", err)
os.Exit(1)
}
dashboardAvailable, err := isDashboardAvailable(client)
if err != nil {
log.Debugf("Error checking dashboard availability: %s", err)
}
if err != nil || !dashboardAvailable {
fmt.Fprintf(os.Stderr, "Conduit is not running in the \"%s\" namespace\n", controlPlaneNamespace)
fmt.Fprintf(os.Stderr, "Install with: conduit install --conduit-namespace %s | kubectl apply -f -\n", controlPlaneNamespace)
os.Exit(1)
}
fmt.Printf("Conduit dashboard available at:\n%s\n", url.String())
fmt.Printf("Grafana dashboard available at:\n%s\n", grafanaUrl.String())
switch options.dashboardShow {
case showConduit:
fmt.Println("Opening Conduit dashboard in the default browser")
err = browser.OpenURL(url.String())
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to open Conduit 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: conduit, grafana, url)")
return cmd
}
func isDashboardAvailable(client pb.ApiClient) (bool, error) {
res, err := client.SelfCheck(context.Background(), &healthcheckPb.SelfCheckRequest{})
if err != nil {
return false, err
}
for _, result := range res.Results {
if result.Status != healthcheckPb.CheckStatus_OK {
return false, nil
}
}
return true, nil
}