From 5ee0f55d4e4b366d48d2857ac5d088ba27fab8ef Mon Sep 17 00:00:00 2001 From: devppratik Date: Fri, 24 Nov 2023 10:14:12 +0530 Subject: [PATCH 1/2] OSD-19751 Enhance osdctl for Dynatrace minor change for package update description fix minor UX --- cmd/cluster/context.go | 22 ++++++++++++++++++---- cmd/cluster/dynatrace.go | 34 ++++++++++++++++++++++------------ pkg/utils/utils.go | 2 +- 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/cmd/cluster/context.go b/cmd/cluster/context.go index e3158d41..3c5f60bd 100644 --- a/cmd/cluster/context.go +++ b/cmd/cluster/context.go @@ -401,17 +401,31 @@ 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" + + // 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" return } - data.DyntraceEnvURL, err = GetDynatraceURLFromCluster(cluster) + data.DyntraceEnvURL, err = GetDynatraceURLFromCluster(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" diff --git a/cmd/cluster/dynatrace.go b/cmd/cluster/dynatrace.go index 600220ff..4bf362c0 100644 --- a/cmd/cluster/dynatrace.go +++ b/cmd/cluster/dynatrace.go @@ -9,7 +9,6 @@ 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" @@ -23,7 +22,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])) @@ -32,8 +31,9 @@ func newCmdDynatraceURL() *cobra.Command { return orgIdCmd } -func fetchDetails(clusterID string) error { - if err := ocmutils.IsValidClusterKey(clusterID); err != nil { +func fetchDetails(clusterKey string) error { + var clusterID string + if err := ocmutils.IsValidClusterKey(clusterKey); err != nil { return err } connection, err := ocmutils.CreateConnection() @@ -42,16 +42,26 @@ 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 = cluster.ID() + // Get MC Cluster if it is HCP + if cluster.Hypershift().Enabled() { + ManagementCluster, err := ocmutils.GetManagementCluster(cluster.ID()) + 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") } - url, err := GetDynatraceURLFromCluster(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) } @@ -60,10 +70,10 @@ func fetchDetails(clusterID string) error { } // 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 { @@ -87,13 +97,13 @@ func isManagementCluster(connection *sdk.Connection, cluster *v1.Cluster) bool { return false } -func GetDynatraceURLFromCluster(cluster *v1.Cluster) (string, error) { +func GetDynatraceURLFromCluster(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 } diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 9126437c..c655ede9 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -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) { From 80c1026624715081b6bed4acd97d0ad544779c30 Mon Sep 17 00:00:00 2001 From: devppratik Date: Mon, 27 Nov 2023 19:25:43 +0530 Subject: [PATCH 2/2] Refactored a code --- cmd/cluster/context.go | 21 +++++---------------- cmd/cluster/dynatrace.go | 35 +++++++++++++++++++++-------------- 2 files changed, 26 insertions(+), 30 deletions(-) diff --git a/cmd/cluster/context.go b/cmd/cluster/context.go index 3c5f60bd..df402171 100644 --- a/cmd/cluster/context.go +++ b/cmd/cluster/context.go @@ -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" diff --git a/cmd/cluster/dynatrace.go b/cmd/cluster/dynatrace.go index 4bf362c0..664188e6 100644 --- a/cmd/cluster/dynatrace.go +++ b/cmd/cluster/dynatrace.go @@ -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" @@ -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 } @@ -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 @@ -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 {