-
Notifications
You must be signed in to change notification settings - Fork 429
Fix for 'oc login should avoid clobbering kubeconfig contexts #283' #2185
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
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 | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -30,6 +30,7 @@ import ( | |||||||||||||||||||
| "k8s.io/client-go/pkg/apis/clientauthentication" | ||||||||||||||||||||
| restclient "k8s.io/client-go/rest" | ||||||||||||||||||||
| kclientcmd "k8s.io/client-go/tools/clientcmd" | ||||||||||||||||||||
| "k8s.io/client-go/tools/clientcmd/api" | ||||||||||||||||||||
|
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. 🧩 Analysis chain🏁 Script executed: # First, find the file
find . -name "loginoptions.go" -type fRepository: openshift/oc Length of output: 87 🏁 Script executed: # Read the imports section (first ~50 lines) to see the imports
head -50 ./pkg/cli/login/loginoptions.go | cat -nRepository: openshift/oc Length of output: 1864 🏁 Script executed: # Check the usage around line 562-565 to see if api.Context is being used
sed -n '555,570p' ./pkg/cli/login/loginoptions.go | cat -nRepository: openshift/oc Length of output: 623 🏁 Script executed: # Let's verify if this file can compile by checking if there are any compilation errors
# Try to get the full file to see the context better
wc -l ./pkg/cli/login/loginoptions.goRepository: openshift/oc Length of output: 91 🏁 Script executed: # Let's verify the complete imports and usage context
# Check if there's a go.mod file and try to understand the module structure
head -n 100 ./pkg/cli/login/loginoptions.go | tail -n 50Repository: openshift/oc Length of output: 1338 🏁 Script executed: # Search for usage of "api." in the file to see what's using the non-aliased import
grep -n "api\." ./pkg/cli/login/loginoptions.goRepository: openshift/oc Length of output: 506 🏁 Script executed: # Let's verify this is indeed a Go compilation error by checking if there's a way to test this
# Search for any other uses of the non-aliased "api" that might be imported
grep -n " api\." ./pkg/cli/login/loginoptions.go | head -20Repository: openshift/oc Length of output: 38 🏁 Script executed: # Let's search for the exact error by looking at the grep output more carefully
# Check if line 564 specifically uses "api.Context"
sed -n '564p' ./pkg/cli/login/loginoptions.go | cat -nRepository: openshift/oc Length of output: 158 🏁 Script executed: # Verify: Is there any use of just "api." (not kclientcmdapi.) in the file?
grep -E '\bapi\.[A-Z]' ./pkg/cli/login/loginoptions.goRepository: openshift/oc Length of output: 151 Duplicate import will cause compilation failure. Lines 33-34 both import 🛠️ Suggested fix- "k8s.io/client-go/tools/clientcmd/api"
@@
- newConfig.Contexts = map[string]*api.Context{o.StartingKubeConfig.CurrentContext: newContext}
+ newConfig.Contexts = map[string]*kclientcmdapi.Context{o.StartingKubeConfig.CurrentContext: newContext}🤖 Prompt for AI Agents |
||||||||||||||||||||
| kclientcmdapi "k8s.io/client-go/tools/clientcmd/api" | ||||||||||||||||||||
| "k8s.io/klog/v2" | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
@@ -70,6 +71,7 @@ type LoginOptions struct { | |||||||||||||||||||
| Project string | ||||||||||||||||||||
| WebLogin bool | ||||||||||||||||||||
| CallbackPort int32 | ||||||||||||||||||||
| KeepCtxName bool | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // infra | ||||||||||||||||||||
| StartingKubeConfig *kclientcmdapi.Config | ||||||||||||||||||||
|
|
@@ -557,6 +559,11 @@ func (o *LoginOptions) SaveConfig() (bool, error) { | |||||||||||||||||||
| if err != nil { | ||||||||||||||||||||
| return false, err | ||||||||||||||||||||
| } | ||||||||||||||||||||
| if o.KeepCtxName { | ||||||||||||||||||||
| newContext := newConfig.Contexts[newConfig.CurrentContext] | ||||||||||||||||||||
|
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. Could this take the context from the Thanks! 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. ..likely resolved by #2187 |
||||||||||||||||||||
| newConfig.Contexts = map[string]*api.Context{o.StartingKubeConfig.CurrentContext: newContext} | ||||||||||||||||||||
| newConfig.CurrentContext = o.StartingKubeConfig.CurrentContext | ||||||||||||||||||||
|
Comment on lines
+562
to
+565
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. Guard If 🛠️ Suggested fix- if o.KeepCtxName {
+ if o.KeepCtxName && o.StartingKubeConfig != nil && o.StartingKubeConfig.CurrentContext != "" {
newContext := newConfig.Contexts[newConfig.CurrentContext]
newConfig.Contexts = map[string]*api.Context{o.StartingKubeConfig.CurrentContext: newContext}
newConfig.CurrentContext = o.StartingKubeConfig.CurrentContext
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| cwd, err := os.Getwd() | ||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||
|
|
||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for opening a PR to fix this. But we would not prefer a new flag for this functionality.