Skip to content

Commit

Permalink
feat: allow select context for kubecm add and merge command (#952)
Browse files Browse the repository at this point in the history
* feat: allow select context for kubecm add and merge command
  • Loading branch information
cr7258 authored May 20, 2024
1 parent e33f2ac commit adf5297
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 17 deletions.
26 changes: 21 additions & 5 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"reflect"
"strconv"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -38,6 +39,7 @@ func (ac *AddCommand) Init() {
ac.command.Flags().StringP("file", "f", "", "Path to merge kubeconfig files")
ac.command.Flags().String("context-name", "", "override context name when add kubeconfig context")
ac.command.PersistentFlags().BoolP("cover", "c", false, "Overwrite local kubeconfig files")
ac.command.PersistentFlags().Bool("select-context", false, "select the context to be added")
_ = ac.command.MarkFlagRequired("file")
ac.AddCommands(&DocsCommand{})
}
Expand All @@ -46,6 +48,7 @@ func (ac *AddCommand) runAdd(cmd *cobra.Command, args []string) error {
file, _ := ac.command.Flags().GetString("file")
cover, _ := ac.command.Flags().GetBool("cover")
contextName, _ := ac.command.Flags().GetString("context-name")
selectContext, _ := ac.command.Flags().GetBool("select-context")

var newConfig *clientcmdapi.Config
var err error
Expand All @@ -72,15 +75,15 @@ func (ac *AddCommand) runAdd(cmd *cobra.Command, args []string) error {
}
}

err = AddToLocal(newConfig, file, contextName, cover)
err = AddToLocal(newConfig, file, contextName, cover, selectContext)
if err != nil {
return err
}
return nil
}

