Skip to content

Commit

Permalink
Project should not fetch client immediately
Browse files Browse the repository at this point in the history
oc project can be used to switch config contexts, but loads its client
in the Complete() method which does API negotiation. Since a user may
not be logged in or the server they are talking to may be down, changing
projects then doesn't work.

Use a lazy client loader just for project until we can sort out lazy api
negotiation and/or something better on client.
  • Loading branch information
smarterclayton committed May 8, 2016
1 parent 5331355 commit 205f9d7
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions pkg/cmd/cli/cmd/project.go
Expand Up @@ -23,8 +23,8 @@ import (

type ProjectOptions struct {
Config clientcmdapi.Config
Client *client.Client
ClientConfig *restclient.Config
ClientFn func() (*client.Client, error)
Out io.Writer
PathOptions *kubecmdconfig.PathOptions

Expand Down Expand Up @@ -104,9 +104,9 @@ func (o *ProjectOptions) Complete(f *clientcmd.Factory, args []string, out io.Wr
return err
}

o.Client, _, err = f.Clients()
if err != nil {
return err
o.ClientFn = func() (*client.Client, error) {
client, _, err := f.Clients()
return client, err
}

o.Out = out
Expand Down Expand Up @@ -134,8 +134,12 @@ func (o ProjectOptions) RunProject() error {
return nil
}

_, err := o.Client.Projects().Get(currentProject)
client, err := o.ClientFn()
if err != nil {
return err
}

if _, err := client.Projects().Get(currentProject); err != nil {
if kapierrors.IsNotFound(err) {
return fmt.Errorf("the project %q specified in your config does not exist.", currentProject)
}
Expand Down Expand Up @@ -181,8 +185,12 @@ func (o ProjectOptions) RunProject() error {

} else {
if !o.SkipAccessValidation {
_, err := o.Client.Projects().Get(argument)
client, err := o.ClientFn()
if err != nil {
return err
}

if _, err := client.Projects().Get(argument); err != nil {
if isNotFound, isForbidden := kapierrors.IsNotFound(err), clientcmd.IsForbidden(err); isNotFound || isForbidden {
var msg string
if isForbidden {
Expand All @@ -191,7 +199,7 @@ func (o ProjectOptions) RunProject() error {
msg = fmt.Sprintf("A project named %q does not exist on %q.", argument, clientCfg.Host)
}

projects, err := getProjects(o.Client)
projects, err := getProjects(client)
if err == nil {
switch len(projects) {
case 0:
Expand Down

0 comments on commit 205f9d7

Please sign in to comment.