package kubeconfig

import "html/template"

const (
	tokenTemplateText = `apiVersion: v1
kind: Config
clusters:
{{- range .Nodes}}
- name: "{{.ClusterName}}"
  cluster:
    server: "{{.Server}}"
{{- if ne .Cert "" }}
    certificate-authority-data: "{{.Cert}}"
{{- end }}
{{- end}}

users:
- name: "{{.ClusterName}}"
  user:
    token: "{{.Token}}"

contexts:
{{- range .Nodes}}
- name: "{{.ClusterName}}"
  context:
    user: "{{.ClusterName}}"
    cluster: "{{.ClusterName}}"
{{- end}}

current-context: "{{.ClusterName}}"
`

	basicTemplateText = `apiVersion: v1
kind: Config
clusters:
- name: "{{.ClusterName}}"
  cluster:
    server: "https://{{.Host}}"
    api-version: v1

users:
- name: "{{.ClusterName}}"
  user:
    username: "{{.Username}}"
    password: "{{.Password}}"

contexts:
- name: "{{.ClusterName}}"
  context:
    user: "{{.ClusterName}}"
    cluster: "{{.ClusterName}}"

current-context: "{{.ClusterName}}"
`
)

var (
	basicTemplate = template.Must(template.New("basicTemplate").Parse(basicTemplateText))
	tokenTemplate = template.Must(template.New("tokenTemplate").Parse(tokenTemplateText))
)

package kubeconfig

import "html/template"

const (
	tokenTemplateText = `apiVersion: v1
kind: Config
clusters:
{{- range .Nodes}}
- name: "{{.ClusterName}}"
  cluster:
    server: "{{.Server}}"
{{- if ne .Cert "" }}
    certificate-authority-data: "{{.Cert}}"
{{- end }}
{{- end}}

users:
- name: "{{.User}}"
  user:
    token: "{{.Token}}"

contexts:
{{- range .Nodes}}
- name: "{{.ClusterName}}"
  context:
    user: "{{.User}}"
    cluster: "{{.ClusterName}}"
{{- end}}

current-context: "{{.ClusterName}}"
`

	basicTemplateText = `apiVersion: v1
kind: Config
clusters:
- name: "{{.ClusterName}}"
  cluster:
    server: "https://{{.Host}}"
    api-version: v1

users:
- name: "{{.User}}"
  user:
    username: "{{.Username}}"
    password: "{{.Password}}"

contexts:
- name: "{{.ClusterName}}"
  context:
    user: "{{.User}}"
    cluster: "{{.ClusterName}}"

current-context: "{{.ClusterName}}"
`
)

var (
	basicTemplate = template.Must(template.New("basicTemplate").Parse(basicTemplateText))
	tokenTemplate = template.Must(template.New("tokenTemplate").Parse(tokenTemplateText))
)

--- 
+++ 
@@ -16,7 +16,7 @@
 {{- end}}
 
 users:
-- name: "{{.User}}"
+- name: "{{.ClusterName}}"
   user:
     token: "{{.Token}}"
 
@@ -24,7 +24,7 @@
 {{- range .Nodes}}
 - name: "{{.ClusterName}}"
   context:
-    user: "{{.User}}"
+    user: "{{.ClusterName}}"
     cluster: "{{.ClusterName}}"
 {{- end}}
 
@@ -40,7 +40,7 @@
     api-version: v1
 
 users:
-- name: "{{.User}}"
+- name: "{{.ClusterName}}"
   user:
     username: "{{.Username}}"
     password: "{{.Password}}"
@@ -48,7 +48,7 @@
 contexts:
 - name: "{{.ClusterName}}"
   context:
-    user: "{{.User}}"
+    user: "{{.ClusterName}}"
     cluster: "{{.ClusterName}}"
 
 current-context: "{{.ClusterName}}"
Use ClusterName for KubeConfig User's name

Currently the user name is always the same for every config you generate
(`u-xxxxx`), so the kubeconfigs for 2 clusters cannot be merged together.

Instead, Generate kubeconfigs with the User, Context, and Cluster name all
set to the cluster `name`, so that the configs of any two clusters (with
different names...) can be merged together successfully by kubectl.
