|
5 | 5 | "errors"
|
6 | 6 | "fmt"
|
7 | 7 | "io"
|
| 8 | + "net/url" |
8 | 9 |
|
9 | 10 | kapi "k8s.io/kubernetes/pkg/api"
|
10 | 11 | kapierrors "k8s.io/kubernetes/pkg/api/errors"
|
@@ -105,7 +106,30 @@ func (o *ProjectOptions) Complete(f *clientcmd.Factory, args []string, out io.Wr
|
105 | 106 |
|
106 | 107 | o.ClientConfig, err = f.ClientConfig()
|
107 | 108 | 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 | + |
109 | 133 | }
|
110 | 134 |
|
111 | 135 | o.ClientFn = func() (*client.Client, kclientset.Interface, error) {
|
@@ -183,12 +207,11 @@ func (o ProjectOptions) RunProject() error {
|
183 | 207 |
|
184 | 208 | // Check if argument is an existing context, if so just set it as the context in use.
|
185 | 209 | // 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 { |
187 | 211 | contextInUse = argument
|
188 | 212 | namespaceInUse = context.Namespace
|
189 | 213 |
|
190 | 214 | config.CurrentContext = argument
|
191 |
| - |
192 | 215 | } else {
|
193 | 216 | if !o.SkipAccessValidation {
|
194 | 217 | client, kubeclient, err := o.ClientFn()
|
@@ -281,6 +304,15 @@ func (o ProjectOptions) RunProject() error {
|
281 | 304 | return nil
|
282 | 305 | }
|
283 | 306 |
|
| 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 | + |
284 | 316 | func confirmProjectAccess(currentProject string, oClient *client.Client, kClient kclientset.Interface) error {
|
285 | 317 | _, projectErr := oClient.Projects().Get(currentProject)
|
286 | 318 | if !kapierrors.IsNotFound(projectErr) && !kapierrors.IsForbidden(projectErr) {
|
|
0 commit comments