Skip to content

Commit

Permalink
Merge pull request #485 from devppratik/OSD-19751-Enhance-osdctl-for-…
Browse files Browse the repository at this point in the history
…Dynatrace

OSD-19751: Enhance osdctl for Dynatrace
  • Loading branch information
openshift-merge-bot[bot] authored Nov 27, 2023
2 parents c4f227c + 80c1026 commit cdfaf84
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
11 changes: 7 additions & 4 deletions cmd/cluster/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,17 +401,20 @@ func (o *contextOptions) generateContextData() (*contextData, []error) {
}

GetDynatraceURL := func() {
var clusterID string = o.clusterID
defer wg.Done()
if o.verbose {
fmt.Fprintln(os.Stderr, "Getting Dynatrace URL...")
}
if !isManagementCluster(ocmClient, cluster) {
errors = append(errors, fmt.Errorf("cluster is not a management cluster"))
data.DyntraceEnvURL = "cluster is Not a Management Cluster"

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

data.DyntraceEnvURL, err = GetDynatraceURLFromCluster(cluster)
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
39 changes: 28 additions & 11 deletions cmd/cluster/dynatrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const HypershiftClusterTypeLabel string = "ext-hypershift.openshift.io/cluster-t
func newCmdDynatraceURL() *cobra.Command {
orgIdCmd := &cobra.Command{
Use: "dynatrace CLUSTER_ID",
Short: "Get the Dyntrace Tenant URL for a given MC cluster",
Short: "Get the Dyntrace Tenant URL for a given MC or HCP cluster",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(fetchDetails(args[0]))
Expand All @@ -32,8 +32,8 @@ func newCmdDynatraceURL() *cobra.Command {
return orgIdCmd
}

func fetchDetails(clusterID string) error {
if err := ocmutils.IsValidClusterKey(clusterID); err != nil {
func fetchDetails(clusterKey string) error {
if err := ocmutils.IsValidClusterKey(clusterKey); err != nil {
return err
}
connection, err := ocmutils.CreateConnection()
Expand All @@ -42,28 +42,45 @@ func fetchDetails(clusterID string) error {
}
defer connection.Close()

cluster, err := ocmutils.GetCluster(connection, clusterID)
cluster, err := ocmutils.GetCluster(connection, clusterKey)
if err != nil {
return err
}

if !isManagementCluster(connection, cluster) {
return fmt.Errorf("cluster is not a management cluster")
clusterID, err := determineManagementCluster(connection, cluster)
if err != nil {
return err
}

url, err := GetDynatraceURLFromCluster(cluster)
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(clusterID)
if err != nil {
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")
}
return clusterID, nil
}

// Sanity Check for MC Cluster
func isManagementCluster(connection *sdk.Connection, cluster *v1.Cluster) bool {
func isManagementCluster(connection *sdk.Connection, clusterID string) bool {
collection := connection.ClustersMgmt().V1().Clusters()
// Get the labels externally available for the cluster
resource := collection.Cluster(cluster.ID()).ExternalConfiguration().Labels()
resource := collection.Cluster(clusterID).ExternalConfiguration().Labels()
// Send the request to retrieve the list of external cluster labels:
response, err := resource.List().Send()
if err != nil {
Expand All @@ -87,13 +104,13 @@ func isManagementCluster(connection *sdk.Connection, cluster *v1.Cluster) bool {
return false
}

func GetDynatraceURLFromCluster(cluster *v1.Cluster) (string, error) {
func GetDynatraceURLFromManagementCluster(clusterID string) (string, error) {
// Register v1beta1 for DynaKube
scheme := runtime.NewScheme()
if err := v1beta1.AddToScheme(scheme); err != nil {
return "", err
}
c, err := k8s.New(cluster.ID(), client.Options{Scheme: scheme})
c, err := k8s.New(clusterID, client.Options{Scheme: scheme})
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func GetRegistryCredentials(connection *sdk.Connection, accountId string) ([]*am
func ConfirmPrompt() bool {
fmt.Print("Continue? (y/N): ")

var response string
var response string = "n"
_, _ = fmt.Scanln(&response) // Erroneous input will be handled by the default case below

switch strings.ToLower(response) {
Expand Down

0 comments on commit cdfaf84

Please sign in to comment.