Skip to content

Conversation

@tchap
Copy link
Contributor

@tchap tchap commented Jan 16, 2026

The changes are explained below. We need to change both at once to make custom context names work.


oc login: Implement --context flag

The flag can be used to tell oc what context name to use when updating kubeconfig. If a matching context already exists, it is replaced altogether, including the linked cluster and authInfo. This means that the configured clusterandauthInfo` names are also reused.


oc project: Add support for custom context names

When --context or the current context contain a custom context name, reuse that context if it matches the current --server value instead of always expecting the context match the generated context name.

The change is entirely backwards compatible. When the current context matches the generated context name, the old functionality is retained.


Fixes #283

@openshift-ci openshift-ci bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jan 16, 2026
@coderabbitai
Copy link

coderabbitai bot commented Jan 16, 2026

Walkthrough

Adds a new --context flag and Context field to LoginOptions; implements mergeConfig to perform context-aware kubeconfig merging; updates SaveConfig to use mergeConfig; adds tests for merge behavior; separately adjusts project context-update logic to preserve custom contexts.

Changes

Cohort / File(s) Summary
CLI flag & usage
pkg/cli/login/login.go
Add --context flag parsing and populate LoginOptions.Context; update usage examples to include --context.
Login options & merge logic
pkg/cli/login/loginoptions.go, pkg/cli/login/loginoptions_test.go
Add Context string to LoginOptions; add mergeConfig(additionalConfig kclientcmdapi.Config) (*kclientcmdapi.Config, error) which rewrites additionalConfig when Context is provided, aligns cluster/authInfo with an existing context, sets CurrentContext, and delegates to cliconfig.MergeConfig; update SaveConfig to call o.mergeConfig(...); add TestMergeConfig covering multiple context scenarios.
Project context preservation
pkg/cli/project/project.go
Add logic to detect non-generated current context whose server matches target and update its namespace in-place (skip creating/merging new kubeconfig); add server URL normalization and related error handling.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Assessment against linked issues

Objective Addressed Explanation
Avoid clobbering kubeconfig contexts when re-authenticating [#283]
Support context-aware credential updates in-place [#283]

Out-of-scope changes

Code Change Explanation
Update existing-context namespace in-place (pkg/cli/project/project.go, ~+38/-10 lines) This change modifies project context handling and namespace updates during project selection; it is not part of the login/context merge objectives described in issue #283.
✨ Finishing touches
  • 📝 Generate docstrings

Comment @coderabbitai help to get the list of available commands and usage tips.

@openshift-ci
Copy link
Contributor

openshift-ci bot commented Jan 16, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: tchap
Once this PR has been reviewed and has the lgtm label, please assign ingvagabund for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Fix all issues with AI agents
In `@pkg/cli/login/login.go`:
- Line 168: NewCmdLogin reads o.Context via kcmdutil.GetFlagString in the Run
flow but never registers a "--context" flag, causing a runtime panic; fix by
registering the flag in NewCmdLogin (alongside other flags) with
cmds.Flags().StringVar tied to o.Context and a helpful usage string (e.g., "The
name of the kubeconfig context to use or create") so kcmdutil.GetFlagString can
safely read it at runtime.

In `@pkg/cli/login/loginoptions.go`:
- Around line 597-632: The mergeConfig method in LoginOptions doesn't update
ret.CurrentContext when o.Context (the --context flag) is provided, so the
active context remains unchanged; modify mergeConfig (in the LoginOptions type)
to set ret.CurrentContext = o.Context after you assign ret.Contexts[o.Context] =
&newContext (i.e., when o.Context != ""), mirroring the behavior of
cliconfig.MergeConfig so the specified context becomes the active
CurrentContext.
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Cache: Disabled due to data retention organization setting

Knowledge base: Disabled due to Reviews -> Disable Knowledge Base setting

📥 Commits

Reviewing files that changed from the base of the PR and between 430c822 and 54475e2.

📒 Files selected for processing (2)
  • pkg/cli/login/login.go
  • pkg/cli/login/loginoptions.go
🧰 Additional context used
📓 Path-based instructions (1)
**

⚙️ CodeRabbit configuration file

-Focus on major issues impacting performance, readability, maintainability and security. Avoid nitpicks and avoid verbosity.

Files:

  • pkg/cli/login/loginoptions.go
  • pkg/cli/login/login.go
🧬 Code graph analysis (1)
pkg/cli/login/loginoptions.go (1)
pkg/helpers/kubeconfig/smart_merge.go (1)
  • MergeConfig (115-153)
🔇 Additional comments (3)
pkg/cli/login/login.go (1)

47-49: LGTM!

The example clearly demonstrates the new --context flag usage.

pkg/cli/login/loginoptions.go (2)

78-78: LGTM!

Field addition is straightforward and appropriately placed.


574-577: LGTM!

Clean refactoring to delegate merge logic to the new method.

✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@pkg/cli/login/loginoptions.go`:
- Around line 604-623: When reusing an existing context (existingContext) you
update additionalConfig.Clusters/AuthInfos but you never update the Context
object to point at those reused names, leaving Context.Cluster and
Context.AuthInfo pointing to the original created names; modify the loop that
sets additionalConfig.Contexts so that for the Context object stored under key
o.Context you also set ctx.Cluster = existingContext.Cluster and ctx.AuthInfo =
existingContext.AuthInfo (i.e. update the Context value from
additionalConfig.Contexts before assigning it back), and keep the existing logic
that remaps additionalConfig.Clusters and additionalConfig.AuthInfos to the
existingContext names so the context references match the renamed cluster/auth
entries.
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Cache: Disabled due to data retention organization setting

