Skip to content

Commit

Permalink
cluster: display grafana URL on the top (#1819)
Browse files Browse the repository at this point in the history
  • Loading branch information
Smityz authored Mar 31, 2022
1 parent 514d2ae commit 9060112
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pkg/cluster/manager/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
66 changes: 66 additions & 0 deletions pkg/cluster/manager/display_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 9060112

Please sign in to comment.