Skip to content

Commit daa62d5

Browse files
committed
prevent ctx-switching from failing on server err
1 parent 900d01c commit daa62d5

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

pkg/cmd/cli/cmd/project.go

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"io"
8+
"net/url"
89

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

106107
o.ClientConfig, err = f.ClientConfig()
107108
if err != nil {
108-
return err
109+
contextNameExists := false
110+
if _, exists := o.GetContextFromName(o.ProjectName); exists {
111+
contextNameExists = exists
112+
}
113+
114+
if _, isURLError := err.(*url.Error); !(isURLError || kapierrors.IsInternalError(err)) || !contextNameExists {
115+
return err
116+
}
117+
118+
// if the argument for o.ProjectName passed by the user is a context name,
119+
// prevent local context-switching from failing due to an unreachable
120+
// server or an unfetchable ClientConfig.
121+
o.Config.CurrentContext = o.ProjectName
122+
if err := kclientcmd.ModifyConfig(o.PathOptions, o.Config, true); err != nil {
123+
return err
124+
}
125+
126+
// since we failed to retrieve ClientConfig for the current server,
127+
// fetch local OpenShift client config
128+
o.ClientConfig, err = f.OpenShiftClientConfig().ClientConfig()
129+
if err != nil {
130+
return err
131+
}
132+
109133
}
110134

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

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

190214
config.CurrentContext = argument
191-
192215
} else {
193216
if !o.SkipAccessValidation {
194217
client, kubeclient, err := o.ClientFn()
@@ -281,6 +304,15 @@ func (o ProjectOptions) RunProject() error {
281304
return nil
282305
}
283306

307+
// returns a context by the given contextName and a boolean true if the context exists
308+
func (o *ProjectOptions) GetContextFromName(contextName string) (*clientcmdapi.Context, bool) {
309+
if context, contextExists := o.Config.Contexts[contextName]; !o.ProjectOnly && contextExists {
310+
return context, true
311+
}
312+
313+
return nil, false
314+
}
315+
284316
func confirmProjectAccess(currentProject string, oClient *client.Client, kClient kclientset.Interface) error {
285317
_, projectErr := oClient.Projects().Get(currentProject)
286318
if !kapierrors.IsNotFound(projectErr) && !kapierrors.IsForbidden(projectErr) {

0 commit comments

Comments
 (0)