From 46fc74be72a8da7cbb20560b2e46f810ab58fb02 Mon Sep 17 00:00:00 2001 From: "liqiang.clq" Date: Sun, 2 Feb 2020 11:01:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8D=E9=80=82=E7=94=A8panic=EF=BC=8C?= =?UTF-8?q?=E6=89=80=E6=9C=89=E9=94=99=E8=AF=AF=E9=83=BD=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=EF=BC=8C=E5=9C=A8main=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E4=B8=AD=E7=BB=9F=E4=B8=80=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/ktctl/main.go | 3 ++- pkg/kt/command/actions.go | 29 +++++++++++------------------ pkg/kt/command/commands.go | 6 ++---- pkg/kt/connect/mesh.go | 2 +- 4 files changed, 16 insertions(+), 24 deletions(-) diff --git a/cmd/ktctl/main.go b/cmd/ktctl/main.go index 0e42869..6077803 100644 --- a/cmd/ktctl/main.go +++ b/cmd/ktctl/main.go @@ -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) { diff --git a/pkg/kt/command/actions.go b/pkg/kt/command/actions.go index 29b5405..87d263d 100644 --- a/pkg/kt/command/actions.go +++ b/pkg/kt/command/actions.go @@ -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 diff --git a/pkg/kt/command/commands.go b/pkg/kt/command/commands.go index c2c0697..52f9c39 100644 --- a/pkg/kt/command/commands.go +++ b/pkg/kt/command/commands.go @@ -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) }, } } diff --git a/pkg/kt/connect/mesh.go b/pkg/kt/connect/mesh.go index 42da5de..5403fa3 100644 --- a/pkg/kt/connect/mesh.go +++ b/pkg/kt/connect/mesh.go @@ -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)