diff --git a/README.md b/README.md index 6c626dc..a663a92 100644 --- a/README.md +++ b/README.md @@ -5,54 +5,32 @@ **kubenv** is a tool to ease the management of multiple kubernetes cluster. Features: -- Merge different kubeconfig file in different path into a single big kubeconfig +- Merge different kubeconfig files from different path into a single big kubeconfig - Switch between kubernetes context - Execute a command using either a single or mutliple contexts ## Install -Grap the latest release in github or build by yourself: +Grabe the latest release in github release or build it by yourself: ``` go get https://github.com/mqllr/kubenv ``` -Define your configuration file as [kubenv-example.yaml](https://github.com/MqllR/kubenv/blob/master/example/kubenv_example.yaml) and export the environment variable: +## Usage -``` -export KUBENV_CONFIG=/path/to/my/config.yaml -``` +Get started by picking up your kubeconfig files: -## Configuration - - ### k8sConfigs - -The k8sConfigs section define the sync mode. 2 modes available: `local` for local files and `exec` for command execution. The exec mode will capture the command output. - -```yaml - dev: - sync: - mode: local - path: /tmp/k8senv/dev/config - kind: - sync: - mode: exec - command: - - bash - - -c - - | - kind export -q kubeconfig --kubeconfig /tmp/test && cat /tmp/test +``` +kubenv sync -a=false -m=exec --command="cat /path/to/kubeconfig" +▸ Start the synchronization of kubeconfig file into /home/john/.kube/config ... ``` -## Usage - -Get started by picking up your kubeconfig files: +Which is equivalent to: ``` -kubenv sync -▸ Start the synchronization of kubeconfig file into /home/mql/.kube/config ... -Sync kubeconfig foo ✔ -Sync kubeconfig bar ✔ +kubenv sync -a=false -m=local --path="/path/to/kubeconfig" +▸ Start the synchronization of kubeconfig file into /home/john/.kube/config ... ``` Then you can change your default context by using the `use-context` @@ -67,6 +45,14 @@ or execute a command against a single or multiple contexts: kubenv with-context ``` -The tool uses https://github.com/AlecAivazis/survey to navigate between contexts. If you prefer to use j/k to go down or up, press `esc`: +The CLI uses https://github.com/AlecAivazis/survey to navigate between contexts. If you prefer to use j/k to go down or up, press `esc`: > The user can also press esc to toggle the ability cycle through the options with the j and k keys to do down and up respectively. + +## Bonus + +Want the CLI to be part of kubectl? + +``` +ln -s /home/john/go/bin/kubenv /usr/local/bin/kubectl-env +``` diff --git a/TODO.md b/TODO.md index df3ac4d..422879f 100644 --- a/TODO.md +++ b/TODO.md @@ -3,12 +3,11 @@ - [ ] Backup / restore kubeconfig when running the sync command - [ ] Switch between namespaces - [x] Run command across mutliple clusters -- [ ] Read the kubenv config through http(s) ## Sync - [ ] Get kubeconfig files using http(s) -- [ ] Get kubeconfig with glob +- [ ] Get kubeconfig with glob pattern - [ ] Add arguments to append a kubeconfig file to an existing config ## Prompt diff --git a/cmd/root.go b/cmd/root.go index 512007f..b19fa09 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,19 +3,12 @@ package cmd import ( goflag "flag" "fmt" - "os" "github.com/spf13/cobra" - "github.com/spf13/viper" "k8s.io/klog" - - "github.com/mqllr/kubenv/pkg/config" ) var ( - // Used for flags. - cfgFile string - rootCmd = &cobra.Command{ Use: "kubenv", Short: "A tool to manage multiple Kube cluster", @@ -37,49 +30,20 @@ func Execute() error { } func init() { - cobra.OnInitialize(initConfig) - klog.InitFlags(nil) rootCmd.PersistentFlags().AddGoFlagSet(goflag.CommandLine) rootCmd.Flags().SortFlags = false - rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/kubenv.yaml)") - rootCmd.AddCommand(NewVersionCmd()) + rootCmd.AddCommand(versionCmd()) // root cmd - rootCmd.AddCommand(showCmd) - rootCmd.AddCommand(syncCmd) - rootCmd.AddCommand(useContextCmd) - rootCmd.AddCommand(withContextCmd) + rootCmd.AddCommand(showCmd()) + rootCmd.AddCommand(syncCommand()) + rootCmd.AddCommand(useContextCmd()) + rootCmd.AddCommand(withContextCmd()) // show cmd - showCmd.AddCommand(showClusterCmd) - showCmd.AddCommand(showUserCmd) -} - -func initConfig() { - viper.SetEnvPrefix("kubenv") - err := viper.BindEnv("config") - if err != nil { - klog.Fatalf("Error when binding the config key %s", err) - } - - if cfgFile != "" { - viper.SetConfigFile(cfgFile) - } else if viper.GetString("config") != "" { - viper.SetConfigFile(viper.GetString("config")) - } else { - viper.SetConfigName("kubenv") - viper.SetConfigType("yaml") - viper.AddConfigPath("$HOME/") - viper.AddConfigPath(".") - } - - viper.SetDefault("kubeConfig", os.Getenv("HOME")+"/.kube/config") - - err = config.LoadConfig() - if err != nil { - klog.Fatal(err) - } + showCmd().AddCommand(showClusterCmd()) + showCmd().AddCommand(showUserCmd()) } diff --git a/cmd/show.go b/cmd/show.go index 5dee6f4..2716140 100644 --- a/cmd/show.go +++ b/cmd/show.go @@ -2,8 +2,10 @@ package cmd import "github.com/spf13/cobra" -var showCmd = &cobra.Command{ - Use: "show", - Short: "show different information", - Aliases: []string{"sh"}, +func showCmd() *cobra.Command { + return &cobra.Command{ + Use: "show", + Short: "show different information", + Aliases: []string{"sh"}, + } } diff --git a/cmd/show_cluster.go b/cmd/show_cluster.go index 1dae65a..19ec419 100644 --- a/cmd/show_cluster.go +++ b/cmd/show_cluster.go @@ -10,16 +10,20 @@ import ( "k8s.io/klog" ) -var showClusterCmd = &cobra.Command{ - Use: "cluster", - Short: "Print out the current context's cluster", - Run: func(cmd *cobra.Command, args []string) { - showCluster(args) - }, +func showClusterCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "cluster", + Short: "Print out the current context's cluster", + Run: func(cmd *cobra.Command, args []string) { + showCluster(args) + }, + } + + return cmd } func showCluster(args []string) { - kubeconfig, err := k8s.NewKubeConfigFromFile(config.Conf.KubeConfig) + kubeconfig, err := k8s.NewKubeConfigFromFile(config.GetKubeConfig()) if err != nil { klog.Fatalf("Cannot load the kubeconfig file: %s", err) } diff --git a/cmd/show_user.go b/cmd/show_user.go index eaa3d56..68c04a8 100644 --- a/cmd/show_user.go +++ b/cmd/show_user.go @@ -10,16 +10,19 @@ import ( "k8s.io/klog" ) -var showUserCmd = &cobra.Command{ - Use: "user", - Short: "Print out the current context's user", - Run: func(cmd *cobra.Command, args []string) { - showUser(args) - }, +func showUserCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "user", + Short: "Print out the current context's user", + Run: func(cmd *cobra.Command, args []string) { + showUser(args) + }, + } + return cmd } func showUser(args []string) { - kubeconfig, err := k8s.NewKubeConfigFromFile(config.Conf.KubeConfig) + kubeconfig, err := k8s.NewKubeConfigFromFile(config.GetKubeConfig()) if err != nil { klog.Fatalf("Cannot load the kubeconfig file: %s", err) } diff --git a/cmd/sync.go b/cmd/sync.go index 309093c..b9b647e 100644 --- a/cmd/sync.go +++ b/cmd/sync.go @@ -5,47 +5,77 @@ import ( "github.com/manifoldco/promptui" "github.com/spf13/cobra" - "k8s.io/klog" "github.com/mqllr/kubenv/pkg/config" "github.com/mqllr/kubenv/pkg/k8s" - k8ssync "github.com/mqllr/kubenv/pkg/sync" + "github.com/mqllr/kubenv/pkg/sync" ) -var syncCmd = &cobra.Command{ - Use: "sync", - Short: "Synchronize the kubernetes config files", - Run: func(cmd *cobra.Command, args []string) { - sync(args) - }, -} +func syncCommand() *cobra.Command { + opts := sync.SyncOptions{} -func sync(args []string) { - fmt.Printf("%v Start the synchronization of kubeconfig file into %s ...\n", promptui.IconSelect, config.Conf.KubeConfig) + cmd := &cobra.Command{ + Use: "sync", + Short: "Synchronize the kubernetes config files", + PreRunE: func(cmd *cobra.Command, args []string) error { + return validateFlags(&opts) + }, + RunE: func(cmd *cobra.Command, args []string) error { + return runSync(&opts) + }, + } - fullConfig := k8s.NewKubeConfig() + f := cmd.Flags() + f.BoolVarP(&opts.AppendTo, "append", "a", true, "Append the new kubeconfig files to ~/.kube/config") + f.StringVarP(&opts.Mode, "mode", "m", "local", "Mode to read a kubeconfig file. Either local, exec or glob") + f.StringVar(&opts.Path, "path", "", "A path to the local kubeconfig file") + f.StringVar(&opts.Command, "command", "", "A command to execute to retrieve the kubeconfig file") - for name, conf := range config.Conf.K8SConfigs { - fmt.Printf("Sync kubeconfig %s", name) + return cmd +} - s, err := k8ssync.NewService(*conf.Sync) - if err != nil { - fmt.Printf(" %v\n", promptui.IconBad) - klog.V(2).Infof("Cannot sync: %s", err) - continue +func validateFlags(opts *sync.SyncOptions) error { + existInSyncMode := func(mode string) bool { + for _, m := range config.SyncMode { + if mode == m { + return true + } } + return false + } + + if !existInSyncMode(opts.Mode) { + return fmt.Errorf("Mode %s not supported", opts.Mode) + } + + return nil +} + +func runSync(opts *sync.SyncOptions) error { + fmt.Printf("%v Start to synchronize the kubeconfig file into %s ...\n", promptui.IconSelect, config.GetKubeConfig()) - err = s.AppendKubeConfig(fullConfig) + baseKubeConfig := k8s.NewKubeConfig() + var err error + + if opts.AppendTo { + baseKubeConfig, err = k8s.NewKubeConfigFromFile(config.GetKubeConfig()) if err != nil { - fmt.Printf(" %v\n", promptui.IconBad) - klog.V(2).Infof("Error when getting the config back: %s", err) - } else { - fmt.Printf(" %v\n", promptui.IconGood) + return fmt.Errorf("Something went wrong: %s", err) } } - err := fullConfig.WriteFile(config.Conf.KubeConfig) + svc := sync.NewService(opts) + kubeconfig, err := svc.GetKubeConfig() + if err != nil { + return fmt.Errorf("Cannot retrieve the kubeconfig: %s", err) + } + + baseKubeConfig.Append(kubeconfig) + + err = baseKubeConfig.WriteFile(config.GetKubeConfig()) if err != nil { fmt.Printf("%v Failed to write the kubeconfig file: %s", promptui.IconBad, err) } + + return nil } diff --git a/cmd/use_context.go b/cmd/use_context.go index d80ee47..b951243 100644 --- a/cmd/use_context.go +++ b/cmd/use_context.go @@ -11,24 +11,31 @@ import ( "github.com/mqllr/kubenv/pkg/prompt" ) -var context string - -var useContextCmd = &cobra.Command{ - Aliases: []string{"uc"}, - Use: "use-context", - Short: "Switch to k8s context", - Run: func(cmd *cobra.Command, args []string) { - useContext(args) - }, +type useContextOptions struct { + context string } -func init() { - useContextCmd.Flags().StringVarP(&context, "context", "c", "", "Kubernetes context to switch") +func useContextCmd() *cobra.Command { + opts := useContextOptions{} + + cmd := &cobra.Command{ + Use: "use-context", + Short: "Switch to k8s context", + Aliases: []string{"uc"}, + Run: func(cmd *cobra.Command, args []string) { + useContext(&opts) + }, + } + + f := cmd.Flags() + f.StringVarP(&opts.context, "context", "c", "", "Kubernetes context to switch") + + return cmd } // use-context command -func useContext(args []string) { - kubeconfig, err := k8s.NewKubeConfigFromFile(config.Conf.KubeConfig) +func useContext(opts *useContextOptions) { + kubeconfig, err := k8s.NewKubeConfigFromFile(config.GetKubeConfig()) if err != nil { klog.Fatalf("Cannot load kubeconfig file: %s", err) } @@ -37,11 +44,11 @@ func useContext(args []string) { var selectedContext string - if context != "" { - if !kubeconfig.IsContextExist(context) { - klog.Fatalf("Context %s doesn't exist", context) + if opts.context != "" { + if !kubeconfig.IsContextExist(opts.context) { + klog.Fatalf("Context %s doesn't exist", opts.context) } - selectedContext = context + selectedContext = opts.context } else { sort.Strings(contexts) @@ -57,7 +64,7 @@ func useContext(args []string) { klog.Fatalf("Cannot set the current context %s: %s", selectedContext, err) } - err = kubeconfig.WriteFile(config.Conf.KubeConfig) + err = kubeconfig.WriteFile(config.GetKubeConfig()) if err != nil { klog.Fatalf("Cannot write the kubeconfig file: %s", err) } diff --git a/cmd/version.go b/cmd/version.go index 0d673e7..dd04207 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -9,18 +9,22 @@ import ( ) const ( - version = "0.3.2" + version = "1.0.0" ) -var output string +type versionOptions struct { + output string +} + +// versionCmd cobra command for version +func versionCmd() *cobra.Command { + opts := versionOptions{} -// NewVersionCmd cobra command for version -func NewVersionCmd() *cobra.Command { cmd := &cobra.Command{ Use: "version", Short: "Print the CLI version", RunE: func(cmd *cobra.Command, args []string) error { - switch output { + switch opts.output { case "json": v, _ := json.Marshal(map[string]string{"version": version}) fmt.Fprintf(cmd.OutOrStdout(), "%s", string(v)) @@ -34,7 +38,8 @@ func NewVersionCmd() *cobra.Command { Aliases: []string{"v"}, } - cmd.Flags().StringVarP(&output, "output", "o", "", "Output: json") + f := cmd.Flags() + f.StringVarP(&opts.output, "output", "o", "", "Output: json") return cmd } diff --git a/cmd/version_test.go b/cmd/version_test.go index 31cf63a..31a3c52 100644 --- a/cmd/version_test.go +++ b/cmd/version_test.go @@ -29,7 +29,7 @@ func TestVersionOutput(t *testing.T) { b := bytes.NewBufferString("") - cmd := NewVersionCmd() + cmd := versionCmd() cmd.SetOut(b) cmd.SetArgs([]string{"-o", option.arg}) diff --git a/cmd/with_context.go b/cmd/with_context.go index 2f13b1b..160b049 100644 --- a/cmd/with_context.go +++ b/cmd/with_context.go @@ -12,19 +12,22 @@ import ( "github.com/mqllr/kubenv/pkg/prompt" ) -var withContextCmd = &cobra.Command{ - Aliases: []string{"wc"}, - Use: "with-context command ...", - Short: "Execute a command with one or multiple k8s context", - Args: cobra.MinimumNArgs(1), - Run: func(cmd *cobra.Command, args []string) { - withContext(args) - }, +func withContextCmd() *cobra.Command { + cmd := &cobra.Command{ + Aliases: []string{"wc"}, + Use: "with-context command ...", + Short: "Execute a command with one or multiple k8s context", + Args: cobra.MinimumNArgs(1), + Run: func(cmd *cobra.Command, args []string) { + withContext(args) + }, + } + return cmd } // with-context command func withContext(args []string) { - c, err := k8s.NewKubeConfigFromFile(config.Conf.KubeConfig) + c, err := k8s.NewKubeConfigFromFile(config.GetKubeConfig()) if err != nil { klog.Fatalf("Error when loading kubeconfig file: %s", err) } diff --git a/example/kubenv_example.yaml b/example/kubenv_example.yaml deleted file mode 100644 index 80cc230..0000000 --- a/example/kubenv_example.yaml +++ /dev/null @@ -1,20 +0,0 @@ ---- -kubeConfig: "/home/test/.kube/config" # Optional, default to $HOME/.kube/config - -k8sConfigs: - dev: - sync: - mode: local - path: /tmp/k8senv/dev/config - test: - sync: - mode: local - path: /tmp/k8senv/test/config - kind: - sync: - mode: exec - command: - - bash - - -c - - | - kind export -q kubeconfig --kubeconfig /tmp/test && cat /tmp/test diff --git a/go.mod b/go.mod index 51e7c4d..85b2f2d 100644 --- a/go.mod +++ b/go.mod @@ -7,33 +7,22 @@ require ( github.com/fatih/color v1.7.0 github.com/manifoldco/promptui v0.8.0 github.com/spf13/cobra v1.1.3 - github.com/spf13/viper v1.7.1 gopkg.in/yaml.v2 v2.4.0 k8s.io/klog v1.0.0 ) require ( github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect - github.com/fsnotify/fsnotify v1.4.9 // indirect - github.com/hashicorp/hcl v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a // indirect github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect github.com/lunixbochs/vtclean v1.0.0 // indirect - github.com/magiconair/properties v1.8.5 // indirect github.com/mattn/go-colorable v0.1.8 // indirect github.com/mattn/go-isatty v0.0.13 // indirect github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect - github.com/mitchellh/mapstructure v1.4.1 // indirect - github.com/pelletier/go-toml v1.9.1 // indirect - github.com/spf13/afero v1.6.0 // indirect - github.com/spf13/cast v1.3.1 // indirect - github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/stretchr/testify v1.5.1 // indirect - github.com/subosito/gotenv v1.2.0 // indirect golang.org/x/sys v0.0.0-20210603125802-9665404d3644 // indirect golang.org/x/term v0.0.0-20210503060354-a79de5458b56 // indirect golang.org/x/text v0.3.6 // indirect - gopkg.in/ini.v1 v1.62.0 // indirect ) diff --git a/go.sum b/go.sum index f7335f9..40ccadf 100644 --- a/go.sum +++ b/go.sum @@ -49,8 +49,6 @@ github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8 github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= -github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= -github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= @@ -78,7 +76,6 @@ github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OI github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= -github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= @@ -99,7 +96,6 @@ github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/b github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= -github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ= @@ -112,7 +108,6 @@ github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANyt github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= -github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a h1:FaWFmfWdAUKbSCtOU2QjDaorUexogfaMgbipgYATUMU= github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a/go.mod h1:UJSiEoRfvx3hP73CvoARgeLjaIOjybY9vj8PUPPFGeU= @@ -122,7 +117,6 @@ github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:C github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= @@ -135,8 +129,6 @@ github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a/go.mod h1:pHhQN github.com/lunixbochs/vtclean v1.0.0 h1:xu2sLAri4lGiovBDQKxl5mrXyESr3gUr5m5SM5+LVb8= github.com/lunixbochs/vtclean v1.0.0/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI= github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= -github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls= -github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= github.com/manifoldco/promptui v0.8.0 h1:R95mMF+McvXZQ7j1g8ucVZE1gLP3Sv6j9vlF9kyRqQo= github.com/manifoldco/promptui v0.8.0/go.mod h1:n4zTdgP0vr0S3w7/O/g98U+e0gwLScEXGwov2nIKuGQ= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= @@ -161,19 +153,14 @@ github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS4 github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY= github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= -github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag= -github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= -github.com/pelletier/go-toml v1.9.1 h1:a6qW1EVNZWH9WGI6CsYdD8WAylkoXBS5yv0XHlh17Tc= -github.com/pelletier/go-toml v1.9.1/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= @@ -193,38 +180,26 @@ github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= -github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= -github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= -github.com/spf13/afero v1.6.0 h1:xoax2sJ2DT8S8xA2paPFjDCScCNeWsg75VG0DLRreiY= -github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= -github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng= -github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= github.com/spf13/cobra v1.1.3 h1:xghbfqPkxzxP3C/f3n5DdpAbdKLj4ZE4BWQI362l53M= github.com/spf13/cobra v1.1.3/go.mod h1:pGADOWyqRD/YMrPZigI/zbliZ2wVD/23d+is3pSWzOo= github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= -github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk= -github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg= -github.com/spf13/viper v1.7.1 h1:pM5oEahlgWv/WnHXpgbKz7iLIxRf65tye2Ci+XFK5sk= -github.com/spf13/viper v1.7.1/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= -github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= @@ -240,7 +215,6 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= @@ -296,7 +270,6 @@ golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -357,8 +330,6 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33 gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= -gopkg.in/ini.v1 v1.62.0 h1:duBzk771uxoUuOlyRLkHsygud9+5lrlGjdFBb4mSKDU= -gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/pkg/config/config.go b/pkg/config/config.go index 8b6e509..4511893 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -1,45 +1,21 @@ package config import ( - "fmt" - "sync" - - "github.com/spf13/viper" -) - -var ( - // Conf store the full config. It can be used to access - // to any configuration information after calling LoadConfig() - Conf *Config - confMu = &sync.Mutex{} + "os" + "path" ) -// Config global config description -type Config struct { - KubeConfig string `yaml:"kubeConfig"` - K8SConfigs map[string]*K8SConfig `mapstructure:"k8sConfigs"` -} - -// LoadConfig should be call first to load the configuration. It stores -// the configuration in Conf. -func LoadConfig() error { - confMu.Lock() - defer confMu.Unlock() - - err := viper.ReadInConfig() - if err != nil { - return fmt.Errorf("Error Using config file %s: %s", viper.ConfigFileUsed(), err) - } +func GetKubeConfig() string { + kubeconfig := os.Getenv("KUBECONFIG") - Conf = &Config{} - err = viper.Unmarshal(Conf) - if err != nil { - return fmt.Errorf("Error when unmarshaling the config file %s: %s", viper.ConfigFileUsed(), err) - } + if kubeconfig == "" { + home, err := os.UserHomeDir() + if err != nil { + return "" + } - if err = Conf.Validate(); err != nil { - return fmt.Errorf("Bad config syntax: %s", err) + return path.Join(home, ".kube/config") } - return nil + return kubeconfig } diff --git a/pkg/config/constants.go b/pkg/config/constants.go index d8a8633..9712412 100644 --- a/pkg/config/constants.go +++ b/pkg/config/constants.go @@ -1,7 +1,7 @@ package config var ( - availableK8SSyncMode = []string{ + SyncMode = []string{ "local", "exec", } diff --git a/pkg/config/k8sconfigs.go b/pkg/config/k8sconfigs.go deleted file mode 100644 index d155e92..0000000 --- a/pkg/config/k8sconfigs.go +++ /dev/null @@ -1,48 +0,0 @@ -package config - -import "fmt" - -// K8SConfig represents a K8SConfig, with only a sync description -type K8SConfig struct { - Sync *K8SSync `yaml:"sync"` -} - -// K8SSync represents a sync configuration -type K8SSync struct { - Mode string `yaml:"mode"` - Path string `yaml:"path,omitempty"` - Command []string `yaml:"command,omitempty"` -} - -// FindK8SConfig get a configuration for a single K8SConfig -func (c *Config) FindK8SConfig(config string) *K8SConfig { - if conf, ok := c.K8SConfigs[config]; ok { - return conf - } - - return &K8SConfig{} -} - -// ListK8SConfigsNames just list the names of all the K8SConfigs -func (c *Config) ListK8SConfigsNames() []string { - var configs []string - for config := range c.K8SConfigs { - configs = append(configs, config) - } - - return configs -} - -func (c *Config) Validate() error { - for _, conf := range c.K8SConfigs { - for _, syncMode := range availableK8SSyncMode { - if conf.Sync.Mode == syncMode { - return nil - } - } - - return fmt.Errorf("K8S Sync mode %s not implemented", conf.Sync.Mode) - } - - return nil -} diff --git a/pkg/sync/sync.go b/pkg/sync/sync.go index c721bb2..f39cd01 100644 --- a/pkg/sync/sync.go +++ b/pkg/sync/sync.go @@ -1,12 +1,18 @@ package sync import ( - "fmt" + "strings" - "github.com/mqllr/kubenv/pkg/config" "github.com/mqllr/kubenv/pkg/k8s" ) +type SyncOptions struct { + AppendTo bool + Mode string + Path string + Command string +} + // Sync implements a way to pick up a kubeconfig type Sync interface { GetKubeConfig() (*k8s.KubeConfig, error) @@ -15,38 +21,26 @@ type Sync interface { // Service represents the required information to // pick a kubeconfig according to the config type Service struct { - s Sync - config config.K8SSync + s Sync } // NewService creates a SyncService according to the // sync type -func NewService(conf config.K8SSync) (*Service, error) { - sync := &Service{ - config: conf, - } +func NewService(opts *SyncOptions) *Service { + sync := &Service{} - switch conf.Mode { + switch opts.Mode { case "local": - sync.s = NewLocalFile(conf.Path) + sync.s = NewLocalFile(opts.Path) case "exec": - sync.s = NewCommandExec(conf.Command) - default: - return nil, fmt.Errorf("Sync mode not implemented") + cmd := strings.Split(opts.Command, " ") + sync.s = NewCommandExec(cmd) } - return sync, nil + return sync } -// AppendKubeConfig merges the kubeconfig synchronised into the -// kubeConfig in argument -func (s *Service) AppendKubeConfig(kubeConfig *k8s.KubeConfig) error { - k, err := s.s.GetKubeConfig() - if err != nil { - return fmt.Errorf("Cannot get the kubeconfig: %s", err) - } - - kubeConfig.Append(k) - - return nil +// GetKubeConfig retrieve a kubeconfig using a sync mode +func (s *Service) GetKubeConfig() (*k8s.KubeConfig, error) { + return s.s.GetKubeConfig() }