Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cluster: add support for displaying the grafana URLs in JSON output. #2041

Merged
merged 3 commits into from
Oct 17, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions pkg/cluster/manager/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,17 @@ type LabelInfo struct {

// ClusterMetaInfo hold the structure for the JSON output of the dashboard info
type ClusterMetaInfo struct {
ClusterType string `json:"cluster_type"`
ClusterName string `json:"cluster_name"`
ClusterVersion string `json:"cluster_version"`
DeployUser string `json:"deploy_user"`
SSHType string `json:"ssh_type"`
TLSEnabled bool `json:"tls_enabled"`
TLSCACert string `json:"tls_ca_cert,omitempty"`
TLSClientCert string `json:"tls_client_cert,omitempty"`
TLSClientKey string `json:"tls_client_key,omitempty"`
DashboardURL string `json:"dashboard_url,omitempty"`
ClusterType string `json:"cluster_type"`
ClusterName string `json:"cluster_name"`
ClusterVersion string `json:"cluster_version"`
DeployUser string `json:"deploy_user"`
SSHType string `json:"ssh_type"`
TLSEnabled bool `json:"tls_enabled"`
TLSCACert string `json:"tls_ca_cert,omitempty"`
TLSClientCert string `json:"tls_client_cert,omitempty"`
TLSClientKey string `json:"tls_client_key,omitempty"`
DashboardURL string `json:"dashboard_url,omitempty"`
GrafanaURLS []string `json:"grafana_urls,omitempty"`
}

// JSONOutput holds the structure for the JSON output of `tiup cluster display --json`
Expand Down Expand Up @@ -139,6 +140,7 @@ func (m *Manager) Display(dopt DisplayOption, opt operator.Options) error {
"", // Client Cert
"", // Client Key
"",
nil,
},
InstanceInfos: clusterInstInfos,
}
Expand Down Expand Up @@ -237,7 +239,12 @@ func (m *Manager) Display(dopt DisplayOption, opt operator.Options) error {
}
}

if m.logger.GetDisplayMode() != logprinter.DisplayModeJSON {
if m.logger.GetDisplayMode() == logprinter.DisplayModeJSON {
grafanaURLs := getGrafanaURL(clusterInstInfos)
if len(grafanaURLs) != 0 {
j.ClusterMetaInfo.GrafanaURLS = grafanaURLs
}
} else {
urls, exist := getGrafanaURLStr(clusterInstInfos)
if exist {
fmt.Printf("Grafana URL: %s\n", cyan.Sprintf("%s", urls))
Expand Down Expand Up @@ -283,13 +290,18 @@ func (m *Manager) Display(dopt DisplayOption, opt operator.Options) error {
return nil
}

func getGrafanaURLStr(clusterInstInfos []InstInfo) (result string, exist bool) {
func getGrafanaURL(clusterInstInfos []InstInfo) (result []string) {
var grafanaURLs []string
for _, instance := range clusterInstInfos {
if instance.Role == "grafana" {
grafanaURLs = append(grafanaURLs, fmt.Sprintf("http://%s:%d", instance.Host, instance.Port))
}
}
return grafanaURLs
}

func getGrafanaURLStr(clusterInstInfos []InstInfo) (result string, exist bool) {
grafanaURLs := getGrafanaURL(clusterInstInfos)
if len(grafanaURLs) == 0 {
return "", false
}
Expand Down Expand Up @@ -329,6 +341,7 @@ func (m *Manager) DisplayTiKVLabels(dopt DisplayOption, opt operator.Options) er
"", // Client Cert
"", // Client Key
"",
nil,
},
}

Expand Down