Skip to content

prevent ctx-switching from failing on server err #12571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions pkg/cmd/cli/cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"net/url"

kapi "k8s.io/kubernetes/pkg/api"
kapierrors "k8s.io/kubernetes/pkg/api/errors"
Expand Down Expand Up @@ -105,7 +106,30 @@ func (o *ProjectOptions) Complete(f *clientcmd.Factory, args []string, out io.Wr

o.ClientConfig, err = f.ClientConfig()
if err != nil {
return err
contextNameExists := false
if _, exists := o.GetContextFromName(o.ProjectName); exists {
contextNameExists = exists
}

if _, isURLError := err.(*url.Error); !(isURLError || kapierrors.IsInternalError(err)) || !contextNameExists {
return err
}

// if the argument for o.ProjectName passed by the user is a context name,
// prevent local context-switching from failing due to an unreachable
// server or an unfetchable ClientConfig.
o.Config.CurrentContext = o.ProjectName
if err := kclientcmd.ModifyConfig(o.PathOptions, o.Config, true); err != nil {
return err
}

// since we failed to retrieve ClientConfig for the current server,
// fetch local OpenShift client config
o.ClientConfig, err = f.OpenShiftClientConfig().ClientConfig()
if err != nil {
return err
}

}

o.ClientFn = func() (*client.Client, kclientset.Interface, error) {
Expand Down Expand Up @@ -183,12 +207,11 @@ func (o ProjectOptions) RunProject() error {

// Check if argument is an existing context, if so just set it as the context in use.
// If not a context then we will try to handle it as a project.
if context, contextExists := config.Contexts[argument]; !o.ProjectOnly && contextExists {
if context, contextExists := o.GetContextFromName(argument); contextExists {
contextInUse = argument
namespaceInUse = context.Namespace

config.CurrentContext = argument

} else {
if !o.SkipAccessValidation {
client, kubeclient, err := o.ClientFn()
Expand Down Expand Up @@ -281,6 +304,15 @@ func (o ProjectOptions) RunProject() error {
return nil
}

// returns a context by the given contextName and a boolean true if the context exists
func (o *ProjectOptions) GetContextFromName(contextName string) (*clientcmdapi.Context, bool) {
if context, contextExists := o.Config.Contexts[contextName]; !o.ProjectOnly && contextExists {
return context, true
}

return nil, false
}

func confirmProjectAccess(currentProject string, oClient *client.Client, kClient kclientset.Interface) error {
_, projectErr := oClient.Projects().Get(currentProject)
if !kapierrors.IsNotFound(projectErr) && !kapierrors.IsForbidden(projectErr) {
Expand Down