Skip to content

Commit ea89ae0

Browse files
committed
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 cluster and authInfo names are also reused.
1 parent 4df0e94 commit ea89ae0

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

pkg/cli/login/login.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ var (
4444
# Log in to the given server with the given credentials (will not prompt interactively)
4545
oc login localhost:8443 --username=myuser --password=mypass
4646
47+
# Log in to the given server as myuser and use a custom kubeconfig context name.
48+
oc login localhost:8443 --username=myuser --context=local
49+
4750
# Log in to the given server through a browser
4851
oc login localhost:8443 --web --callback-port 8280
4952
@@ -162,6 +165,7 @@ func (o *LoginOptions) Complete(f kcmdutil.Factory, cmd *cobra.Command, args []s
162165
o.Token = kcmdutil.GetFlagString(cmd, "token")
163166

164167
o.DefaultNamespace, _, _ = f.ToRawKubeConfigLoader().Namespace()
168+
o.Context = kcmdutil.GetFlagString(cmd, "context")
165169

166170
o.PathOptions = kclientcmd.NewDefaultPathOptions()
167171
// we need to set explicit path if one was specified, since NewDefaultPathOptions doesn't do it for us

pkg/cli/login/loginoptions.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ type LoginOptions struct {
7575
StartingKubeConfig *kclientcmdapi.Config
7676
DefaultNamespace string
7777
Config *restclient.Config
78+
Context string
7879

7980
// cert data to be used when authenticating
8081
CertFile string
@@ -570,7 +571,7 @@ func (o *LoginOptions) SaveConfig() (bool, error) {
570571
return false, err
571572
}
572573

573-
configToWrite, err := cliconfig.MergeConfig(*o.StartingKubeConfig, *newConfig)
574+
configToWrite, err := o.mergeConfig(*newConfig)
574575
if err != nil {
575576
return false, err
576577
}
@@ -593,6 +594,44 @@ func (o *LoginOptions) SaveConfig() (bool, error) {
593594
return created, nil
594595
}
595596

597+
func (o *LoginOptions) mergeConfig(additionalConfig kclientcmdapi.Config) (*kclientcmdapi.Config, error) {
598+
if o.Context == "" {
599+
return cliconfig.MergeConfig(*o.StartingKubeConfig, additionalConfig)
600+
}
601+
602+
// Unwrap additionalConfig. There is expected to be just a single context, cluster and authInfo.
603+
var (
604+
newContext kclientcmdapi.Context
605+
newCluster kclientcmdapi.Cluster
606+
newAuthInfo kclientcmdapi.AuthInfo
607+
)
608+
for _, v := range additionalConfig.Contexts {
609+
newContext = *v
610+
}
611+
for _, v := range additionalConfig.Clusters {
612+
newCluster = *v
613+
}
614+
for _, v := range additionalConfig.AuthInfos {
615+
newAuthInfo = *v
616+
}
617+
618+
ret := *o.StartingKubeConfig
619+
620+
// If the selected context exists, replace the linked cluster and authInfo.
621+
// When there is no match, we use the new objects, but o.Context is still used as the new context name.
622+
existingContext, ok := ret.Contexts[o.Context]
623+
if ok {
624+
newContext.Cluster = existingContext.Cluster
625+
newContext.AuthInfo = existingContext.AuthInfo
626+
}
627+
628+
ret.Clusters[newContext.Cluster] = &newCluster
629+
ret.AuthInfos[newContext.AuthInfo] = &newAuthInfo
630+
ret.Contexts[o.Context] = &newContext
631+
ret.CurrentContext = o.Context
632+
return &ret, nil
633+
}
634+
596635
func (o *LoginOptions) whoAmI(clientConfig *restclient.Config) (*userv1.User, error) {
597636
if o.whoAmIFunc != nil {
598637
return o.whoAmIFunc(clientConfig)

0 commit comments

Comments
 (0)