From c29ca245c1955f2ffbb86da6a41b2fe189569f89 Mon Sep 17 00:00:00 2001 From: yunlzheng Date: Fri, 13 Dec 2019 18:40:51 +0800 Subject: [PATCH] refactor ktctl cmd --- cmd/ktctl/main.go | 177 +-------------------------------- pkg/kt/command/global_flags.go | 159 +++++++++++++++++++++++++++++ pkg/kt/command/options.go | 60 +++++++++++ 3 files changed, 224 insertions(+), 172 deletions(-) create mode 100644 pkg/kt/command/global_flags.go create mode 100644 pkg/kt/command/options.go diff --git a/cmd/ktctl/main.go b/cmd/ktctl/main.go index d583ea5..0c8073a 100644 --- a/cmd/ktctl/main.go +++ b/cmd/ktctl/main.go @@ -1,12 +1,9 @@ package main import ( - "fmt" "os" - "path/filepath" - "github.com/alibaba/kt-connect/pkg/kt/action" - "github.com/alibaba/kt-connect/pkg/kt/util" + "github.com/alibaba/kt-connect/pkg/kt/command" "github.com/rs/zerolog" "github.com/rs/zerolog/log" "github.com/urfave/cli" @@ -14,185 +11,21 @@ import ( _ "k8s.io/client-go/plugin/pkg/client/auth/oidc" ) -var ( - // global params - kubeconfig string - namespace string - debug bool - image string - labels string - - // connect - disableDNS bool - sshPort int - socke5Proxy int - cidr string - method string - - // exchange - expose string - - // context - pidFile string - userHome string -) - func init() { zerolog.SetGlobalLevel(zerolog.InfoLevel) log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) } func main() { - userHome := util.HomeDir() - appHome := fmt.Sprintf("%s/.ktctl", userHome) - util.CreateDirIfNotExist(appHome) - pidFile = fmt.Sprintf("%s/pid", appHome) + options := command.NewDaemonOptions() app := cli.NewApp() app.Name = "KT Connect" app.Usage = "" app.Version = "0.0.8" - app.Authors = []cli.Author{ - cli.Author{ - Name: "rdc incubator", - }, - } - - app.Flags = []cli.Flag{ - cli.StringFlag{ - Name: "namespace,n", - Value: "default", - Destination: &namespace, - }, - cli.StringFlag{ - Name: "kubeconfig,c", - Value: filepath.Join(userHome, ".kube", "config"), - Destination: &kubeconfig, - }, - cli.StringFlag{ - Name: "image,i", - Usage: "Custom proxy image", - Value: "registry.cn-hangzhou.aliyuncs.com/rdc-incubator/kt-connect-shadow:stable", - Destination: &image, - }, - cli.BoolFlag{ - Name: "debug,d", - Usage: "debug mode", - Destination: &debug, - }, - cli.StringFlag{ - Name: "label,l", - Usage: "Extra labels on proxy pod e.g. 'label1=val1,label2=val2'", - Destination: &labels, - }, - } - - app.Commands = []cli.Command{ - { - Name: "connect", - Usage: "connection to kubernetes cluster", - Flags: []cli.Flag{ - cli.StringFlag{ - Name: "method", - Value: "vpn", - Usage: "Connect method 'vpn' or 'socks5'", - Destination: &method, - }, - cli.IntFlag{ - Name: "proxy", - Value: 2223, - Usage: "when should method socks5, you can choice which port to proxy, default 2223", - Destination: &socke5Proxy, - }, - cli.IntFlag{ - Name: "port", - Value: 2222, - Usage: "Local SSH Proxy port", - Destination: &sshPort, - }, - cli.BoolFlag{ - Name: "disableDNS", - Usage: "Disable Cluster DNS", - Destination: &disableDNS, - }, - cli.StringFlag{ - Name: "cidr", - Usage: "Custom CIDR eq '172.2.0.0/16'", - Destination: &cidr, - }, - }, - Action: func(c *cli.Context) error { - action := action.Action{ - Kubeconfig: kubeconfig, - Namespace: namespace, - Debug: debug, - Image: image, - PidFile: pidFile, - UserHome: userHome, - Labels: labels, - } - if debug { - zerolog.SetGlobalLevel(zerolog.DebugLevel) - } - action.Connect(sshPort, method, socke5Proxy, disableDNS, cidr) - return nil - }, - }, - { - Name: "exchange", - Usage: "exchange kubernetes deployment to local", - Flags: []cli.Flag{ - cli.StringFlag{ - Name: "expose", - Usage: "expose port", - Destination: &expose, - }, - }, - Action: func(c *cli.Context) error { - action := action.Action{ - Kubeconfig: kubeconfig, - Namespace: namespace, - Debug: debug, - Image: image, - PidFile: pidFile, - UserHome: userHome, - Labels: labels, - } - if debug { - zerolog.SetGlobalLevel(zerolog.DebugLevel) - } - action.Exchange(c.Args().First(), expose, userHome, pidFile) - return nil - }, - }, - { - Name: "mesh", - Usage: "mesh kubernetes deployment to local", - Flags: []cli.Flag{ - cli.StringFlag{ - Name: "expose", - Usage: "expose port", - Destination: &expose, - }, - }, - Action: func(c *cli.Context) error { - action := action.Action{ - Kubeconfig: kubeconfig, - Namespace: namespace, - Debug: debug, - Image: image, - PidFile: pidFile, - UserHome: userHome, - Labels: labels, - } - if debug { - zerolog.SetGlobalLevel(zerolog.DebugLevel) - } - action.Mesh(c.Args().First(), expose, userHome, pidFile) - return nil - }, - }, - } + app.Authors = command.NewCliAuthor() + app.Flags = command.AppFlags(options) + app.Commands = command.NewCommands(options) err := app.Run(os.Args) if err != nil { diff --git a/pkg/kt/command/global_flags.go b/pkg/kt/command/global_flags.go new file mode 100644 index 0000000..aaab3a5 --- /dev/null +++ b/pkg/kt/command/global_flags.go @@ -0,0 +1,159 @@ +package command + +import ( + "path/filepath" + "github.com/urfave/cli" + "github.com/alibaba/kt-connect/pkg/kt/action" + "github.com/rs/zerolog" +) + +// NewCliAuthor return cli author +func NewCliAuthor() []cli.Author{ + return []cli.Author{ + cli.Author{ + Name: "rdc incubator", + }, + } +} + +// NewCommands return new Connect Command +func NewCommands(options *DaemonOptions) []cli.Command{ + return []cli.Command{ + { + Name: "connect", + Usage: "connection to kubernetes cluster", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "method", + Value: "vpn", + Usage: "Connect method 'vpn' or 'socks5'", + Destination: &options.ConnectOptions.Method, + }, + cli.IntFlag{ + Name: "proxy", + Value: 2223, + Usage: "when should method socks5, you can choice which port to proxy, default 2223", + Destination: &options.ConnectOptions.Socke5Proxy, + }, + cli.IntFlag{ + Name: "port", + Value: 2222, + Usage: "Local SSH Proxy port", + Destination: &options.ConnectOptions.SSHPort, + }, + cli.BoolFlag{ + Name: "disableDNS", + Usage: "Disable Cluster DNS", + Destination: &options.ConnectOptions.DisableDNS, + }, + cli.StringFlag{ + Name: "cidr", + Usage: "Custom CIDR eq '172.2.0.0/16'", + Destination: &options.ConnectOptions.CIDR, + }, + }, + Action: func(c *cli.Context) error { + action := action.Action{ + Kubeconfig: options.Kubeconfig, + Namespace: options.Namespace, + Debug: options.Debug, + Image: options.Image, + PidFile: options.RuntimeOptions.PidFile, + UserHome: options.RuntimeOptions.UserHome, + Labels: options.Labels, + } + if options.Debug { + zerolog.SetGlobalLevel(zerolog.DebugLevel) + } + action.Connect(options.ConnectOptions.SSHPort, options.ConnectOptions.Method, options.ConnectOptions.Socke5Proxy, options.ConnectOptions.DisableDNS, options.ConnectOptions.CIDR) + return nil + }, + }, + { + Name: "exchange", + Usage: "exchange kubernetes deployment to local", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "expose", + Usage: "expose port", + Destination: &options.ExchangeOptions.Expose, + }, + }, + Action: func(c *cli.Context) error { + action := action.Action{ + Kubeconfig: options.Kubeconfig, + Namespace: options.Namespace, + Debug: options.Debug, + Image: options.Image, + Labels: options.Labels, + PidFile: options.RuntimeOptions.PidFile, + UserHome: options.RuntimeOptions.UserHome, + } + if options.Debug { + zerolog.SetGlobalLevel(zerolog.DebugLevel) + } + action.Exchange(c.Args().First(), options.ExchangeOptions.Expose, options.RuntimeOptions.UserHome, options.RuntimeOptions.PidFile) + return nil + }, + }, + { + Name: "mesh", + Usage: "mesh kubernetes deployment to local", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "expose", + Usage: "expose port", + Destination: &options.MeshOptions.Expose, + }, + }, + Action: func(c *cli.Context) error { + action := action.Action{ + Kubeconfig: options.Kubeconfig, + Namespace: options.Namespace, + Debug: options.Debug, + Image: options.Image, + Labels: options.Labels, + PidFile: options.RuntimeOptions.PidFile, + UserHome: options.RuntimeOptions.UserHome, + } + if options.Debug { + zerolog.SetGlobalLevel(zerolog.DebugLevel) + } + action.Mesh(c.Args().First(), options.MeshOptions.Expose, options.RuntimeOptions.UserHome, options.RuntimeOptions.PidFile) + return nil + }, + }, + } +} + +// AppFlags return app flags +func AppFlags(options *DaemonOptions) []cli.Flag { + return []cli.Flag{ + cli.StringFlag{ + Name: "namespace,n", + Value: "default", + Destination: &options.Namespace, + }, + cli.StringFlag{ + Name: "kubeconfig,c", + Value: filepath.Join(options.RuntimeOptions.UserHome, ".kube", "config"), + Destination: &options.Kubeconfig, + }, + cli.StringFlag{ + Name: "image,i", + Usage: "Custom proxy image", + Value: "registry.cn-hangzhou.aliyuncs.com/rdc-incubator/kt-connect-shadow:stable", + Destination: &options.Image, + }, + cli.BoolFlag{ + Name: "debug,d", + Usage: "debug mode", + Destination: &options.Debug, + }, + cli.StringFlag{ + Name: "label,l", + Usage: "Extra labels on proxy pod e.g. 'label1=val1,label2=val2'", + Destination: &options.Labels, + }, + } +} \ No newline at end of file diff --git a/pkg/kt/command/options.go b/pkg/kt/command/options.go new file mode 100644 index 0000000..810a30f --- /dev/null +++ b/pkg/kt/command/options.go @@ -0,0 +1,60 @@ +package command + +import ( + "fmt" + + "github.com/alibaba/kt-connect/pkg/kt/util" +) + +type connectOptions struct { + DisableDNS bool + SSHPort int + Socke5Proxy int + CIDR string + Method string +} + +type exchangeOptions struct { + Expose string +} + +type meshOptions struct { + Expose string +} + +type runtimeOptions struct { + PidFile string + UserHome string + AppHome string +} + +// DaemonOptions cli options +type DaemonOptions struct { + Kubeconfig string + Namespace string + Debug bool + Image string + Labels string + RuntimeOptions *runtimeOptions + ConnectOptions *connectOptions + ExchangeOptions *exchangeOptions + MeshOptions *meshOptions +} + +// NewDaemonOptions return new cli default options +func NewDaemonOptions() *DaemonOptions { + userHome := util.HomeDir() + appHome := fmt.Sprintf("%s/.ktctl", userHome) + util.CreateDirIfNotExist(appHome) + pidFile := fmt.Sprintf("%s/pid", appHome) + return &DaemonOptions{ + RuntimeOptions: &runtimeOptions{ + UserHome: userHome, + AppHome: appHome, + PidFile: pidFile, + }, + ConnectOptions: &connectOptions{}, + ExchangeOptions: &exchangeOptions{}, + MeshOptions: &meshOptions{}, + } +}