Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkg/oc/cli/cmd/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ func (o *LoginOptions) Complete(f *osclientcmd.Factory, cmd *cobra.Command, args
o.CommandName = "oc"
}

context := kcmdutil.GetFlagString(cmd, "context")
if len(context) > 0 {
o.StartingKubeConfig.CurrentContext = context
}

parsedDefaultClusterURL, err := url.Parse(defaultClusterURL)
if err != nil {
return err
Expand Down
29 changes: 28 additions & 1 deletion pkg/oc/cli/config/smart_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,35 @@ func MergeConfig(startingConfig, addition clientcmdapi.Config) (*clientcmdapi.Co
ret.AuthInfos[requestedKey] = value
}

resolvedCurrentContext := addition.CurrentContext

requestedContextNamesToActualContextNames := map[string]string{}
for requestedKey, newContext := range addition.Contexts {
// only add the new context if there is no existing context
// with the same authInfo and cluster info - regardless of context name.
ctxInfoExists := false
for existingCtxName, existingContext := range startingConfig.Contexts {
if existingContext.AuthInfo == newContext.AuthInfo &&
existingContext.Cluster == newContext.Cluster {
// if we are skipping the currentContext of the config we are adding on top of
// the existing config, update the resolvedCurrentContext to the name of the
// existing context that shares the same authInfo and clusterName as the newContext.
if requestedKey == addition.CurrentContext {
resolvedCurrentContext = existingCtxName
}

// copy existing namespace and extensions from what would have been the new context
existingContext.Namespace = newContext.Namespace
existingContext.Extensions = newContext.Extensions

ctxInfoExists = true
break
}
}
if ctxInfoExists {
continue
}

actualContext := clientcmdapi.NewContext()
actualContext.AuthInfo = newContext.AuthInfo
actualContext.Cluster = newContext.Cluster
Expand All @@ -186,7 +213,7 @@ func MergeConfig(startingConfig, addition clientcmdapi.Config) (*clientcmdapi.Co
if newCurrentContext, exists := requestedContextNamesToActualContextNames[addition.CurrentContext]; exists {
ret.CurrentContext = newCurrentContext
} else {
ret.CurrentContext = addition.CurrentContext
ret.CurrentContext = resolvedCurrentContext
}
}

Expand Down