Skip to content

Commit cb6e6de

Browse files
committed
Auto open dynatrace dashboard when requesting it
1 parent 632ee86 commit cb6e6de

File tree

7 files changed

+36
-53
lines changed

7 files changed

+36
-53
lines changed

cmd/dynatrace/cluster.go

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
package dynatrace
22

33
import (
4-
"context"
54
"fmt"
6-
"net/url"
75
"strings"
86

9-
"github.com/Dynatrace/dynatrace-operator/src/api/v1beta1"
10-
"github.com/Dynatrace/dynatrace-operator/src/api/v1beta1/dynakube"
11-
"k8s.io/apimachinery/pkg/runtime"
12-
"sigs.k8s.io/controller-runtime/pkg/client"
13-
14-
"github.com/openshift/osdctl/pkg/k8s"
157
ocmutils "github.com/openshift/osdctl/pkg/utils"
168
)
179

@@ -29,7 +21,7 @@ type HCPCluster struct {
2921
serviceClusterName string
3022
}
3123

32-
var ErrUnsupportedCluster = fmt.Errorf("Not an HCP or MC Cluster")
24+
var ErrUnsupportedCluster = fmt.Errorf("not an HCP or MC Cluster")
3325

3426
func FetchClusterDetails(clusterKey string) (hcpCluster HCPCluster, error error) {
3527
hcpCluster = HCPCluster{}
@@ -58,7 +50,7 @@ func FetchClusterDetails(clusterKey string) (hcpCluster HCPCluster, error error)
5850
hcpCluster.managementClusterName = cluster.Name()
5951
url, err := ocmutils.GetDynatraceURLFromLabel(hcpCluster.managementClusterID)
6052
if err != nil {
61-
return HCPCluster{}, fmt.Errorf("the Dynatrace Environemnt URL could not be determined. \nPlease refer the SOP to determine the correct Dyntrace Tenant URL- https://github.com/openshift/ops-sop/tree/master/dynatrace#what-environments-are-there \n\nError Details - %s", err)
53+
return HCPCluster{}, fmt.Errorf("the Dynatrace Environment URL could not be determined. \nPlease refer the SOP to determine the correct Dynatrace Tenant URL- https://github.com/openshift/ops-sop/tree/master/dynatrace#what-environments-are-there \n\nError Details - %s", err)
6254
}
6355
hcpCluster.DynatraceURL = url
6456
return hcpCluster, nil
@@ -96,35 +88,3 @@ func FetchClusterDetails(clusterKey string) (hcpCluster HCPCluster, error error)
9688

9789
return hcpCluster, nil
9890
}
99-
100-
func GetDynatraceURLFromManagementCluster(clusterID string) (string, error) {
101-
// Register v1beta1 for DynaKube
102-
scheme := runtime.NewScheme()
103-
if err := v1beta1.AddToScheme(scheme); err != nil {
104-
return "", err
105-
}
106-
c, err := k8s.New(clusterID, client.Options{Scheme: scheme})
107-
if err != nil {
108-
return "", err
109-
}
110-
111-
// Fetch the DynaKube Resource
112-
var dynaKubeList dynakube.DynaKubeList
113-
err = c.List(context.TODO(),
114-
&dynaKubeList,
115-
&client.ListOptions{Namespace: "dynatrace"})
116-
if err != nil {
117-
return "", err
118-
}
119-
if len(dynaKubeList.Items) == 0 {
120-
return "", fmt.Errorf("could not locate dynaKube resource on the cluster")
121-
}
122-
123-
// Parse the DT URL
124-
DTApiURL, err := url.Parse(dynaKubeList.Items[0].Spec.APIURL)
125-
if err != nil {
126-
return "", err
127-
}
128-
DTURL := fmt.Sprintf("%s://%s", DTApiURL.Scheme, DTApiURL.Host)
129-
return DTURL, nil
130-
}
Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package dynatrace
22

