Skip to content

Commit

Permalink
Keep user and cluster name when add kubeconfig (#536)
Browse files Browse the repository at this point in the history
  • Loading branch information
astraw99 committed Sep 8, 2022
1 parent c32d19e commit cd223f7
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 30 deletions.
51 changes: 43 additions & 8 deletions cmd/add.go
Expand Up @@ -129,7 +129,7 @@ func (kc *KubeConfigOption) handleContexts(oldConfig *clientcmdapi.Config) (*cli
continue
}
}
itemConfig := kc.handleContext(newName, ctx)
itemConfig := kc.handleContext(oldConfig, newName, ctx)
newConfig = appendConfig(newConfig, itemConfig)
fmt.Printf("Add Context: %s \n", newName)
}
Expand All @@ -144,17 +144,52 @@ func checkContextName(name string, oldConfig *clientcmdapi.Config) bool {
return false
}

func (kc *KubeConfigOption) handleContext(key string, ctx *clientcmdapi.Context) *clientcmdapi.Config {
func checkClusterAndUserName(oldConfig *clientcmdapi.Config, newClusterName, newUserName string) (bool, bool) {
var (
isClusterNameExist bool
isUserNameExist bool
)

for _, ctx := range oldConfig.Contexts {
if ctx.Cluster == newClusterName {
isClusterNameExist = true
}
if ctx.AuthInfo == newUserName {
isUserNameExist = true
}
}

return isClusterNameExist, isUserNameExist
}

func (kc *KubeConfigOption) handleContext(oldConfig *clientcmdapi.Config,
name string, ctx *clientcmdapi.Context) *clientcmdapi.Config {

var (
clusterNameSuffix string
userNameSuffix string
)

isClusterNameExist, isUserNameExist := checkClusterAndUserName(oldConfig, ctx.Cluster, ctx.AuthInfo)
newConfig := clientcmdapi.NewConfig()
suffix := HashSufString(key)
userName := fmt.Sprintf("user-%v", suffix)
clusterName := fmt.Sprintf("cluster-%v", suffix)
suffix := HashSufString(name)

if isClusterNameExist {
clusterNameSuffix = "-" + suffix
}
if isUserNameExist {
userNameSuffix = "-" + suffix
}

userName := fmt.Sprintf("%v%v", ctx.AuthInfo, userNameSuffix)
clusterName := fmt.Sprintf("%v%v", ctx.Cluster, clusterNameSuffix)
newCtx := ctx.DeepCopy()
newConfig.AuthInfos[userName] = kc.config.AuthInfos[newCtx.AuthInfo]
newConfig.Clusters[clusterName] = kc.config.Clusters[newCtx.Cluster]
newConfig.Contexts[key] = newCtx
newConfig.Contexts[key].AuthInfo = userName
newConfig.Contexts[key].Cluster = clusterName
newConfig.Contexts[name] = newCtx
newConfig.Contexts[name].AuthInfo = userName
newConfig.Contexts[name].Cluster = clusterName

return newConfig
}

Expand Down
64 changes: 42 additions & 22 deletions cmd/add_test.go
Expand Up @@ -10,13 +10,18 @@ var (
addTestConfig = clientcmdapi.Config{
AuthInfos: map[string]*clientcmdapi.AuthInfo{
"black-user": {Token: "black-token"},
"red-user": {Token: "red-token"}},
"red-user": {Token: "red-token"},
"not-exist": {Token: "not-exist-token"},
},
Clusters: map[string]*clientcmdapi.Cluster{
"pig-cluster": {Server: "http://pig.org:8080"},
"cow-cluster": {Server: "http://cow.org:8080"}},
"cow-cluster": {Server: "http://cow.org:8080"},
"not-exist": {Server: "http://not.exist:8080"},
},
Contexts: map[string]*clientcmdapi.Context{
"root-context": {AuthInfo: "black-user", Cluster: "pig-cluster", Namespace: "saw-ns"},
"federal-context": {AuthInfo: "red-user", Cluster: "cow-cluster", Namespace: "hammer-ns"},
"root-context": {AuthInfo: "black-user", Cluster: "pig-cluster", Namespace: "saw-ns"},
"federal-context": {AuthInfo: "red-user", Cluster: "cow-cluster", Namespace: "hammer-ns"},
"not-exist-context": {AuthInfo: "not-exist", Cluster: "not-exist", Namespace: "not-exist-ns"},
},
}
oldTestConfig = clientcmdapi.Config{
Expand All @@ -33,30 +38,43 @@ var (
}
handleConfig = clientcmdapi.Config{
AuthInfos: map[string]*clientcmdapi.AuthInfo{
"user-cbc897d6ch": {Token: "red-token"}},
"red-user-cbc897d6ch": {Token: "red-token"}},
Clusters: map[string]*clientcmdapi.Cluster{
"cow-cluster-cbc897d6ch": {Server: "http://cow.org:8080"}},
Contexts: map[string]*clientcmdapi.Context{
"federal-context": {AuthInfo: "red-user-cbc897d6ch", Cluster: "cow-cluster-cbc897d6ch", Namespace: "hammer-ns"},
},
}
handleNotExistConfig = clientcmdapi.Config{
AuthInfos: map[string]*clientcmdapi.AuthInfo{
"not-exist": {Token: "not-exist-token"}},
Clusters: map[string]*clientcmdapi.Cluster{
"cluster-cbc897d6ch": {Server: "http://cow.org:8080"}},
"not-exist": {Server: "http://not.exist:8080"}},
Contexts: map[string]*clientcmdapi.Context{
"federal-context": {AuthInfo: "user-cbc897d6ch", Cluster: "cluster-cbc897d6ch", Namespace: "hammer-ns"}},
"not-exist-context": {AuthInfo: "not-exist", Cluster: "not-exist", Namespace: "not-exist-ns"},
},
}
mergedConfig = clientcmdapi.Config{
AuthInfos: map[string]*clientcmdapi.AuthInfo{
"black-user": {Token: "black-token"},
"red-user": {Token: "red-token"},
"user-7f65b9cc8f": {Token: "red-token"},
"user-gtch2cf96d": {Token: "black-token"},
"black-user": {Token: "black-token"},
"red-user": {Token: "red-token"},
"red-user-7f65b9cc8f": {Token: "red-token"},
"black-user-gtch2cf96d": {Token: "black-token"},
"not-exist": {Token: "not-exist-token"},
},
Clusters: map[string]*clientcmdapi.Cluster{
"pig-cluster": {Server: "http://pig.org:8080"},
"cow-cluster": {Server: "http://cow.org:8080"},
"cluster-7f65b9cc8f": {Server: "http://cow.org:8080"},
"cluster-gtch2cf96d": {Server: "http://pig.org:8080"},
"pig-cluster": {Server: "http://pig.org:8080"},
"cow-cluster": {Server: "http://cow.org:8080"},
"cow-cluster-7f65b9cc8f": {Server: "http://cow.org:8080"},
"pig-cluster-gtch2cf96d": {Server: "http://pig.org:8080"},
"not-exist": {Server: "http://not.exist:8080"},
},
Contexts: map[string]*clientcmdapi.Context{
"root": {AuthInfo: "black-user", Cluster: "pig-cluster", Namespace: "saw-ns"},
"federal": {AuthInfo: "red-user", Cluster: "cow-cluster", Namespace: "hammer-ns"},
"test-d2m9fd8b7d": {AuthInfo: "user-gtch2cf96d", Cluster: "cluster-gtch2cf96d", Namespace: "saw-ns"},
"test-cbc897d6ch": {AuthInfo: "user-7f65b9cc8f", Cluster: "cluster-7f65b9cc8f", Namespace: "hammer-ns"},
"test-d2m9fd8b7d": {AuthInfo: "black-user-gtch2cf96d", Cluster: "pig-cluster-gtch2cf96d", Namespace: "saw-ns"},
"test-cbc897d6ch": {AuthInfo: "red-user-7f65b9cc8f", Cluster: "cow-cluster-7f65b9cc8f", Namespace: "hammer-ns"},
"test-2h6782585t": {AuthInfo: "not-exist", Cluster: "not-exist", Namespace: "not-exist-ns"},
},
}
)
Expand All @@ -72,8 +90,8 @@ func Test_checkContextName(t *testing.T) {
want bool
}{
// TODO: Add more test cases.
{"exit", args{name: "root-context", oldConfig: &addTestConfig}, true},
{"not-exit", args{name: "test", oldConfig: &addTestConfig}, false},
{"exist", args{name: "root-context", oldConfig: &addTestConfig}, true},
{"not-exist", args{name: "test", oldConfig: &addTestConfig}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -87,6 +105,7 @@ func Test_checkContextName(t *testing.T) {
func TestKubeConfig_handleContext(t *testing.T) {
newConfig := addTestConfig.DeepCopy()
testCtx := clientcmdapi.Context{AuthInfo: "red-user", Cluster: "cow-cluster", Namespace: "hammer-ns"}
testNotExistCtx := clientcmdapi.Context{AuthInfo: "not-exist", Cluster: "not-exist", Namespace: "not-exist-ns"}

type fields struct {
config *clientcmdapi.Config
Expand All @@ -103,14 +122,15 @@ func TestKubeConfig_handleContext(t *testing.T) {
}{
// TODO: Add more test cases.
{"one", fields{config: newConfig}, args{"federal-context", &testCtx}, &handleConfig},
{"two", fields{config: newConfig}, args{"not-exist-context", &testNotExistCtx}, &handleNotExistConfig},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
kc := &KubeConfigOption{
config: tt.fields.config,
}
got := kc.handleContext(tt.args.key, tt.args.ctx)
checkConfig(got, tt.want, t)
got := kc.handleContext(&oldTestConfig, tt.args.key, tt.args.ctx)
checkConfig(tt.want, got, t)
})
}
}
Expand Down Expand Up @@ -145,7 +165,7 @@ func TestKubeConfig_handleContexts(t *testing.T) {
t.Errorf("handleContexts() error = %v, wantErr %v", err, tt.wantErr)
return
}
checkConfig(got, tt.want, t)
checkConfig(tt.want, got, t)
//if !reflect.DeepEqual(got, tt.want) {
// t.Errorf("handleContexts() got = %v, want %v", got, tt.want)
//}
Expand Down

0 comments on commit cd223f7

Please sign in to comment.