INNER CODE UNIT · Go
func
sunny0826/kubecm · cmd/add.go:148
func (kc *KubeConfigOption) handleContexts(oldConfig *clientcmdapi.Config, contextPrefix string, selectContext bool, contextTemplate []string, context []string) (*clientcmdapi.Config, error) {
newConfig := clientcmdapi.NewConfig()
var newName string
generatedName := make(map[string]int)
for name, ctx := range kc.config.Contexts {
newName = kc.generateContextName(name, ctx, contextTemplate)
if contextPrefix != "" {
newName = strings.TrimSuffix(fmt.Sprintf("%s-%s", contextPrefix, newName), "-")
}
// prevent generate duplicate context name
// for example: set --context-template user,cluster, the context1 and context2 have the same user and cluster
generatedName[newName]++
if generatedName[newName] > 1 {
newName = fmt.Sprintf("%s-%d", newName, generatedName[newName])
}