Skip to content

Commit

Permalink
Merge pull request #14 from sunny0826/add
Browse files Browse the repository at this point in the history
update add&merge cmd
  • Loading branch information
sunny0826 committed Dec 8, 2020
2 parents a6920eb + ad2054a commit c42bd2d
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 275 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ name: Go
on:
push:
branches:
- "*"
- "master"
pull_request:
branches:
- master

jobs:
test:
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ dist/
kubecm

target
tmp
tmp
mergeDir
config.yaml
68 changes: 29 additions & 39 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"errors"
"fmt"
"os"
"strconv"
"strings"

"github.com/spf13/cobra"
Expand All @@ -20,26 +20,24 @@ type AddCommand struct {
func (ac *AddCommand) Init() {
ac.command = &cobra.Command{
Use: "add",
Short: "Merge configuration file with $HOME/.kube/config",
Long: "Merge configuration file with $HOME/.kube/config",
Short: "Add kubeconfig to $HOME/.kube/config",
Long: "Add kubeconfig to $HOME/.kube/config",
RunE: func(cmd *cobra.Command, args []string) error {
return ac.runAdd(cmd, args)
},
Example: addExample(),
}
ac.command.Flags().StringP("file", "f", "", "Path to merge kubeconfig files")
ac.command.Flags().StringP("name", "n", "", "The name of contexts. if this field is null,it will be named with file name.")
ac.command.Flags().BoolP("cover", "c", false, "Overwrite the original kubeconfig file")
_ = ac.command.MarkFlagRequired("file")
}

func (ac *AddCommand) runAdd(cmd *cobra.Command, args []string) error {
file, _ := ac.command.Flags().GetString("file")
if fileNotExists(file) {
return errors.New(file + " file does not exist")
file, err := CheckAndTransformFilePath(file)
if err != nil {
return err
}
name := ac.command.Flag("name").Value.String()
newConfig, err := formatNewConfig(file, name)
newConfig, newName, err := formatNewConfig(file)
if err != nil {
return err
}
Expand All @@ -48,25 +46,29 @@ func (ac *AddCommand) runAdd(cmd *cobra.Command, args []string) error {
return err
}
outConfig := appendConfig(oldConfig, newConfig)
cover, _ := ac.command.Flags().GetBool("cover")
err = WriteConfig(cover, file, outConfig)
cover := BoolUI(fmt.Sprintf("Are you sure you want to add 「%s」 to the 「%s」context?", newName, cfgFile))
confirm, err := strconv.ParseBool(cover)
if err != nil {
return err
}
err = WriteConfig(confirm, file, outConfig)
if err != nil {
return err
}
return nil
}

func formatNewConfig(file, nameFlag string) (*clientcmdapi.Config, error) {
func formatNewConfig(file string) (*clientcmdapi.Config, string, error) {
config, err := clientcmd.LoadFromFile(file)
if err != nil {
return nil, err
return nil, "", err
}
if len(config.AuthInfos) != 1 {
return nil, errors.New("Only support add 1 context. You can use `merge` cmd")
return nil, "", errors.New("Only support add 1 context. You can use `merge` cmd")
}
name, err := formatAndCheckName(file, nameFlag)
name, err := formatAndCheckName(file)
if err != nil {
return nil, err
return nil, "", err
}
suffix := HashSuf(config)
userName := fmt.Sprintf("user-%v", suffix)
Expand All @@ -89,44 +91,32 @@ func formatNewConfig(file, nameFlag string) (*clientcmdapi.Config, error) {
break
}
fmt.Printf("Context Add: %s \n", name)
return config, nil
return config, name, nil
}

func formatAndCheckName(file, name string) (string, error) {
if name == "" {
n := strings.Split(file, "/")
result := strings.Split(n[len(n)-1], ".")
name = result[0]
func formatAndCheckName(file string) (string, error) {
n := strings.Split(file, "/")
result := strings.Split(n[len(n)-1], ".")
name := result[0]
nameConfirm := BoolUI(fmt.Sprintf("Need to rename 「%s」 context?", name))
if nameConfirm == "True" {
name = PromptUI("Rename", name)
}
config, err := clientcmd.LoadFromFile(cfgFile)
if err != nil {
return "", err
}
for key := range config.Contexts {
if key == name {
return key, errors.New("The name: " + name + " already exists, please replace it")
return key, errors.New("The name: " + name + " already exists, please select another one.")
}
}
return name, nil
}

func fileNotExists(path string) bool {
_, err := os.Stat(path) //os.Stat获取文件信息
if err != nil {
return !os.IsExist(err)
}
return false
}

func addExample() string {
return `
# Merge 1.yaml with $HOME/.kube/config
kubecm add -f 1.yaml
# Merge 1.yaml and name contexts test with $HOME/.kube/config
kubecm add -f 1.yaml -n test
# Overwrite the original kubeconfig file
kubecm add -f 1.yaml -c
# Merge test.yaml with $HOME/.kube/config
kubecm add -f test.yaml
`
}
191 changes: 0 additions & 191 deletions cmd/add_test.go

This file was deleted.

17 changes: 10 additions & 7 deletions cmd/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package cmd
import (
"fmt"
"io/ioutil"
"strconv"

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

"github.com/spf13/cobra"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
Expand All @@ -26,7 +29,6 @@ func (mc *MergeCommand) Init() {
Example: mergeExample(),
}
mc.command.Flags().StringP("folder", "f", "", "Kubeconfig folder")
mc.command.Flags().BoolP("cover", "c", false, "Overwrite the original kubeconfig file")
_ = mc.command.MarkFlagRequired("folder")
}

Expand All @@ -36,14 +38,18 @@ func (mc MergeCommand) runMerge(command *cobra.Command, args []string) error {
mc.command.Printf("Loading kubeconfig file: %v \n", files)
configs := clientcmdapi.NewConfig()
for _, yaml := range files {
config, err := formatNewConfig(yaml, "")
config, err := clientcmd.LoadFromFile(yaml)
if err != nil {
return err
}
configs = appendConfig(configs, config)
}
cover, _ := mc.command.Flags().GetBool("cover")
err := WriteConfig(cover, folder, configs)
cover := BoolUI(fmt.Sprintf("Are you sure you want to overwrite the「%s」file?", cfgFile))
confirm, err := strconv.ParseBool(cover)
if err != nil {
return err
}
err = WriteConfig(confirm, folder, configs)
if err != nil {
return err
}
Expand All @@ -67,8 +73,5 @@ func mergeExample() string {
return `
# Merge kubeconfig in the dir directory
kubecm merge -f dir
# Merge kubeconfig in the directory and overwrite the original kubeconfig file
kubecm merge -f dir -c
`
}
Loading

0 comments on commit c42bd2d

Please sign in to comment.