Skip to content

Commit

Permalink
Fix merge (#36)
Browse files Browse the repository at this point in the history
* Push back

* update add&merge
  • Loading branch information
sunny0826 committed Jan 19, 2021
1 parent 183f3b8 commit 14985c5
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 26 deletions.
31 changes: 19 additions & 12 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ type AddCommand struct {
BaseCommand
}

type KubeConfig struct {
config *clientcmdapi.Config
type KubeConfigOption struct {
config *clientcmdapi.Config
fileName string
}

// Init AddCommand
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
24 changes: 13 additions & 11 deletions cmd/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
},
}
)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions cmd/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
29 changes: 29 additions & 0 deletions cmd/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cmd

import (
"fmt"
"io/ioutil"
"os"
"os/user"
"reflect"
"testing"
Expand Down Expand Up @@ -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)
}
})
}
}

0 comments on commit 14985c5

Please sign in to comment.