Skip to content

Commit

Permalink
inform user to login to OCM if the backplane failed due to that
Browse files Browse the repository at this point in the history
  • Loading branch information
bmeng committed Jul 23, 2024
1 parent 272df9a commit 53c177a
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 29 deletions.
9 changes: 5 additions & 4 deletions cmd/ocm-backplane/accessrequest/createAccessRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (

"github.com/openshift/backplane-cli/pkg/accessrequest"

ocmcli "github.com/openshift-online/ocm-cli/pkg/ocm"
"github.com/openshift/backplane-cli/pkg/login"
"github.com/openshift/backplane-cli/pkg/utils"
logger "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/openshift/backplane-cli/pkg/login"
"github.com/openshift/backplane-cli/pkg/ocm"
"github.com/openshift/backplane-cli/pkg/utils"
)

var (
Expand Down Expand Up @@ -96,7 +97,7 @@ func runCreateAccessRequest(cmd *cobra.Command, args []string) error {
return fmt.Errorf("failed to compute cluster ID: %v", err)
}

ocmConnection, err := ocmcli.NewConnection().Build()
ocmConnection, err := ocm.DefaultOCMInterface.SetupOCMConnection()
if err != nil {
return fmt.Errorf("failed to create OCM connection: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/ocm-backplane/accessrequest/expireAccessRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"

"github.com/openshift/backplane-cli/pkg/accessrequest"
"github.com/openshift/backplane-cli/pkg/ocm"

ocmcli "github.com/openshift-online/ocm-cli/pkg/ocm"
"github.com/spf13/cobra"
)

Expand All @@ -30,7 +30,7 @@ func runExpireAccessRequest(cmd *cobra.Command, args []string) error {
return fmt.Errorf("failed to compute cluster ID: %v", err)
}

ocmConnection, err := ocmcli.NewConnection().Build()
ocmConnection, err := ocm.DefaultOCMInterface.SetupOCMConnection()
if err != nil {
return fmt.Errorf("failed to create OCM connection: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/ocm-backplane/accessrequest/getAccessRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"

"github.com/openshift/backplane-cli/pkg/accessrequest"
"github.com/openshift/backplane-cli/pkg/ocm"

ocmcli "github.com/openshift-online/ocm-cli/pkg/ocm"
logger "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand All @@ -31,7 +31,7 @@ func runGetAccessRequest(cmd *cobra.Command, args []string) error {
return fmt.Errorf("failed to compute cluster ID: %v", err)
}

ocmConnection, err := ocmcli.NewConnection().Build()
ocmConnection, err := ocm.DefaultOCMInterface.SetupOCMConnection()
if err != nil {
return fmt.Errorf("failed to create OCM connection: %v", err)
}
Expand Down
6 changes: 2 additions & 4 deletions cmd/ocm-backplane/cloud/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"os"
"strconv"

ocmsdk "github.com/openshift-online/ocm-cli/pkg/ocm"

"github.com/openshift/backplane-cli/pkg/ocm"

"github.com/pkg/browser"
Expand Down Expand Up @@ -130,9 +128,9 @@ func runConsole(cmd *cobra.Command, argv []string) (err error) {
logger.Infof("Using backplane URL: %s\n", backplaneConfiguration.URL)

// Initialize OCM connection
ocmConnection, err := ocmsdk.NewConnection().Build()
ocmConnection, err := ocm.DefaultOCMInterface.SetupOCMConnection()
if err != nil {
return fmt.Errorf("unable to build ocm sdk: %w", err)
return fmt.Errorf("failed to create OCM connection: %w", err)
}
defer ocmConnection.Close()

Expand Down
5 changes: 2 additions & 3 deletions cmd/ocm-backplane/cloud/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"

ocmsdk "github.com/openshift-online/ocm-cli/pkg/ocm"
logger "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"sigs.k8s.io/yaml"
Expand Down Expand Up @@ -98,9 +97,9 @@ func runCredentials(cmd *cobra.Command, argv []string) error {
logger.Infof("Using backplane URL: %s\n", backplaneConfiguration.URL)

// Initialize OCM connection
ocmConnection, err := ocmsdk.NewConnection().Build()
ocmConnection, err := ocm.DefaultOCMInterface.SetupOCMConnection()
if err != nil {
return fmt.Errorf("unable to build ocm sdk: %w", err)
return fmt.Errorf("failed to create OCM connection: %w", err)
}
defer ocmConnection.Close()

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.19.0
github.com/trivago/tgo v1.0.7
golang.org/x/term v0.22.0
gopkg.in/AlecAivazis/survey.v1 v1.8.8
k8s.io/api v0.28.3
Expand Down Expand Up @@ -123,7 +124,6 @@ require (
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/trivago/tgo v1.0.7 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
github.com/zalando/go-keyring v0.2.3 // indirect
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
Expand Down
15 changes: 15 additions & 0 deletions pkg/ocm/mocks/ocmWrapperMock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 47 additions & 13 deletions pkg/ocm/ocmWrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"errors"
"fmt"
"net/http"
"os"
"strings"

"github.com/openshift-online/ocm-cli/pkg/ocm"
ocmocm "github.com/openshift-online/ocm-cli/pkg/ocm"
ocmurls "github.com/openshift-online/ocm-cli/pkg/urls"
ocmsdk "github.com/openshift-online/ocm-sdk-go"
acctrspv1 "github.com/openshift-online/ocm-sdk-go/accesstransparency/v1"
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
Expand Down Expand Up @@ -35,19 +37,51 @@ type OCMInterface interface {
GetClusterActiveAccessRequest(ocmConnection *ocmsdk.Connection, clusterID string) (*acctrspv1.AccessRequest, error)
CreateClusterAccessRequest(ocmConnection *ocmsdk.Connection, clusterID, reason, jiraIssueID, approvalDuration string) (*acctrspv1.AccessRequest, error)
CreateAccessRequestDecision(ocmConnection *ocmsdk.Connection, accessRequest *acctrspv1.AccessRequest, decision acctrspv1.DecisionDecision, justification string) (*acctrspv1.Decision, error)
SetupOCMConnection() (*ocmsdk.Connection, error)
}

const (
ClustersPageSize = 50
ClustersPageSize = 50
ocmNotLoggedInMessage = "Not logged in"
)

type DefaultOCMInterfaceImpl struct{}
type DefaultOCMInterfaceImpl struct {
// connection *ocmsdk.Connection
}

var DefaultOCMInterface OCMInterface = &DefaultOCMInterfaceImpl{}

// SetupOCMConnection setups the ocm connection for all the other ocm requests
func (o *DefaultOCMInterfaceImpl) SetupOCMConnection() (*ocmsdk.Connection, error) {

envURL := os.Getenv("OCM_URL")
if envURL != "" {
// Fetch the real ocm url from the alias and set it back to the ENV
ocmURL, err := ocmurls.ResolveGatewayURL(envURL, nil)
if err != nil {
return nil, err
}
os.Setenv("OCM_URL", ocmURL)
logger.Debugf("reset the OCM_URL to %s", ocmURL)
}

// Setup connection at the first try
connection, err := ocmocm.NewConnection().Build()
if err != nil {
if strings.Contains(err.Error(), ocmNotLoggedInMessage) {
return nil, fmt.Errorf("please ensure you are logged into OCM by using the command " +
"\"ocm login --url $ENV\"")
} else {
return nil, err
}
}

return connection, nil
}

// IsClusterHibernating returns a boolean to indicate whether the cluster is hibernating
func (o *DefaultOCMInterfaceImpl) IsClusterHibernating(clusterID string) (bool, error) {
connection, err := ocm.NewConnection().Build()
connection, err := o.SetupOCMConnection()
if err != nil {
return false, fmt.Errorf("failed to create OCM connection: %v", err)
}
Expand All @@ -64,7 +98,7 @@ func (o *DefaultOCMInterfaceImpl) IsClusterHibernating(clusterID string) (bool,
// GetTargetCluster returns one single cluster based on the search key and survery.
func (o *DefaultOCMInterfaceImpl) GetTargetCluster(clusterKey string) (clusterID, clusterName string, err error) {
// Create the client for the OCM API:
connection, err := ocm.NewConnection().Build()
connection, err := o.SetupOCMConnection()
if err != nil {
return "", "", fmt.Errorf("failed to create OCM connection: %v", err)
}
Expand Down Expand Up @@ -96,7 +130,7 @@ func (o *DefaultOCMInterfaceImpl) GetTargetCluster(clusterKey string) (clusterID
// for the given clusterID
func (o *DefaultOCMInterfaceImpl) GetManagingCluster(targetClusterID string) (clusterID, clusterName string, isHostedControlPlane bool, err error) {
// Create the client for the OCM API:
connection, err := ocm.NewConnection().Build()
connection, err := o.SetupOCMConnection()
if err != nil {
return "", "", false, fmt.Errorf("failed to create OCM connection: %v", err)
}
Expand Down Expand Up @@ -153,7 +187,7 @@ func (o *DefaultOCMInterfaceImpl) GetManagingCluster(targetClusterID string) (cl
// GetServiceCluster gets the service cluster for a given hpyershift hosted cluster
func (o *DefaultOCMInterfaceImpl) GetServiceCluster(targetClusterID string) (clusterID, clusterName string, err error) {
// Create the client for the OCM API
connection, err := ocm.NewConnection().Build()
connection, err := o.SetupOCMConnection()
if err != nil {
return "", "", fmt.Errorf("failed to create OCM connection: %v", err)
}
Expand Down Expand Up @@ -207,7 +241,7 @@ func (o *DefaultOCMInterfaceImpl) GetServiceCluster(targetClusterID string) (clu
// GetOCMAccessToken initiates the OCM connection and returns the access token
func (o *DefaultOCMInterfaceImpl) GetOCMAccessToken() (*string, error) {
// Get ocm access token
connection, err := ocm.NewConnection().Build()
connection, err := o.SetupOCMConnection()
if err != nil {
return nil, fmt.Errorf("failed to create OCM connection: %v", err)
}
Expand Down Expand Up @@ -236,7 +270,7 @@ func (o *DefaultOCMInterfaceImpl) GetPullSecret() (string, error) {

// Get ocm access token
logger.Debugln("Finding ocm token")
connection, err := ocm.NewConnection().Build()
connection, err := o.SetupOCMConnection()
if err != nil {
return "", fmt.Errorf("failed to create OCM connection: %v", err)
}
Expand All @@ -256,7 +290,7 @@ func (o *DefaultOCMInterfaceImpl) GetPullSecret() (string, error) {
// for a given internal cluster id.
func (o *DefaultOCMInterfaceImpl) GetClusterInfoByID(clusterID string) (*cmv1.Cluster, error) {
// Create the client for the OCM API:
connection, err := ocm.NewConnection().Build()
connection, err := o.SetupOCMConnection()
if err != nil {
return nil, fmt.Errorf("failed to create OCM connection: %v", err)
}
Expand All @@ -282,7 +316,7 @@ func (o *DefaultOCMInterfaceImpl) GetClusterInfoByIDWithConn(ocmConnection *ocms
// IsProduction checks if OCM is currently in production env
func (o *DefaultOCMInterfaceImpl) IsProduction() (bool, error) {
// Create the client for the OCM API:
connection, err := ocm.NewConnection().Build()
connection, err := o.SetupOCMConnection()
if err != nil {
return false, fmt.Errorf("failed to create OCM connection: %v", err)
}
Expand All @@ -299,10 +333,10 @@ func (o *DefaultOCMInterfaceImpl) GetStsSupportJumpRoleARN(ocmConnection *ocmsdk
return response.Body().RoleArn(), nil
}

// GetBackplaneURL returns the Backplane API URL based on the OCM env
// GetOCMEnvironment returns the Backplane API URL based on the OCM env
func (o *DefaultOCMInterfaceImpl) GetOCMEnvironment() (*cmv1.Environment, error) {
// Create the client for the OCM API
connection, err := ocm.NewConnection().Build()
connection, err := o.SetupOCMConnection()
if err != nil {
return nil, fmt.Errorf("failed to create OCM connection: %v", err)
}
Expand Down

0 comments on commit 53c177a

Please sign in to comment.