Skip to content

Commit

Permalink
add check kubeconfig function
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny0826 committed Aug 8, 2019
1 parent 2df5b51 commit a10d551
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 33 deletions.
16 changes: 15 additions & 1 deletion cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"io/ioutil"
"k8s.io/client-go/tools/clientcmd"
"log"
"os"
"strings"
Expand Down Expand Up @@ -81,12 +82,17 @@ kubecm add -f example.yaml -c
`,
Run: func(cmd *cobra.Command, args []string) {
if FileExists(file) {
err := ConfigCheck(file)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
cover, _ = cmd.Flags().GetBool("cover")
oldYaml := Config{}
oldYaml.ReadYaml(cfgFile)
addYaml := Config{}
addYaml.ReadYaml(file)
err := oldYaml.MergeConfig(addYaml)
err = oldYaml.MergeConfig(addYaml)
if err != nil {
fmt.Println(err)
}
Expand Down Expand Up @@ -188,3 +194,11 @@ func (c *Config) Check(name string) error {
}
return nil
}

func ConfigCheck(kubeconfigPath string) error {
_, err := clientcmd.BuildConfigFromFlags("", kubeconfigPath)
if err != nil {
return err
}
return nil
}
11 changes: 8 additions & 3 deletions cmd/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var mergeCmd = &cobra.Command{
Use: "merge",
Short: "Merge the kubeconfig files in the specified directory.",
Long: `Merge the kubeconfig files in the specified directory.`,
Example:`
Example: `
# Merge kubeconfig in the test directory
kubecm merge -f test
Expand All @@ -41,12 +41,17 @@ kubecm merge -f test -c
Run: func(cmd *cobra.Command, args []string) {
cover, _ = cmd.Flags().GetBool("cover")
files := listFile(folder)
fmt.Printf("Loading kubeconfig file:%v \n" , files)
fmt.Printf("Loading kubeconfig file: %v \n", files)
mergeYaml := Config{}
for _, yaml := range files {
err := ConfigCheck(yaml)
if err != nil {
fmt.Printf("Please check kubeconfig file: %v \n%s", yaml, err)
os.Exit(1)
}
tmpYaml := Config{}
tmpYaml.ReadYaml(yaml)
err := mergeYaml.MergeAllConfig(tmpYaml, yaml)
err = mergeYaml.MergeAllConfig(tmpYaml, yaml)
if err != nil {
fmt.Println(err)
}
Expand Down
14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,21 @@ go 1.12

require (
github.com/bndr/gotabulate v1.1.2
github.com/gogo/protobuf v1.2.1 // indirect
github.com/imdario/mergo v0.3.7 // indirect
github.com/json-iterator/go v1.1.7 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/spf13/cobra v0.0.5
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 // indirect
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 // indirect
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.2.2
k8s.io/api v0.0.0-20190313235455-40a48860b5ab
k8s.io/apimachinery v0.0.0-20190313205120-d7deff9243b1
k8s.io/client-go v11.0.0+incompatible
k8s.io/klog v0.3.3 // indirect
k8s.io/utils v0.0.0-20190801114015-581e00157fb1 // indirect
sigs.k8s.io/yaml v1.1.0 // indirect
)
Loading

0 comments on commit a10d551

Please sign in to comment.