diff --git a/cmd/cluster/context.go b/cmd/cluster/context.go index e3158d41..df402171 100644 --- a/cmd/cluster/context.go +++ b/cmd/cluster/context.go @@ -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" diff --git a/cmd/cluster/dynatrace.go b/cmd/cluster/dynatrace.go index 600220ff..664188e6 100644 --- a/cmd/cluster/dynatrace.go +++ b/cmd/cluster/dynatrace.go @@ -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])) @@ -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() @@ -42,16 +42,17 @@ 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) } @@ -59,11 +60,27 @@ func fetchDetails(clusterID string) error { 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 { @@ -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 } diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index c0306012..b70abb1f 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) {