Skip to content

Commit

Permalink
Merge pull request #509 from devppratik/update-dynatrace-url
Browse files Browse the repository at this point in the history
OSD-20697: Fetch Dynatrace URL via API Call
  • Loading branch information
openshift-merge-bot[bot] authored Feb 7, 2024
2 parents d87e5cf + cd375bf commit c163a14
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
14 changes: 9 additions & 5 deletions cmd/cluster/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,17 +409,21 @@ func (o *contextOptions) generateContextData() (*contextData, []error) {
fmt.Fprintln(os.Stderr, "Getting Dynatrace URL...")
}

clusterID, err := determineManagementCluster(ocmClient, cluster)
clusterID, err := getManagementClusterID(ocmClient, cluster)
if err != nil {
errors = append(errors, err)
data.DyntraceEnvURL = err.Error()
return
}

data.DyntraceEnvURL, err = GetDynatraceURLFromManagementCluster(clusterID)
data.DyntraceEnvURL, err = getDynatraceURLFromLabel(ocmClient, clusterID)
if err != nil {
errors = append(errors, fmt.Errorf("Error The Dynatrace Environemnt URL could not be determined %s", err))
data.DyntraceEnvURL = "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"
errors = append(errors, fmt.Errorf("Error The Dynatrace Environemnt URL could not be determined from Label. Using fallback method%s", err))
// FallBack method to determine via Cluster Login
data.DyntraceEnvURL, err = getDynatraceURLFromManagementCluster(clusterID)
if err != nil {
errors = append(errors, fmt.Errorf("Error The Dynatrace Environemnt URL could not be determined %s", err))
data.DyntraceEnvURL = "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"
}
}
}

Expand Down
45 changes: 38 additions & 7 deletions cmd/cluster/dynatrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

const HypershiftClusterTypeLabel string = "ext-hypershift.openshift.io/cluster-type"
const (
HypershiftClusterTypeLabel string = "ext-hypershift.openshift.io/cluster-type"
DynatraceTenantKeyLabel string = "sre-capabilities.dtp.tenant"
)

func newCmdDynatraceURL() *cobra.Command {
orgIdCmd := &cobra.Command{
Expand Down Expand Up @@ -47,20 +50,23 @@ func fetchDetails(clusterKey string) error {
return err
}

clusterID, err := determineManagementCluster(connection, cluster)
clusterID, err := getManagementClusterID(connection, cluster)
if err != nil {
return err
}

url, err := GetDynatraceURLFromManagementCluster(clusterID)
url, err := getDynatraceURLFromLabel(connection, clusterID)
if err != nil {
return 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)
// FallBack method to determine via Cluster Login
url, err = getDynatraceURLFromManagementCluster(clusterID)
if err != nil {
return 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)
}
}
fmt.Println("Dynatrace Environment URL - ", url)
return nil
}

func determineManagementCluster(connection *sdk.Connection, cluster *v1.Cluster) (string, error) {
func getManagementClusterID(connection *sdk.Connection, cluster *v1.Cluster) (string, error) {
var clusterID = cluster.ID()
if cluster.Hypershift().Enabled() {
ManagementCluster, err := ocmutils.GetManagementCluster(clusterID)
Expand Down Expand Up @@ -104,7 +110,32 @@ func isManagementCluster(connection *sdk.Connection, clusterID string) bool {
return false
}

func GetDynatraceURLFromManagementCluster(clusterID string) (string, error) {
func getDynatraceURLFromLabel(connection *sdk.Connection, clusterID string) (string, error) {
subscription, err := ocmutils.GetSubscription(connection, clusterID)
if err != nil {
return "", err
}

subscriptionLabels, err := connection.AccountsMgmt().V1().Subscriptions().Subscription(subscription.ID()).Labels().List().Send()
labels, ok := subscriptionLabels.GetItems()
if !ok {
return "", err
}

for _, label := range labels.Slice() {
if key, ok := label.GetKey(); ok {
if key == DynatraceTenantKeyLabel {
if value, ok := label.GetValue(); ok {
url := fmt.Sprintf("https://%s.live.dynatrace.com/", value)
return url, nil
}
}
}
}
return "", fmt.Errorf("Could not determine URL")
}

func getDynatraceURLFromManagementCluster(clusterID string) (string, error) {
// Register v1beta1 for DynaKube
scheme := runtime.NewScheme()
if err := v1beta1.AddToScheme(scheme); err != nil {
Expand Down

0 comments on commit c163a14

Please sign in to comment.