-
Notifications
You must be signed in to change notification settings - Fork 439
oc login/project: Add support for custom context name #2187
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -276,19 +276,47 @@ func (o ProjectOptions) Run() error { | |||
| userNameInUse = user.Name | ||||
| } | ||||
|
|
||||
| kubeconfig, err := cliconfig.CreateConfig(projectName, userNameInUse, o.RESTConfig) | ||||
| if err != nil { | ||||
| return err | ||||
| // Check if the current context has a custom name (doesn't match the auto-generated pattern). | ||||
| // If so, and the server matches, just update its namespace instead of creating a new context. | ||||
| // This preserves custom context names set via "oc login --context=<name>". | ||||
| contextUpdated := false | ||||
| if currentContext != nil { | ||||
| defaultContextName := cliconfig.GetContextNickname(currentContext.Namespace, currentContext.Cluster, currentContext.AuthInfo) | ||||
| if config.CurrentContext != defaultContextName { | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Logic in this PR makes sense to me. However, historically ProjectName always aligns with contextName ( Line 225 in 7e80dba
I understand the inconvenience filed in the issue. But it is more about annoyance not an actual bug. Initially I was supportive of this work. But now I think I would prefer waiting from official RFE from customers to be driven by BU that support us to change the flow. This PR is already mergeable for me.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alright, didn't know the context name is so important to match. Thanks for the clarification.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Me neither until seeing openshift/origin#16161 (comment). But it is good to have your PR in case we need it in the future. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just landed on this PR by doing a quick google search if oc supports naming for context. I'm atm doing some integrations with Konflux and it requires our tooling to talk with more than one cluster in the same process, having static naming for the credential will save us some troubleshooting time in CI. Do we really need one RFE for this? It's been almost 9 years since openshift/origin#16161 (comment) was done, this was before OCP 4.x (feeling old), a lot changed on how we view Openshift. |
||||
| if cluster := config.Clusters[currentContext.Cluster]; cluster != nil { | ||||
| currentServer, err := cliconfig.NormalizeServerURL(cluster.Server) | ||||
| if err != nil { | ||||
| return fmt.Errorf("invalid server URL %q in kubeconfig: %v", cluster.Server, err) | ||||
| } | ||||
| targetServer, err := cliconfig.NormalizeServerURL(clientCfg.Host) | ||||
| if err != nil { | ||||
| return fmt.Errorf("invalid server URL %q: %v", clientCfg.Host, err) | ||||
| } | ||||
| if currentServer == targetServer { | ||||
| currentContext.Namespace = projectName | ||||
| namespaceInUse = projectName | ||||
| contextInUse = config.CurrentContext | ||||
| contextUpdated = true | ||||
| } | ||||
| } | ||||
| } | ||||
| } | ||||
|
|
||||
| merged, err := cliconfig.MergeConfig(config, *kubeconfig) | ||||
| if err != nil { | ||||
| return err | ||||
| } | ||||
| config = *merged | ||||
| if !contextUpdated { | ||||
| kubeconfig, err := cliconfig.CreateConfig(projectName, userNameInUse, o.RESTConfig) | ||||
| if err != nil { | ||||
| return err | ||||
| } | ||||
|
|
||||
| namespaceInUse = projectName | ||||
| contextInUse = merged.CurrentContext | ||||
| merged, err := cliconfig.MergeConfig(config, *kubeconfig) | ||||
| if err != nil { | ||||
| return err | ||||
| } | ||||
| config = *merged | ||||
|
|
||||
| namespaceInUse = projectName | ||||
| contextInUse = merged.CurrentContext | ||||
| } | ||||
| } | ||||
|
|
||||
| if err := kclientcmd.ModifyConfig(o.PathOptions, config, true); err != nil { | ||||
|
|
||||
Uh oh!
There was an error while loading. Please reload this page.