Skip to content

Commit

Permalink
preserve error type in loginoptions
Browse files Browse the repository at this point in the history
  • Loading branch information
juanvallejo committed Nov 2, 2017
1 parent ea8f407 commit f235ce3
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 14 deletions.
3 changes: 2 additions & 1 deletion pkg/oc/bootstrap/docker/openshift/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

// Login logs into the specified server using given credentials and CA file
func Login(username, password, server, configDir string, f *clientcmd.Factory, c *cobra.Command, out io.Writer) error {
func Login(username, password, server, configDir string, f *clientcmd.Factory, c *cobra.Command, out, errOut io.Writer) error {
existingConfig, err := f.OpenShiftClientConfig().RawConfig()
if err != nil {
if !os.IsNotExist(err) {
Expand Down Expand Up @@ -72,6 +72,7 @@ func Login(username, password, server, configDir string, f *clientcmd.Factory, c
Username: username,
Password: password,
Out: output,
ErrOut: errOut,
StartingKubeConfig: newConfig,
PathOptions: config.NewPathOptions(c),
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/oc/bootstrap/docker/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"path/filepath"
Expand Down Expand Up @@ -1112,7 +1113,7 @@ func (c *ClientStartConfig) RegisterTemplateServiceBroker(out io.Writer) error {
// Login logs into the new server and sets up a default user and project
func (c *ClientStartConfig) Login(out io.Writer) error {
server := c.OpenShiftHelper().Master(c.ServerIP)
return openshift.Login(initialUser, initialPassword, server, c.LocalConfigDir, c.originalFactory, c.command, out)
return openshift.Login(initialUser, initialPassword, server, c.LocalConfigDir, c.originalFactory, c.command, out, out)
}

// CreateProject creates a new project for the current user
Expand Down
2 changes: 1 addition & 1 deletion pkg/oc/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func NewCommandCLI(name, fullName string, in io.Reader, out, errout io.Writer) *

f := clientcmd.New(cmds.PersistentFlags())

loginCmd := login.NewCmdLogin(fullName, f, in, out)
loginCmd := login.NewCmdLogin(fullName, f, in, out, errout)
secretcmds := secrets.NewCmdSecrets(secrets.SecretsRecommendedName, fullName+" "+secrets.SecretsRecommendedName, f, in, out, errout, fullName+" edit")

groups := ktemplates.CommandGroups{
Expand Down
3 changes: 2 additions & 1 deletion pkg/oc/cli/cmd/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ var (
)

// NewCmdLogin implements the OpenShift cli login command
func NewCmdLogin(fullName string, f *osclientcmd.Factory, reader io.Reader, out io.Writer) *cobra.Command {
func NewCmdLogin(fullName string, f *osclientcmd.Factory, reader io.Reader, out, errOut io.Writer) *cobra.Command {
options := &LoginOptions{
Reader: reader,
Out: out,
ErrOut: errOut,
}

cmds := &cobra.Command{
Expand Down
13 changes: 4 additions & 9 deletions pkg/oc/cli/cmd/login/loginoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io"
"net"
"net/http"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -56,6 +55,7 @@ type LoginOptions struct {
Config *restclient.Config
Reader io.Reader
Out io.Writer
ErrOut io.Writer

// cert data to be used when authenticating
CertFile string
Expand Down Expand Up @@ -226,15 +226,10 @@ func (o *LoginOptions) gatherAuthInfo() error {
token, err := tokencmd.RequestToken(o.Config, o.Reader, o.Username, o.Password)
if err != nil {
suggestion := "verify you have provided the correct host and port and that the server is currently running."
fmt.Fprintf(o.ErrOut, "error: %v - %v\n", err, suggestion)

// if internal error occurs, suggest making sure
// client is connecting to the right host:port
if statusErr, ok := err.(*kerrors.StatusError); ok {
if statusErr.Status().Code == http.StatusInternalServerError {
return fmt.Errorf("error: The server was unable to respond - %v", suggestion)
}
}
return fmt.Errorf("%v - %v", err, suggestion)
// return error as-is, as method caller expects to check its type
return err
}
clientConfig.BearerToken = token

Expand Down
3 changes: 2 additions & 1 deletion test/integration/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ func newLoginOptions(server string, username string, password string, insecure b
Password: password,
InsecureTLS: insecure,

Out: ioutil.Discard,
Out: ioutil.Discard,
ErrOut: ioutil.Discard,
}

return loginOptions
Expand Down
1 change: 1 addition & 0 deletions test/integration/oauth_oidc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func TestOAuthOIDC(t *testing.T) {
StartingKubeConfig: &clientcmdapi.Config{},
Reader: bytes.NewBufferString("mylogin\nmypassword\n"),
Out: loginOutput,
ErrOut: ioutil.Discard,
}
if err := loginOptions.GatherInfo(); err != nil {
t.Fatalf("Error logging in: %v\n%v", err, loginOutput.String())
Expand Down
1 change: 1 addition & 0 deletions test/integration/oauth_request_header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ func TestOAuthRequestHeader(t *testing.T) {
StartingKubeConfig: &clientcmdapi.Config{},
Reader: bytes.NewBufferString("myusername\nmypassword\n"),
Out: loginOutput,
ErrOut: ioutil.Discard,
}
if err := loginOptions.GatherInfo(); err != nil {
t.Fatalf("Error trying to determine server info: %v\n%v", err, loginOutput.String())
Expand Down

0 comments on commit f235ce3

Please sign in to comment.