From 906011287b23523d59ddaaf3f7a84b95d6c18448 Mon Sep 17 00:00:00 2001 From: Smilencer Date: Thu, 31 Mar 2022 17:40:29 +0800 Subject: [PATCH] cluster: display grafana URL on the top (#1819) --- pkg/cluster/manager/display.go | 20 +++++++++ pkg/cluster/manager/display_test.go | 66 +++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 pkg/cluster/manager/display_test.go diff --git a/pkg/cluster/manager/display.go b/pkg/cluster/manager/display.go index 5ebac152c4..2ddb9c0ce4 100644 --- a/pkg/cluster/manager/display.go +++ b/pkg/cluster/manager/display.go @@ -215,6 +215,13 @@ func (m *Manager) Display(name string, opt operator.Options) error { } } + if m.logger.GetDisplayMode() != logprinter.DisplayModeJSON { + urls, exist := getGrafanaURLStr(clusterInstInfos) + if exist { + fmt.Printf("Grafana URL: %s\n", cyan.Sprintf("%s", urls)) + } + } + if m.logger.GetDisplayMode() == logprinter.DisplayModeJSON { d, err := json.MarshalIndent(j, "", " ") if err != nil { @@ -254,6 +261,19 @@ func (m *Manager) Display(name string, opt operator.Options) error { return nil } +func getGrafanaURLStr(clusterInstInfos []InstInfo) (result string, exist bool) { + var grafanaURLs []string + for _, instance := range clusterInstInfos { + if instance.Role == "grafana" { + grafanaURLs = append(grafanaURLs, fmt.Sprintf("http://%s:%d", instance.Host, instance.Port)) + } + } + if len(grafanaURLs) == 0 { + return "", false + } + return strings.Join(grafanaURLs, ","), true +} + // DisplayTiKVLabels display cluster tikv labels func (m *Manager) DisplayTiKVLabels(name string, opt operator.Options) error { if err := clusterutil.ValidateClusterNameOrError(name); err != nil { diff --git a/pkg/cluster/manager/display_test.go b/pkg/cluster/manager/display_test.go new file mode 100644 index 0000000000..3aaf94edbe --- /dev/null +++ b/pkg/cluster/manager/display_test.go @@ -0,0 +1,66 @@ +// Copyright 2020 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// See the License for the specific language governing permissions and +// limitations under the License. + +package manager + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetGrafanaURLStr(t *testing.T) { + var str string + var exist bool + + str, exist = getGrafanaURLStr([]InstInfo{ + { + Role: "grafana", + Port: 3000, + Host: "127.0.0.1", + }, { + Role: "others", + Port: 3000, + Host: "127.0.0.1", + }, + }) + assert.Equal(t, exist, true) + assert.Equal(t, "http://127.0.0.1:3000", str) + + str, exist = getGrafanaURLStr([]InstInfo{ + { + Role: "grafana", + Port: 3000, + Host: "127.0.0.1", + }, { + Role: "grafana", + Port: 3000, + Host: "127.0.0.2", + }, + }) + assert.Equal(t, exist, true) + assert.Equal(t, "http://127.0.0.1:3000,http://127.0.0.2:3000", str) + + _, exist = getGrafanaURLStr([]InstInfo{ + { + Role: "others", + Port: 3000, + Host: "127.0.0.1", + }, { + Role: "others", + Port: 3000, + Host: "127.0.0.2", + }, + }) + assert.Equal(t, exist, false) +}