Skip to content

Commit

Permalink
Refactored a code
Browse files Browse the repository at this point in the history
  • Loading branch information
devppratik committed Nov 27, 2023
1 parent 5ee0f55 commit 80c1026
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
21 changes: 5 additions & 16 deletions cmd/cluster/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,25 +407,14 @@ func (o *contextOptions) generateContextData() (*contextData, []error) {
fmt.Fprintln(os.Stderr, "Getting Dynatrace URL...")
}

// Get MC Cluster if HCP
if cluster.Hypershift().Enabled() {
ManagementCluster, err := utils.GetManagementCluster(cluster.ID())
if err != nil {
errors = append(errors, fmt.Errorf("Dynatrace - Cannot determine MC Cluster"))
data.DyntraceEnvURL = "Cannot determine MC Cluster"
return
}
clusterID = ManagementCluster.ID()
}

// Sanity Check for MC/HCP Cluster
if !isManagementCluster(ocmClient, clusterID) && !cluster.Hypershift().Enabled() {
errors = append(errors, fmt.Errorf("Dynatrace - cluster is not a HCP/Management cluster"))
data.DyntraceEnvURL = "cluster is Not a HCP/Management Cluster"
clusterID, err := determineManagementCluster(ocmClient, cluster)
if err != nil {
errors = append(errors, err)
data.DyntraceEnvURL = err.Error()
return
}

data.DyntraceEnvURL, err = GetDynatraceURLFromCluster(clusterID)
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
35 changes: 21 additions & 14 deletions cmd/cluster/dynatrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/Dynatrace/dynatrace-operator/src/api/v1beta1"
"github.com/Dynatrace/dynatrace-operator/src/api/v1beta1/dynakube"
sdk "github.com/openshift-online/ocm-sdk-go"
v1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/openshift/osdctl/pkg/k8s"
ocmutils "github.com/openshift/osdctl/pkg/utils"
"github.com/spf13/cobra"
Expand All @@ -32,7 +33,6 @@ func newCmdDynatraceURL() *cobra.Command {
}

func fetchDetails(clusterKey string) error {
var clusterID string
if err := ocmutils.IsValidClusterKey(clusterKey); err != nil {
return err
}
Expand All @@ -47,26 +47,33 @@ func fetchDetails(clusterKey string) error {
return err
}

clusterID = cluster.ID()
// Get MC Cluster if it is HCP
clusterID, err := determineManagementCluster(connection, cluster)
if err != nil {
return err
}

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) {
var clusterID = cluster.ID()
if cluster.Hypershift().Enabled() {
ManagementCluster, err := ocmutils.GetManagementCluster(cluster.ID())
ManagementCluster, err := ocmutils.GetManagementCluster(clusterID)
if err != nil {
return fmt.Errorf("error retreiving Management Cluster for given HCP")
return "", fmt.Errorf("error retreiving Management Cluster for given HCP")
}
clusterID = ManagementCluster.ID()
}

if !isManagementCluster(connection, clusterID) && !cluster.Hypershift().Enabled() {
return fmt.Errorf("cluster is not a HCP/Management Cluster")
}

url, err := GetDynatraceURLFromCluster(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)
return "", fmt.Errorf("cluster is not a HCP/Management Cluster")
}
fmt.Println("Dynatrace Environment URL - ", url)
return nil
return clusterID, nil
}

// Sanity Check for MC Cluster
Expand Down Expand Up @@ -97,7 +104,7 @@ func isManagementCluster(connection *sdk.Connection, clusterID string) bool {
return false
}

func GetDynatraceURLFromCluster(clusterID string) (string, error) {
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 80c1026

Please sign in to comment.