// AddToLocal add kubeConfig to local
func AddToLocal(newConfig *clientcmdapi.Config, path, newName string, cover bool) error {
func AddToLocal(newConfig *clientcmdapi.Config, path, newName string, cover bool, selectContext bool) error {
oldConfig, err := clientcmd.LoadFromFile(cfgFile)
if err != nil {
return err
Expand All @@ -90,7 +93,7 @@ func AddToLocal(newConfig *clientcmdapi.Config, path, newName string, cover bool
fileName: getFileName(path),
}
// merge context loop
outConfig, err := kco.handleContexts(oldConfig, newName)
outConfig, err := kco.handleContexts(oldConfig, newName, selectContext)
if err != nil {
return err
}
Expand All @@ -99,6 +102,12 @@ func AddToLocal(newConfig *clientcmdapi.Config, path, newName string, cover bool
outConfig.CurrentContext = k
}
}

if reflect.DeepEqual(oldConfig, outConfig) {
fmt.Println("No context to add.")
return nil
}

if !cover {
cover, err = strconv.ParseBool(BoolUI(fmt.Sprintf("Does it overwrite File 「%s」?", cfgFile)))
if err != nil {
Expand All @@ -112,7 +121,7 @@ func AddToLocal(newConfig *clientcmdapi.Config, path, newName string, cover bool
return nil
}

func (kc *KubeConfigOption) handleContexts(oldConfig *clientcmdapi.Config, contextName string) (*clientcmdapi.Config, error) {
func (kc *KubeConfigOption) handleContexts(oldConfig *clientcmdapi.Config, contextName string, selectContext bool) (*clientcmdapi.Config, error) {
newConfig := clientcmdapi.NewConfig()
var index int
var newName string
Expand All @@ -129,8 +138,15 @@ func (kc *KubeConfigOption) handleContexts(oldConfig *clientcmdapi.Config, conte
newName = contextName
}

if selectContext {
importContext := BoolUI(fmt.Sprintf("Do you want to add context「%s」? (If you select `False`, this context will not be merged)", newName))
if importContext == "False" {
continue
}
}

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))
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", newName)
if newName == kc.fileName {
Expand Down
4 changes: 2 additions & 2 deletions cmd/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func TestKubeConfig_handleContexts(t *testing.T) {
config: tt.fields.config,
fileName: tt.fields.fileName,
}
got, err := kc.handleContexts(tt.args.oldConfig, tt.args.newName)
got, err := kc.handleContexts(tt.args.oldConfig, tt.args.newName, false)
if (err != nil) != tt.wantErr {
t.Errorf("handleContexts() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down Expand Up @@ -265,7 +265,7 @@ func TestAddToLocal(t *testing.T) {
}

// Test AddToLocal function
err = AddToLocal(newConfig, tempFile.Name(), "", true)
err = AddToLocal(newConfig, tempFile.Name(), "", true, false)
if err != nil {
t.Fatalf("Failed to add to local: %v", err)
}
Expand Down
19 changes: 10 additions & 9 deletions cmd/cloud_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (ca *CloudAddCommand) runCloudAdd(cmd *cobra.Command, args []string) error
clusterID, _ := ca.command.Flags().GetString("cluster_id")
regionID, _ := ca.command.Flags().GetString("region_id")
cover, _ := ca.command.Flags().GetBool("cover")
selectContext, _ := ca.command.Flags().GetBool("select-context")
var num int
if provider == "" {
num = selectCloud(Clouds, "Select Cloud")
Expand Down Expand Up @@ -74,7 +75,7 @@ func (ca *CloudAddCommand) runCloudAdd(cmd *cobra.Command, args []string) error
if err != nil {
return err
}
err = AddToLocal(newConfig, clusters[clusterNum].Name, "", cover)
err = AddToLocal(newConfig, clusters[clusterNum].Name, "", cover, selectContext)
if err != nil {
return err
}
Expand All @@ -87,7 +88,7 @@ func (ca *CloudAddCommand) runCloudAdd(cmd *cobra.Command, args []string) error
if err != nil {
return err
}
err = AddToLocal(newConfig, fmt.Sprintf("alicloud-%s", clusterID), "", cover)
err = AddToLocal(newConfig, fmt.Sprintf("alicloud-%s", clusterID), "", cover, selectContext)
if err != nil {
return err
}
Expand Down Expand Up @@ -127,7 +128,7 @@ func (ca *CloudAddCommand) runCloudAdd(cmd *cobra.Command, args []string) error
if err != nil {
return err
}
err = AddToLocal(newConfig, clusters[clusterNum].Name, "", cover)
err = AddToLocal(newConfig, clusters[clusterNum].Name, "", cover, selectContext)
if err != nil {
return err
}
Expand All @@ -140,7 +141,7 @@ func (ca *CloudAddCommand) runCloudAdd(cmd *cobra.Command, args []string) error
if err != nil {
return err
}
err = AddToLocal(newConfig, fmt.Sprintf("tencent-%s", clusterID), "", cover)
err = AddToLocal(newConfig, fmt.Sprintf("tencent-%s", clusterID), "", cover, selectContext)
if err != nil {
return err
}
Expand Down Expand Up @@ -169,7 +170,7 @@ func (ca *CloudAddCommand) runCloudAdd(cmd *cobra.Command, args []string) error
if err != nil {
return err
}
err = AddToLocal(newConfig, clusters[clusterNum].Name, "", cover)
err = AddToLocal(newConfig, clusters[clusterNum].Name, "", cover, selectContext)
if err != nil {
return err
}
Expand All @@ -182,7 +183,7 @@ func (ca *CloudAddCommand) runCloudAdd(cmd *cobra.Command, args []string) error
if err != nil {
return err
}
err = AddToLocal(newConfig, fmt.Sprintf("rancher-%s", clusterID), "", cover)
err = AddToLocal(newConfig, fmt.Sprintf("rancher-%s", clusterID), "", cover, selectContext)
if err != nil {
return err
}
Expand Down Expand Up @@ -219,7 +220,7 @@ func (ca *CloudAddCommand) runCloudAdd(cmd *cobra.Command, args []string) error
if err != nil {
return err
}
err = AddToLocal(newConfig, fmt.Sprintf("aws-%s", clusterID), "", cover)
err = AddToLocal(newConfig, fmt.Sprintf("aws-%s", clusterID), "", cover, selectContext)
if err != nil {
return err
}
Expand Down Expand Up @@ -279,7 +280,7 @@ func (ca *CloudAddCommand) runCloudAdd(cmd *cobra.Command, args []string) error
if err != nil {
return err
}
return AddToLocal(newConfig, fmt.Sprintf("azure-%s", clusterID), "", cover)
return AddToLocal(newConfig, fmt.Sprintf("azure-%s", clusterID), "", cover, selectContext)
}

subscriptionList, err := azure.ListSubscriptions()
Expand Down Expand Up @@ -332,7 +333,7 @@ func (ca *CloudAddCommand) runCloudAdd(cmd *cobra.Command, args []string) error
if err != nil {
return err
}
return AddToLocal(newConfig, fmt.Sprintf("azure-%s", clusterID), "", cover)
return AddToLocal(newConfig, fmt.Sprintf("azure-%s", clusterID), "", cover, selectContext)

}
return nil
Expand Down
11 changes: 10 additions & 1 deletion cmd/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ func (mc *MergeCommand) Init() {
}
mc.command.Flags().StringP("folder", "f", "", "KubeConfig folder")
mc.command.Flags().BoolP("assumeyes", "y", false, "skip interactive file overwrite confirmation")
mc.command.PersistentFlags().Bool("select-context", false, "select the context to be merged")
//_ = mc.command.MarkFlagRequired("folder")
mc.AddCommands(&DocsCommand{})
}

func (mc MergeCommand) runMerge(command *cobra.Command, args []string) error {
files := args
folder, _ := mc.command.Flags().GetString("folder")
selectContext, _ := mc.command.Flags().GetBool("select-context")

if folder != "" {
folder, err := CheckAndTransformFilePath(folder)
if err != nil {
Expand All @@ -60,11 +63,17 @@ func (mc MergeCommand) runMerge(command *cobra.Command, args []string) error {
config: loadConfig,
fileName: getFileName(yaml),
}
outConfigs, err = kco.handleContexts(outConfigs, "")
outConfigs, err = kco.handleContexts(outConfigs, "", selectContext)
if err != nil {
return err
}
}

if len(outConfigs.Contexts) == 0 {
fmt.Println("No context to merge.")
return nil
}

confirm, _ := mc.command.Flags().GetBool("assumeyes")
if !confirm {
cover := BoolUI(fmt.Sprintf("Are you sure you want to overwrite the 「%s」 file?", cfgFile))
Expand Down
1 change: 1 addition & 0 deletions docs/en-us/cli/kubecm_add.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ cat /etc/kubernetes/admin.conf | kubecm add -f -
-c, --cover Overwrite local kubeconfig files
-f, --file string Path to merge kubeconfig files
-h, --help help for add
--select-context select the context to be added
```

### Options inherited from parent commands
Expand Down
1 change: 1 addition & 0 deletions docs/en-us/cli/kubecm_merge.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ kubecm merge -f dir --config kubecm.config
-y, --assumeyes skip interactive file overwrite confirmation
-f, --folder string KubeConfig folder
-h, --help help for merge
--select-context select the context to be merged
```

### Options inherited from parent commands
Expand Down
1 change: 1 addition & 0 deletions docs/zh-cn/cli/kubecm_add.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ cat /etc/kubernetes/admin.conf | kubecm add -f -
-f, --file string Path to merge kubeconfig files
-h, --help help for add
--select-context select the context to be added
```

### 全局选项
Expand Down
1 change: 1 addition & 0 deletions docs/zh-cn/cli/kubecm_merge.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ kubecm merge -f dir --config kubecm.config
-y, --assumeyes skip interactive file overwrite confirmation
-f, --folder string KubeConfig folder
-h, --help help for merge
--select-context select the context to be merged
```

### 全局选项
Expand Down

0 comments on commit adf5297

Please sign in to comment.