33
import (
44
"fmt"
5+
"os/exec"
6+
"runtime"
57

68
"github.com/spf13/cobra"
79
cmdutil "k8s.io/kubectl/pkg/cmd/util"
@@ -12,11 +14,32 @@ var (
1214
clusterId string
1315
)
1416

17+
// openBrowser opens the specified URL in the default browser
18+
func openBrowser(url string) error {
19+
var cmd string
20+
var args []string
21+
22+
switch runtime.GOOS {
23+
case "linux":
24+
cmd = "xdg-open"
25+
case "windows":
26+
cmd = "rundll32"
27+
args = []string{"url.dll,FileProtocolHandler"}
28+
case "darwin":
29+
cmd = "open"
30+
default:
31+
return fmt.Errorf("unsupported platform")
32+
}
33+
34+
args = append(args, url)
35+
return exec.Command(cmd, args...).Start()
36+
}
37+
1538
func newCmdDashboard() *cobra.Command {
1639
urlCmd := &cobra.Command{
1740
Use: "dashboard --cluster-id CLUSTER_ID",
1841
Aliases: []string{"dash"},
19-
Short: "Get the Dyntrace Cluster Overview Dashboard for a given MC or HCP cluster",
42+
Short: "Get the Dynatrace Cluster Overview Dashboard for a given MC or HCP cluster",
2043
DisableAutoGenTag: true,
2144
Run: func(cmd *cobra.Command, args []string) {
2245
// We need the Dynatrace URL
@@ -42,6 +65,12 @@ func newCmdDashboard() *cobra.Command {
4265
// Tell the user
4366
dashUrl := hcpCluster.DynatraceURL + "ui/apps/dynatrace.dashboards/dashboard/" + id + "#vfilter__id=" + hcpCluster.externalID
4467
fmt.Printf("\n\nDashboard URL:\n %s\n", dashUrl)
68+
69+
// Open the dashboard in the default browser
70+
fmt.Println("\nOpening dashboard in your browser...")
71+
if err := openBrowser(dashUrl); err != nil {
72+
fmt.Printf("Could not open browser automatically: %s\n", err)
73+
}
4574
},
4675
}
4776

docs/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
- `list` - List the cost of each Account/OU under given OU
8787
- `reconcile` - Checks if there's a cost category for every OU. If an OU is missing a cost category, creates the cost category
8888
- `dynatrace` - Dynatrace related utilities
89-
- `dashboard --cluster-id CLUSTER_ID` - Get the Dyntrace Cluster Overview Dashboard for a given MC or HCP cluster
89+
- `dashboard --cluster-id CLUSTER_ID` - Get the Dynatrace Cluster Overview Dashboard for a given MC or HCP cluster
9090
- `gather-logs --cluster-id <cluster-identifier>` - Gather all Pod logs and Application event from HCP
9191
- `logs --cluster-id <cluster-identifier>` - Fetch logs from Dynatrace
9292
- `url --cluster-id <cluster-identifier>` - Get the Dynatrace Tenant URL for a given MC or HCP cluster
@@ -2620,7 +2620,7 @@ osdctl dynatrace [flags]
26202620

26212621
### osdctl dynatrace dashboard
26222622

2623-
Get the Dyntrace Cluster Overview Dashboard for a given MC or HCP cluster
2623+
Get the Dynatrace Cluster Overview Dashboard for a given MC or HCP cluster
26242624

26252625
```
26262626
osdctl dynatrace dashboard --cluster-id CLUSTER_ID [flags]

docs/osdctl_dynatrace.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Dynatrace related utilities
2626
### SEE ALSO
2727

2828
* [osdctl](osdctl.md) - OSD CLI
29-
* [osdctl dynatrace dashboard](osdctl_dynatrace_dashboard.md) - Get the Dyntrace Cluster Overview Dashboard for a given MC or HCP cluster
29+
* [osdctl dynatrace dashboard](osdctl_dynatrace_dashboard.md) - Get the Dynatrace Cluster Overview Dashboard for a given MC or HCP cluster
3030
* [osdctl dynatrace gather-logs](osdctl_dynatrace_gather-logs.md) - Gather all Pod logs and Application event from HCP
3131
* [osdctl dynatrace logs](osdctl_dynatrace_logs.md) - Fetch logs from Dynatrace
3232
* [osdctl dynatrace url](osdctl_dynatrace_url.md) - Get the Dynatrace Tenant URL for a given MC or HCP cluster

docs/osdctl_dynatrace_dashboard.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## osdctl dynatrace dashboard
22

3-
Get the Dyntrace Cluster Overview Dashboard for a given MC or HCP cluster
3+
Get the Dynatrace Cluster Overview Dashboard for a given MC or HCP cluster
44

55
```
66
osdctl dynatrace dashboard --cluster-id CLUSTER_ID [flags]

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ go 1.25.7
55
require (
66
cloud.google.com/go/compute v1.37.0
77
github.com/AlecAivazis/survey/v2 v2.3.7
8-
github.com/Dynatrace/dynatrace-operator v0.14.2
98
github.com/Masterminds/semver/v3 v3.4.0
109
github.com/PagerDuty/go-pagerduty v1.8.0
1110
github.com/andygrunwald/go-jira v1.17.0
@@ -148,7 +147,6 @@ require (
148147
github.com/google/btree v1.1.3 // indirect
149148
github.com/google/gnostic-models v0.6.9 // indirect
150149
github.com/google/go-cmp v0.7.0 // indirect
151-
github.com/google/go-containerregistry v0.20.3 // indirect
152150
github.com/google/go-querystring v1.1.0 // indirect
153151
github.com/google/gofuzz v1.2.0 // indirect
154152
github.com/google/s2a-go v0.1.9 // indirect

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ github.com/AlecAivazis/survey/v2 v2.3.7/go.mod h1:xUTIdE4KCOIjsBAE1JYsUPoCqYdZ1r
2121
github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c h1:udKWzYgxTojEKWjV8V+WSxDXJ4NFATAsZjh8iIbsQIg=
2222
github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
2323
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
24-
github.com/Dynatrace/dynatrace-operator v0.14.2 h1:eETVaqBBFM5MG+OxoPsB13ehNfsnIGCp784pJVZtwQg=
25-
github.com/Dynatrace/dynatrace-operator v0.14.2/go.mod h1:MEyWc7w33UuoIxpCbZArtutDQSGVAfo0Vkibc5BUFMQ=
2624
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
2725
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
2826
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
@@ -268,8 +266,6 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
268266
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
269267
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
270268
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
271-
github.com/google/go-containerregistry v0.20.3 h1:oNx7IdTI936V8CQRveCjaxOiegWwvM7kqkbXTpyiovI=
272-
github.com/google/go-containerregistry v0.20.3/go.mod h1:w00pIgBRDVUDFM6bq+Qx8lwNWK+cxgCuX1vd3PIBDNI=
273269
github.com/google/go-github/v63 v63.0.0 h1:13xwK/wk9alSokujB9lJkuzdmQuVn2QCPeck76wR3nE=
274270
github.com/google/go-github/v63 v63.0.0/go.mod h1:IqbcrgUmIcEaioWrGYei/09o+ge5vhffGOcxrO0AfmA=
275271
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=

0 commit comments

Comments
 (0)