Skip to content

Commit

Permalink
Merge pull request #22 from sunny0826/project-0.13.1
Browse files Browse the repository at this point in the history
feature of #18
  • Loading branch information
sunny0826 authored Dec 30, 2020
2 parents 917191c + 30b0942 commit 00572ee
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 8 deletions.
1 change: 1 addition & 0 deletions cmd/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (mc MergeCommand) runMerge(command *cobra.Command, args []string) error {
files := listFile(folder)
mc.command.Printf("Loading kubeconfig file: %v \n", files)
configs := clientcmdapi.NewConfig()
// TODO 还原合并逻辑,使其与 add 相同
for _, yaml := range files {
config, err := clientcmd.LoadFromFile(yaml)
if err != nil {
Expand Down
55 changes: 47 additions & 8 deletions cmd/switch.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package cmd

import (
"errors"
"fmt"

clientcmdapi "k8s.io/client-go/tools/clientcmd/api"

"github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"
)
Expand All @@ -19,6 +24,12 @@ func (sc *SwitchCommand) Init() {
Switch Kube Context interactively
`,
Aliases: []string{"s"},
Args: func(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
return errors.New("no support for more than 1 parameter")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return sc.runSwitch(cmd, args)
},
Expand All @@ -31,6 +42,35 @@ func (sc *SwitchCommand) runSwitch(command *cobra.Command, args []string) error
if err != nil {
return err
}
switch len(args) {
case 0:
config, err = handleOperation(config)
if err != nil {
return err
}
case 1:
config, err = handleQuickSwitch(config, args[0])
if err != nil {
return err
}
}
err = WriteConfig(true, cfgFile, config)
if err != nil {
return err
}
fmt.Printf("Switched to context 「%s」\n", config.CurrentContext)
return nil
}

func handleQuickSwitch(config *clientcmdapi.Config, name string) (*clientcmdapi.Config, error) {
if _, ok := config.Contexts[name]; !ok {
return config, errors.New("cannot find context named 「" + name + "」")
}
config.CurrentContext = name
return config, nil
}

func handleOperation(config *clientcmdapi.Config) (*clientcmdapi.Config, error) {
var kubeItems []Needle
current := config.CurrentContext
for key, obj := range config.Contexts {
Expand All @@ -41,24 +81,23 @@ func (sc *SwitchCommand) runSwitch(command *cobra.Command, args []string) error
}
}
// exit option
kubeItems, err = ExitOption(kubeItems)
kubeItems, err := ExitOption(kubeItems)
if err != nil {
return err
return config, err
}
num := SelectUI(kubeItems, "Select Kube Context")
kubeName := kubeItems[num].Name
config.CurrentContext = kubeName
err = WriteConfig(true, cfgFile, config)
if err != nil {
return err
}
sc.command.Printf("Switched to context 「%s」\n", config.CurrentContext)
return nil
return config, nil
}

//TODO need update docs

func switchExample() string {
return `
# Switch Kube Context interactively
kubecm switch
# Quick switch Kube Context
kubecm switch dev
`
}
57 changes: 57 additions & 0 deletions cmd/switch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package cmd

import (
"reflect"
"testing"

clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)

var (
switchConfig = clientcmdapi.Config{
AuthInfos: map[string]*clientcmdapi.AuthInfo{
"black-user": {Token: "black-token"}},
Clusters: map[string]*clientcmdapi.Cluster{
"pig-cluster": {Server: "http://pig.org:8080"}},
Contexts: map[string]*clientcmdapi.Context{
"root-context": {AuthInfo: "black-user", Cluster: "pig-cluster", Namespace: "saw-ns"}},
}
currentContextSwitchConfig = clientcmdapi.Config{
AuthInfos: map[string]*clientcmdapi.AuthInfo{
"black-user": {Token: "black-token"}},
Clusters: map[string]*clientcmdapi.Cluster{
"pig-cluster": {Server: "http://pig.org:8080"}},
Contexts: map[string]*clientcmdapi.Context{
"root-context": {AuthInfo: "black-user", Cluster: "pig-cluster", Namespace: "saw-ns"}},
CurrentContext: "root-context",
}
)

func Test_handleQuickSwitch(t *testing.T) {
type args struct {
config *clientcmdapi.Config
name string
}
tests := []struct {
name string
args args
want *clientcmdapi.Config
wantErr bool
}{
// TODO: Add test cases.
{"exist-context", args{&switchConfig, "root-context"}, &currentContextSwitchConfig, false},
{"no-exist-context", args{&switchConfig, "test"}, &currentContextSwitchConfig, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := handleQuickSwitch(tt.args.config, tt.args.name)
if (err != nil) != tt.wantErr {
t.Errorf("handleQuickSwitch() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("handleQuickSwitch() got = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 00572ee

Please sign in to comment.