Knowledge base: Disabled due to Reviews -> Disable Knowledge Base setting

📥 Commits

Reviewing files that changed from the base of the PR and between ea89ae0 and 2090bdc.

📒 Files selected for processing (2)
  • pkg/cli/login/login.go
  • pkg/cli/login/loginoptions.go
🧰 Additional context used
📓 Path-based instructions (1)
**

⚙️ CodeRabbit configuration file

-Focus on major issues impacting performance, readability, maintainability and security. Avoid nitpicks and avoid verbosity.

Files:

  • pkg/cli/login/login.go
  • pkg/cli/login/loginoptions.go
🧬 Code graph analysis (1)
pkg/cli/login/loginoptions.go (1)
pkg/helpers/kubeconfig/smart_merge.go (1)
  • MergeConfig (115-153)
🔇 Additional comments (4)
pkg/cli/login/loginoptions.go (2)

78-78: Context option addition looks good.


574-575: Using a dedicated merge helper is a clean upgrade.

pkg/cli/login/login.go (2)

47-49: Example addition is clear and helpful.


168-168: Context flag wiring is correct.

✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.

@tchap tchap force-pushed the oc-login-keep-ctx-name-flag branch from 2090bdc to 2855dc9 Compare January 18, 2026 11:37
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@pkg/cli/login/loginoptions.go`:
- Around line 612-614: The code currently calls panic(errors.New("no context
found in additionalConfig")) when newContext is nil; replace this panic with
returning an error (e.g., return nil, fmt.Errorf("no context found in
additionalConfig") or simply return fmt.Errorf(...)) and propagate that error up
the caller chain by updating the enclosing function's signature to return an
error if necessary; locate the check around the newContext variable in
loginoptions.go and ensure callers of that function handle and surface the
returned error to the CLI user instead of allowing a panic.

The flag can be used to tell oc what context name to use when updating
kubeconfig. If a matching context already exists, it is replaced
altogether, including the linked cluster and authInfo. This means that
the configured cluster and authInfo names are also reused.
@tchap tchap force-pushed the oc-login-keep-ctx-name-flag branch from 2855dc9 to f10a819 Compare January 18, 2026 20:55
@tchap
Copy link
Contributor Author

tchap commented Jan 18, 2026

Added some tests.

/unhold

@openshift-ci openshift-ci bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jan 18, 2026
@ardaguclu
Copy link
Member

ardaguclu commented Jan 19, 2026

@tchap I did some deep investigations today to understand why this hasn't been implemented yet. There was this openshift/origin#16161 PR which tried to adopt the similar approach with yours and it was rejected by openshift/origin#16161 (comment). The real reason is some commands in oc (i.e. oc project) rely on the oc generated context name (ref: openshift/origin#18645 (comment)). Modification of kubeconfig manually is intentionally disallowed.

When --context or the current context contain a custom context name,
reuse that context if it matches the current --server value instead of
always expecting the context match the generated context name.

The change is entirely backwards compatible. When the current context
matches the generated context name, the old functionality is retained.
@tchap tchap changed the title oc login: Implement --context flag oc login/project: Add support for custom context name Jan 19, 2026
@tchap
Copy link
Contributor Author

tchap commented Jan 19, 2026

/retest

1 similar comment
@tchap
Copy link
Contributor Author

tchap commented Jan 20, 2026

/retest

@openshift-ci
Copy link
Contributor

openshift-ci bot commented Jan 20, 2026

@tchap: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
ci/prow/e2e-aws-ovn-serial-2of2 c141ce0 link true /test e2e-aws-ovn-serial-2of2

Full PR test history. Your PR dashboard.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

@tchap
Copy link
Contributor Author

tchap commented Jan 21, 2026

Seems like this needs a minor test alignment in checking oc stdout.
Will be better to wait until #2181 is merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

oc login should avoid clobbering kubeconfig contexts

2 participants