mirror of
https://github.com/wahyd4/kt-connect.git
synced 2026-08-09 05:16:02 +10:00
不适用panic,所有错误都显示返回,在main函数中统一处理
This commit is contained in:
+2
-1
@@ -31,8 +31,9 @@ func main() {
|
||||
ch := command.SetUpCloseHandler(options)
|
||||
err := app.Run(os.Args)
|
||||
if err != nil {
|
||||
log.Info().Msg(err.Error())
|
||||
log.Error().Msg(err.Error())
|
||||
command.CleanupWorkspace(options)
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
if util.IsHelpCommand(os.Args) {
|
||||
|
||||
+11
-18
@@ -22,8 +22,7 @@ type Action struct {
|
||||
// Connect connect vpn to kubernetes cluster
|
||||
func (action *Action) Connect(options *options.DaemonOptions) (err error) {
|
||||
if util.IsDaemonRunning(options.RuntimeOptions.PidFile) {
|
||||
err = fmt.Errorf("Connect already running %s exit this", options.RuntimeOptions.PidFile)
|
||||
panic(err)
|
||||
return fmt.Errorf("Connect already running %s exit this", options.RuntimeOptions.PidFile)
|
||||
}
|
||||
pid, err := util.WritePidFile(options.RuntimeOptions.PidFile)
|
||||
if err != nil {
|
||||
@@ -72,28 +71,27 @@ func (action *Action) Connect(options *options.DaemonOptions) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
factory.StartConnect(podName, endPointIP, cidrs, options.Debug)
|
||||
err = factory.StartConnect(podName, endPointIP, cidrs, options.Debug)
|
||||
return
|
||||
}
|
||||
|
||||
//Exchange exchange kubernetes workload
|
||||
func (action *Action) Exchange(swap string, options *options.DaemonOptions) {
|
||||
func (action *Action) Exchange(swap string, options *options.DaemonOptions) error {
|
||||
checkConnectRunning(options.RuntimeOptions.PidFile)
|
||||
expose := options.ExchangeOptions.Expose
|
||||
|
||||
if swap == "" || expose == "" {
|
||||
err := fmt.Errorf("-expose is required")
|
||||
panic(err.Error())
|
||||
return fmt.Errorf("-expose is required")
|
||||
}
|
||||
|
||||
clientset, err := cluster.GetKubernetesClient(options.KubeConfig)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
origin, err := clientset.AppsV1().Deployments(options.Namespace).Get(swap, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
replicas := origin.Spec.Replicas
|
||||
@@ -104,31 +102,26 @@ func (action *Action) Exchange(swap string, options *options.DaemonOptions) {
|
||||
|
||||
factory := connect.Connect{}
|
||||
_, err = factory.Exchange(options, origin, clientset, util.String2Map(options.Labels))
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
//Mesh exchange kubernetes workload
|
||||
func (action *Action) Mesh(swap string, options *options.DaemonOptions) {
|
||||
func (action *Action) Mesh(swap string, options *options.DaemonOptions) error {
|
||||
checkConnectRunning(options.RuntimeOptions.PidFile)
|
||||
expose := options.MeshOptions.Expose
|
||||
|
||||
if swap == "" || expose == "" {
|
||||
err := fmt.Errorf("-expose is required")
|
||||
panic(err.Error())
|
||||
return fmt.Errorf("-expose is required")
|
||||
}
|
||||
|
||||
clientset, err := cluster.GetKubernetesClient(options.KubeConfig)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
factory := connect.Connect{}
|
||||
_, err = factory.Mesh(swap, options, clientset, util.String2Map(options.Labels))
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// checkConnectRunning check connect is running and print help msg
|
||||
|
||||
@@ -76,8 +76,7 @@ func newConnectCommand(options *options.DaemonOptions) cli.Command {
|
||||
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
||||
}
|
||||
action := Action{}
|
||||
action.Connect(options)
|
||||
return nil
|
||||
return action.Connect(options)
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -100,8 +99,7 @@ func newExchangeCommand(options *options.DaemonOptions) cli.Command {
|
||||
}
|
||||
|
||||
action := Action{}
|
||||
action.Exchange(c.Args().First(), options)
|
||||
return nil
|
||||
return action.Exchange(c.Args().First(), options)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ import (
|
||||
func (c *Connect) Mesh(swap string, options *options.DaemonOptions, clientset *kubernetes.Clientset, labels map[string]string) (workload string, err error) {
|
||||
workload, podIP, podName, err := c.createMeshShadown(swap, clientset, labels, options.Namespace, options.Image)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
return
|
||||
}
|
||||
options.RuntimeOptions.Shadow = workload
|
||||
err = remotePortForward(options.MeshOptions.Expose, options.KubeConfig, options.Namespace, podName, podIP, options.Debug)
|
||||
|
||||
Reference in New Issue
Block a user