diff --git a/cmd/add.go b/cmd/add.go index 2ffef4c0..87fb7b35 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -15,8 +15,9 @@ type AddCommand struct { BaseCommand } -type KubeConfig struct { - config *clientcmdapi.Config +type KubeConfigOption struct { + config *clientcmdapi.Config + fileName string } // Init AddCommand @@ -49,11 +50,12 @@ func (ac *AddCommand) runAdd(cmd *cobra.Command, args []string) error { if err != nil { return err } - kubeConfig := &KubeConfig{ - config: newConfig, + kco := &KubeConfigOption{ + config: newConfig, + fileName: getFileName(file), } // merge context loop - outConfig, err := kubeConfig.handleContexts(oldConfig) + outConfig, err := kco.handleContexts(oldConfig) if err != nil { return err } @@ -74,15 +76,20 @@ func (ac *AddCommand) runAdd(cmd *cobra.Command, args []string) error { return nil } -func (kc *KubeConfig) handleContexts(oldConfig *clientcmdapi.Config) (*clientcmdapi.Config, error) { +func (kc *KubeConfigOption) handleContexts(oldConfig *clientcmdapi.Config) (*clientcmdapi.Config, error) { newConfig := clientcmdapi.NewConfig() for name, ctx := range kc.config.Contexts { - newName := name - if checkContextName(name, oldConfig) { - nameConfirm := BoolUI(fmt.Sprintf("「%s」 Name already exists, do you want to rename it. (If you select `False`, this context will not be merged)", name)) + var newName string + if len(kc.config.Contexts) > 1 { + newName = fmt.Sprintf("%s-%s", kc.fileName, HashSufString(name)) + } else { + newName = kc.fileName + } + if checkContextName(newName, oldConfig) { + nameConfirm := BoolUI(fmt.Sprintf("「%s」 Name already exists, do you want to rename it. (If you select `False`, this context will not be merged)", newName)) if nameConfirm == "True" { - newName = PromptUI("Rename", name) - if newName == name { + newName = PromptUI("Rename", newName) + if newName == kc.fileName { return nil, errors.New("need to rename") } } else { @@ -104,7 +111,7 @@ func checkContextName(name string, oldConfig *clientcmdapi.Config) bool { return false } -func (kc *KubeConfig) handleContext(key string, ctx *clientcmdapi.Context) *clientcmdapi.Config { +func (kc *KubeConfigOption) handleContext(key string, ctx *clientcmdapi.Context) *clientcmdapi.Config { newConfig := clientcmdapi.NewConfig() suffix := HashSufString(key) userName := fmt.Sprintf("user-%v", suffix) diff --git a/cmd/add_test.go b/cmd/add_test.go index 78059401..de59586f 100644 --- a/cmd/add_test.go +++ b/cmd/add_test.go @@ -43,20 +43,20 @@ var ( AuthInfos: map[string]*clientcmdapi.AuthInfo{ "black-user": {Token: "black-token"}, "red-user": {Token: "red-token"}, - "user-cbc897d6ch": {Token: "red-token"}, - "user-d2m9fd8b7d": {Token: "black-token"}, + "user-7f65b9cc8f": {Token: "red-token"}, + "user-gtch2cf96d": {Token: "black-token"}, }, Clusters: map[string]*clientcmdapi.Cluster{ "pig-cluster": {Server: "http://pig.org:8080"}, "cow-cluster": {Server: "http://cow.org:8080"}, - "cluster-cbc897d6ch": {Server: "http://cow.org:8080"}, - "cluster-d2m9fd8b7d": {Server: "http://pig.org:8080"}, + "cluster-7f65b9cc8f": {Server: "http://cow.org:8080"}, + "cluster-gtch2cf96d": {Server: "http://pig.org: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"}, - "root-context": {AuthInfo: "user-d2m9fd8b7d", Cluster: "cluster-d2m9fd8b7d", Namespace: "saw-ns"}, - "federal-context": {AuthInfo: "user-cbc897d6ch", Cluster: "cluster-cbc897d6ch", 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"}, }, } ) @@ -106,7 +106,7 @@ func TestKubeConfig_handleContext(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - kc := &KubeConfig{ + kc := &KubeConfigOption{ config: tt.fields.config, } got := kc.handleContext(tt.args.key, tt.args.ctx) @@ -118,7 +118,8 @@ func TestKubeConfig_handleContext(t *testing.T) { func TestKubeConfig_handleContexts(t *testing.T) { newConfig := addTestConfig.DeepCopy() type fields struct { - config *clientcmdapi.Config + config *clientcmdapi.Config + fileName string } type args struct { oldConfig *clientcmdapi.Config @@ -131,12 +132,13 @@ func TestKubeConfig_handleContexts(t *testing.T) { wantErr bool }{ // TODO: Add test cases. - {"test", fields{config: newConfig}, args{&oldTestConfig}, &mergedConfig, false}, + {"test", fields{config: newConfig, fileName: "test"}, args{&oldTestConfig}, &mergedConfig, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - kc := &KubeConfig{ - config: tt.fields.config, + kc := &KubeConfigOption{ + config: tt.fields.config, + fileName: tt.fields.fileName, } got, err := kc.handleContexts(tt.args.oldConfig) if (err != nil) != tt.wantErr { diff --git a/cmd/merge.go b/cmd/merge.go index d32abd32..7e448ecd 100644 --- a/cmd/merge.go +++ b/cmd/merge.go @@ -47,10 +47,11 @@ func (mc MergeCommand) runMerge(command *cobra.Command, args []string) error { if err != nil { return err } - kubeConfig := &KubeConfig{ - config: loadConfig, + kco := &KubeConfigOption{ + config: loadConfig, + fileName: getFileName(yaml), } - outConfigs, err = kubeConfig.handleContexts(outConfigs) + outConfigs, err = kco.handleContexts(outConfigs) if err != nil { return err } diff --git a/cmd/utils.go b/cmd/utils.go index e0da9ae4..559456c6 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -388,3 +388,9 @@ func CheckValidContext(config *clientcmdapi.Config) *clientcmdapi.Config { } return config } + +func getFileName(path string) string { + n := strings.Split(path, "/") + result := strings.Split(n[len(n)-1], ".") + return result[0] +} diff --git a/cmd/utils_test.go b/cmd/utils_test.go index c32415f3..d5799e7e 100644 --- a/cmd/utils_test.go +++ b/cmd/utils_test.go @@ -1,6 +1,9 @@ package cmd import ( + "fmt" + "io/ioutil" + "os" "os/user" "reflect" "testing" @@ -238,3 +241,29 @@ func checkConfig(want, got *clientcmdapi.Config, t *testing.T) { t.Errorf("expected: %#v\n actual: %#v", want, got) } } + +func Test_getFileName(t *testing.T) { + tempDir, _ := ioutil.TempDir("", "kubecm-get-file-") + defer os.RemoveAll(tempDir) + tempFilePath := fmt.Sprintf("%s/%s", tempDir, "testPath") + _ = ioutil.WriteFile(tempFilePath, []byte{}, 0666) + + type args struct { + path string + } + tests := []struct { + name string + args args + want string + }{ + // TODO: Add test cases. + {"TestFileName", args{path: tempFilePath}, "testPath"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := getFileName(tt.args.path); got != tt.want { + t.Errorf("getFileName() = %v, want %v", got, tt.want) + } + }) + } +}