-
Notifications
You must be signed in to change notification settings - Fork 137
ROSAENG-14107: new 'hcp-dashboard' sub-command for 'osdctl rhobs' #920
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| package rhobs | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/pkg/browser" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func newCmdHcpDashboard() *cobra.Command { | ||
| var rhobsCell string | ||
| var isOpeningGrafanaUrl bool | ||
| var dashboardName string | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "hcp-dashboard [dashboard-name]", | ||
| Short: "Get the HCP dashboard URL for a given HCP cluster", | ||
| Long: "Get the HCP dashboard URL for a given HCP cluster. " + | ||
| "The dashboard name is optional and defaults to the hosted cluster dashboard. " + | ||
| "Allowed values for the dashboard name are: " + strings.Join(GetAllowedGrafanaDashboardsShortNames(), ", ") + ". " + | ||
| "The URL of the RHOBS cell(s) to use can be specified with the --rhobs-cell option, " + | ||
| "but it is usually more convenient to specify the cluster with the --cluster-id option and let the command figure out the RHOBS cell(s) to use. " + | ||
| "Note that the --rhobs-cell option is not working with all dashboards and cannot be used together with the --cluster-id option.", | ||
| Args: cobra.MaximumNArgs(1), | ||
| SilenceErrors: true, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| dashboardName = defaultGrafanaDashboardShortName | ||
| if len(args) == 1 { | ||
| dashboardName = args[0] | ||
| } | ||
|
|
||
| grafanaDashboard := GetGrafanaDashboardForShortName(dashboardName) | ||
| if grafanaDashboard == nil { | ||
| return fmt.Errorf("invalid dashboard name: %s", dashboardName) | ||
| } | ||
|
|
||
| rhobsCells := []string{} | ||
| if rhobsCell != "" { | ||
| if cmd.Parent().Flags().Changed("cluster-id") { | ||
| return fmt.Errorf("--rhobs-cell and --cluster-id options cannot be used together") | ||
| } | ||
|
|
||
| rhobsCells = strings.Split(rhobsCell, ",") | ||
| if len(rhobsCells) > 2 { | ||
| return fmt.Errorf("value passed to --rhobs-cell must have at most 2 elements: %s", rhobsCell) | ||
| } | ||
| } | ||
|
|
||
| cmd.SilenceUsage = true | ||
|
|
||
| var metricsRhobsFetcher, logsRhobsFetcher *RhobsFetcher | ||
| var err error | ||
|
|
||
| if len(rhobsCells) > 0 { | ||
| metricsRhobsFetcher, err = CreateRhobsFetcherFromCell(rhobsCells[0]) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if len(rhobsCells) == 2 { | ||
| logsRhobsFetcher, err = CreateRhobsFetcherFromCell(rhobsCells[1]) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } else { | ||
| logsRhobsFetcher = metricsRhobsFetcher | ||
| } | ||
| } else { | ||
| metricsRhobsFetcher, err = CreateRhobsFetcher(commonOptions.clusterId, RhobsFetchForMetrics, commonOptions.hiveOcmUrl) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| logsRhobsFetcher, err = CreateRhobsFetcher(commonOptions.clusterId, RhobsFetchForLogs, commonOptions.hiveOcmUrl) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
Comment on lines
+69
to
+77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Description: Check if CreateRhobsFetcher performs network calls or expensive operations
# Find the implementation
ast-grep --pattern $'func CreateRhobsFetcher($$$) $$$'
# Look for HTTP calls, OCM client usage, or heavy operations
rg -n -C5 'func CreateRhobsFetcher|http\.|ocm\.|Get.*Cluster' --type=go -g '**/rhobs/**'Repository: openshift/osdctl Length of output: 11631 Avoid redundant OCM/cluster/config initialization when creating metrics vs logs RHOBS fetchers
🤖 Prompt for AI Agents
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, maybe there should be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Look at the CreateRhobsFetcher implementation to understand shared vs diverging parts
ast-grep --pattern $'func CreateRhobsFetcher($$$) ($$$) {
$$$
}'
# Also check RhobsFetcher struct definition
ast-grep --pattern $'type RhobsFetcher struct {
$$$
}'
# Check what RhobsFetchForMetrics / RhobsFetchForLogs diverge on
rg -n 'RhobsFetchFor|fetchType|FetchType|metricsCell|logsCell|IsHostedCluster' --type=goLength of output: 6218
A // CreateRhobsFetchers creates both a metrics and a logs RhobsFetcher for the
// given cluster, sharing the OCM connection and cluster-resolution work.
func CreateRhobsFetchers(clusterKey string, hiveOcmUrl string) (metricsFetcher, logsFetcher *RhobsFetcher, err error) {
ocmConn, err := ocmutils.CreateConnection()
if err != nil {
return nil, nil, err
}
defer ocmConn.Close()
cluster, err := ocmutils.GetCluster(ocmConn, clusterKey)
if err != nil {
return nil, nil, err
}
var monitoredClusterId string
isHcp := cluster.Hypershift().Enabled()
isMC := false
if isHcp {
managementCluster, err := ocmutils.GetManagementCluster(cluster.ID())
if err != nil {
return nil, nil, fmt.Errorf("failed to retrieve management cluster for cluster '%s': %v", cluster.ID(), err)
}
monitoredClusterId = managementCluster.ID()
} else {
monitoredClusterId = cluster.ID()
isMC, err = ocmutils.IsManagementCluster(cluster.ID())
if err != nil {
return nil, nil, fmt.Errorf("failed to determine if cluster '%s' is a management cluster: %v", cluster.ID(), err)
}
}
// Cells CAN differ between metrics and logs — resolve both.
metricsCell, err := getRhobsCell(monitoredClusterId, ocmConn, RhobsFetchForMetrics, hiveOcmUrl)
if err != nil {
return nil, nil, err
}
logsCell, err := getRhobsCell(monitoredClusterId, ocmConn, RhobsFetchForLogs, hiveOcmUrl)
if err != nil {
return nil, nil, err
}
// Reload config once after backplane may have mutated it.
if err = osdctlConfig.EnsureConfigFile(); err != nil {
return nil, nil, fmt.Errorf("failed to reload osdctl config: %v", err)
}
base := RhobsFetcher{
clusterId: cluster.ID(),
clusterExternalId: cluster.ExternalID(),
clusterName: cluster.Name(),
IsHostedCluster: isHcp,
isManagementCluster: isMC,
ocmEnvName: ocmutils.GetCurrentOCMEnv(ocmConn),
}
mf := base
mf.RhobsCell = metricsCell
lf := base
lf.RhobsCell = logsCell
return &mf, &lf, nil
}
Would you like me to open a follow-up GitHub issue to track this refactor, or plan the code changes directly? ✏️ Learnings added
🧠 Learnings used |
||
|
|
||
| grafanaUrl, err := GetGrafanaDashboardUrl(metricsRhobsFetcher, logsRhobsFetcher, grafanaDashboard) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to compute Grafana URL: %v", err) | ||
| } | ||
| if isOpeningGrafanaUrl { | ||
| err = browser.OpenURL(grafanaUrl) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to open Grafana URL in browser: %v", err) | ||
| } | ||
| } else { | ||
| fmt.Println(grafanaUrl) | ||
| } | ||
|
|
||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringVarP(&rhobsCell, "rhobs-cell", "c", "", "RHOBS cell URL - "+ | ||
| "for instance: https://us-east-1-0.rhobs.api.stage.openshift.com - "+ | ||
| "use a comma to separate the RHOBS cell to use for metrics from the logs RHOBS cell if they are different - "+ | ||
| "this option is not working with all dashboards - exclusive with --cluster-id") | ||
| cmd.Flags().BoolVarP(&isOpeningGrafanaUrl, "browser", "b", false, "Open in the URL in the default browser") | ||
|
|
||
| return cmd | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.