diff --git a/pkg/cli/login/login.go b/pkg/cli/login/login.go index f7a837c546..82654de324 100644 --- a/pkg/cli/login/login.go +++ b/pkg/cli/login/login.go @@ -95,6 +95,7 @@ func NewCmdLogin(f kcmdutil.Factory, streams genericiooptions.IOStreams) *cobra. cmds.Flags().BoolVarP(&o.WebLogin, "web", "w", o.WebLogin, "Login with web browser. Starts a local HTTP callback server to perform the OAuth2 Authorization Code Grant flow. Use with caution on multi-user systems, as the server's port will be open to all users.") cmds.Flags().Int32VarP(&o.CallbackPort, "callback-port", "c", o.CallbackPort, "Port for the callback server when using --web. Defaults to a random open port") + cmds.Flags().BoolVar(&o.KeepCtxName, "no-clobber", o.KeepCtxName, "Experimental: Keep the current context name in the kubeconfig file.") cmds.Flags().StringVar(&o.OIDCExecPluginType, "exec-plugin", o.OIDCExecPluginType, "Experimental: Specify credentials exec plugin type to be used to authenticate external OIDC issuer. Currently only 'oc-oidc' is supported") cmds.Flags().StringVar(&o.OIDCClientID, "client-id", o.OIDCClientID, "Experimental: Client ID for external OIDC issuer. Only supports Auth Code + PKCE. Required.") cmds.Flags().StringVar(&o.OIDCClientSecret, "client-secret", o.OIDCClientSecret, "Experimental: Client secret for external OIDC issuer. Optional.") diff --git a/pkg/cli/login/loginoptions.go b/pkg/cli/login/loginoptions.go index 75ffe281c8..900318a56e 100644 --- a/pkg/cli/login/loginoptions.go +++ b/pkg/cli/login/loginoptions.go @@ -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" 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] + newConfig.Contexts = map[string]*api.Context{o.StartingKubeConfig.CurrentContext: newContext} + newConfig.CurrentContext = o.StartingKubeConfig.CurrentContext + } cwd, err := os.Getwd() if err != nil {