From b8ae016ee645a81f6d416a53e9941a0bbacf1897 Mon Sep 17 00:00:00 2001 From: sanposhiho <44139130+sanposhiho@users.noreply.github.com> Date: Sun, 22 Aug 2021 19:20:47 +0900 Subject: [PATCH] Fix: return error instead of os.Exit when something goes wrong --- cmd/kube-scheduler/app/server.go | 9 ++++----- cmd/kube-scheduler/scheduler.go | 12 +++++++++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/cmd/kube-scheduler/app/server.go b/cmd/kube-scheduler/app/server.go index 0234379dac2..d9781431466 100644 --- a/cmd/kube-scheduler/app/server.go +++ b/cmd/kube-scheduler/app/server.go @@ -79,15 +79,14 @@ suitable Node. Multiple different schedulers may be used within a cluster; kube-scheduler is the reference implementation. See [scheduling](https://kubernetes.io/docs/concepts/scheduling-eviction/) for more information about scheduling and the kube-scheduler component.`, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { if err := opts.Complete(&namedFlagSets); err != nil { - fmt.Fprintf(os.Stderr, "%v\n", err) - os.Exit(1) + return fmt.Errorf("completes the remaining instantiation of the options: %w", err) } if err := runCommand(cmd, opts, registryOptions...); err != nil { - fmt.Fprintf(os.Stderr, "%v\n", err) - os.Exit(1) + return fmt.Errorf("run command: %w", err) } + return nil }, Args: func(cmd *cobra.Command, args []string) error { for _, arg := range args { diff --git a/cmd/kube-scheduler/scheduler.go b/cmd/kube-scheduler/scheduler.go index 521bf5582ec..0d21ccedb5f 100644 --- a/cmd/kube-scheduler/scheduler.go +++ b/cmd/kube-scheduler/scheduler.go @@ -17,6 +17,7 @@ limitations under the License. package main import ( + "fmt" "math/rand" "os" "time" @@ -32,6 +33,13 @@ import ( ) func main() { + if err := runSchedulerCmd(); err != nil { + fmt.Fprintf(os.Stderr, "failed to run scheduler command: %v\n", err) + os.Exit(1) + } +} + +func runSchedulerCmd() error { rand.Seed(time.Now().UnixNano()) pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc) @@ -42,6 +50,8 @@ func main() { defer logs.FlushLogs() if err := command.Execute(); err != nil { - os.Exit(1) + return fmt.Errorf("execute command: %w", err) } + + return nil }