refactor ktctl cmd

This commit is contained in:
yunlzheng
2019-12-13 18:40:51 +08:00
parent bbe348c39f
commit c29ca245c1
3 changed files with 224 additions and 172 deletions
+5 -172
View File
@@ -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 {
+159
View File
@@ -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,
},
}
}
+60
View File
@@ -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{},
}
}