-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdashboard.go
427 lines (379 loc) · 11.3 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
Copyright (C) 2022-2024 ApeCloud Co., Ltd
This file is part of KubeBlocks project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package dashboard
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericiooptions"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/portforward"
"k8s.io/client-go/transport/spdy"
cmdpf "k8s.io/kubectl/pkg/cmd/portforward"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util/templates"
"k8s.io/utils/pointer"
"github.com/apecloud/kbcli/pkg/printer"
"github.com/apecloud/kbcli/pkg/util"
)
// kb support dashboard name
const (
grafanaAddonName = "kubeblocks-grafana"
bytebaseAddonName = "bytebase"
nyancatAddonName = "kubeblocks-nyancat"
prometheusAlertManager = "kubeblocks-prometheus-alertmanager"
prometheusServer = "kubeblocks-prometheus-server"
pyroscopeServer = "kubeblocks-pyroscope-server"
jupyterHubAddon = "jupyter-hub"
jupyterNoteBookAddon = "jupyter-notebook"
minio = "minio"
)
const (
podRunningTimeoutFlag = "pod-running-timeout"
defaultPodExecTimeout = 60 * time.Second
lokiAddonName = "kubeblocks-logs"
lokiGrafanaDirect = "container-logs"
localAdd = "127.0.0.1"
)
type dashboard struct {
Name string
AddonName string
Port string
TargetPort string
Namespace string
CreationTime string
// Label used to get the service
Label string
}
var (
listExample = templates.Examples(`
# List all dashboards
kbcli dashboard list
`)
openExample = templates.Examples(`
# Open a dashboard, such as kube-grafana
kbcli dashboard open kubeblocks-grafana
# Open a dashboard with a specific local port
kbcli dashboard open kubeblocks-grafana --port 8080
# for dashboard kubeblocks-grafana, support to direct the specified dashboard type
# now we support mysql,mongodb,postgresql,redis,weaviate,kafka,cadvisor,jmx and node
kbcli dashboard open kubeblocks-grafana mysql
`)
// we do not use the default port to port-forward to avoid conflict with other services
dashboards = [...]*dashboard{
{
Name: grafanaAddonName,
AddonName: "kb-addon-grafana",
Label: "app.kubernetes.io/instance=kb-addon-grafana,app.kubernetes.io/name=grafana",
TargetPort: "13000",
},
{
Name: prometheusAlertManager,
AddonName: "kb-addon-prometheus-alertmanager",
Label: "app=prometheus,component=alertmanager,release=kb-addon-prometheus",
TargetPort: "19093",
},
{
Name: prometheusServer,
AddonName: "kb-addon-prometheus-server",
Label: "app=prometheus,component=server,release=kb-addon-prometheus",
TargetPort: "19090",
},
{
Name: nyancatAddonName,
AddonName: "kb-addon-nyancat",
Label: "app.kubernetes.io/instance=kb-addon-nyancat",
TargetPort: "8087",
},
{
Name: lokiAddonName,
AddonName: "kb-addon-loki",
Label: "app.kubernetes.io/instance=kb-addon-loki",
TargetPort: "13100",
},
{
Name: pyroscopeServer,
AddonName: "kb-addon-pyroscope-server",
Label: "app.kubernetes.io/instance=kb-addon-pyroscope-server,app.kubernetes.io/name=pyroscope",
TargetPort: "14040",
}, {
Name: bytebaseAddonName,
AddonName: "bytebase-entrypoint",
Label: "app=bytebase",
TargetPort: "18080",
},
{
Name: jupyterHubAddon,
AddonName: "proxy-public",
Label: "app=jupyterhub",
TargetPort: "18081",
},
{
Name: jupyterNoteBookAddon,
AddonName: "jupyter-notebook",
Label: " app.kubernetes.io/instance=kb-addon-jupyter-notebook",
TargetPort: "18888",
},
{
Name: minio,
AddonName: "kb-addon-minio",
Label: "app.kubernetes.io/instance=kb-addon-minio",
TargetPort: "9001",
Port: "9001",
},
}
)
type listOptions struct {
genericiooptions.IOStreams
factory cmdutil.Factory
client *kubernetes.Clientset
}
func newListOptions(f cmdutil.Factory, streams genericiooptions.IOStreams) *listOptions {
return &listOptions{
factory: f,
IOStreams: streams,
}
}
// NewDashboardCmd creates the dashboard command
func NewDashboardCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Use: "dashboard",
Short: "List and open the KubeBlocks dashboards.",
}
// add subcommands
cmd.AddCommand(
newListCmd(f, streams),
newOpenCmd(f, streams),
)
return cmd
}
func newListCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
o := newListOptions(f, streams)
cmd := &cobra.Command{
Use: "list",
Short: "List all dashboards.",
Example: listExample,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
util.CheckErr(o.complete())
util.CheckErr(o.run())
},
}
return cmd
}
func (o *listOptions) complete() error {
var err error
o.client, err = o.factory.KubernetesClientSet()
return err
}
// get all dashboard service and print
func (o *listOptions) run() error {
if err := getDashboardInfo(o.client); err != nil {
return err
}
return printTable(o.Out)
}
func printTable(out io.Writer) error {
tbl := printer.NewTablePrinter(out)
tbl.SetHeader("NAME", "NAMESPACE", "PORT", "CREATED-TIME")
for _, d := range dashboards {
if d.Namespace == "" {
continue
}
tbl.AddRow(d.Name, d.Namespace, d.TargetPort, d.CreationTime)
}
tbl.Print()
return nil
}
type openOptions struct {
factory cmdutil.Factory
genericiooptions.IOStreams
portForwardOptions *cmdpf.PortForwardOptions
name string
localPort string
}
func newOpenOptions(f cmdutil.Factory, streams genericiooptions.IOStreams) *openOptions {
return &openOptions{
factory: f,
IOStreams: streams,
portForwardOptions: &cmdpf.PortForwardOptions{
PortForwarder: &defaultPortForwarder{streams},
},
}
}
func newOpenCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
o := newOpenOptions(f, streams)
cmd := &cobra.Command{
Use: "open NAME [DASHBOARD-TYPE] [--port PORT]",
Short: "Open one dashboard.",
Example: openExample,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 1 && args[0] == supportDirectDashboard {
var name []string
for i := range availableTypes {
if strings.HasPrefix(availableTypes[i], toComplete) {
name = append(name, availableTypes[i])
}
}
return name, cobra.ShellCompDirectiveNoFileComp
}
var names []string
for _, d := range dashboards {
names = append(names, d.Name)
}
return names, cobra.ShellCompDirectiveNoFileComp
},
Run: func(cmd *cobra.Command, args []string) {
util.CheckErr(o.complete(cmd, args))
util.CheckErr(o.run())
},
}
cmd.Flags().StringVar(&o.localPort, "port", "", "dashboard local port")
cmd.Flags().Duration(podRunningTimeoutFlag, defaultPodExecTimeout,
"The time (like 5s, 2m, or 3h, higher than zero) to wait for at least one pod is running")
return cmd
}
func (o *openOptions) complete(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("missing dashborad name")
}
o.name = args[0]
client, err := o.factory.KubernetesClientSet()
if err != nil {
return err
}
if err = getDashboardInfo(client); err != nil {
return err
}
dashName := o.name
// opening loki dashboard redirects to grafana dashboard
if o.name == lokiAddonName {
dashName = grafanaAddonName
}
dash := getDashboardByName(dashName)
if dash == nil {
return fmt.Errorf("failed to find dashboard \"%s\", run \"kbcli dashboard list\" to list all dashboards", o.name)
}
if dash.Name == supportDirectDashboard && len(args) > 1 {
clusterType = args[1]
}
if o.localPort == "" {
if o.name == lokiAddonName {
// revert the target port for loki dashboard
o.localPort = getDashboardByName(lokiAddonName).TargetPort
} else {
o.localPort = dash.TargetPort
}
}
pfArgs := []string{fmt.Sprintf("svc/%s", dash.AddonName), fmt.Sprintf("%s:%s", o.localPort, dash.Port)}
o.portForwardOptions.Namespace = dash.Namespace
o.portForwardOptions.Address = []string{localAdd}
return o.portForwardOptions.Complete(newFactory(dash.Namespace), cmd, pfArgs)
}
func (o *openOptions) run() error {
url := fmt.Sprintf("http://%s:%s", localAdd, o.localPort)
if o.name == "kubeblocks-grafana" {
err := buildGrafanaDirectURL(&url, clusterType)
if err != nil {
return err
}
}
// customized by loki
if o.name == lokiAddonName {
err := buildGrafanaDirectURL(&url, lokiGrafanaDirect)
if err != nil {
return err
}
}
go func() {
<-o.portForwardOptions.ReadyChannel
fmt.Fprintf(o.Out, "Forward successfully! Opening browser ...\n")
if err := util.OpenBrowser(url); err != nil {
fmt.Fprintf(o.ErrOut, "Failed to open browser: %v", err)
}
}()
return o.portForwardOptions.RunPortForward()
}
func getDashboardByName(name string) *dashboard {
for i, d := range dashboards {
if d.Name == name {
return dashboards[i]
}
}
return nil
}
func getDashboardInfo(client *kubernetes.Clientset) error {
getSvcs := func(client *kubernetes.Clientset, label string) (*corev1.ServiceList, error) {
return client.CoreV1().Services(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{
LabelSelector: label,
})
}
for _, d := range dashboards {
var svc *corev1.Service
// get all services that match the label
svcs, err := getSvcs(client, d.Label)
if err != nil {
return err
}
// find the dashboard service
for i, s := range svcs.Items {
if s.Name == d.AddonName {
svc = &svcs.Items[i]
break
}
}
if svc == nil {
continue
}
// fill dashboard information
d.Namespace = svc.Namespace
d.CreationTime = util.TimeFormat(&svc.CreationTimestamp)
// if port is not specified, use the first port of the service
if len(svc.Spec.Ports) > 0 && d.Port == "" {
d.Port = fmt.Sprintf("%d", svc.Spec.Ports[0].Port)
if d.TargetPort == "" {
d.TargetPort = svc.Spec.Ports[0].TargetPort.String()
}
}
}
return nil
}
func newFactory(namespace string) cmdutil.Factory {
cf := util.NewConfigFlagNoWarnings()
cf.Namespace = pointer.String(namespace)
return cmdutil.NewFactory(cf)
}
type defaultPortForwarder struct {
genericiooptions.IOStreams
}
func (f *defaultPortForwarder) ForwardPorts(method string, url *url.URL, opts cmdpf.PortForwardOptions) error {
transport, upgrader, err := spdy.RoundTripperFor(opts.Config)
if err != nil {
return err
}
dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, method, url)
pf, err := portforward.NewOnAddresses(dialer, opts.Address, opts.Ports, opts.StopChannel, opts.ReadyChannel, f.Out, f.ErrOut)
if err != nil {
return err
}
return pf.ForwardPorts()
}