kubeadm: print the stack trace of an error for klog level v>=5

- replace all stray calls of os.Exit() to util.CheckError() instead
- CheckError() now checks if the klog verbosity level is >=5
and shows a stack trace of the error
- don't call klog.Fatal in version.go
This commit is contained in:
Lubomir I. Ivanov 2019-08-03 04:47:39 +03:00
parent 2af52db689
commit 2fc19136c1
6 changed files with 35 additions and 15 deletions

View File

@ -18,7 +18,10 @@ go_library(
name = "go_default_library", name = "go_default_library",
srcs = ["kubeadm.go"], srcs = ["kubeadm.go"],
importpath = "k8s.io/kubernetes/cmd/kubeadm", importpath = "k8s.io/kubernetes/cmd/kubeadm",
deps = ["//cmd/kubeadm/app:go_default_library"], deps = [
"//cmd/kubeadm/app:go_default_library",
"//cmd/kubeadm/app/util:go_default_library",
],
) )
filegroup( filegroup(

View File

@ -10,6 +10,7 @@ go_library(
importpath = "k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow", importpath = "k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow",
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
deps = [ deps = [
"//cmd/kubeadm/app/util:go_default_library",
"//vendor/github.com/pkg/errors:go_default_library", "//vendor/github.com/pkg/errors:go_default_library",
"//vendor/github.com/spf13/cobra:go_default_library", "//vendor/github.com/spf13/cobra:go_default_library",
"//vendor/github.com/spf13/pflag:go_default_library", "//vendor/github.com/spf13/pflag:go_default_library",

View File

@ -18,12 +18,13 @@ package workflow
import ( import (
"fmt" "fmt"
"os"
"strings" "strings"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag" "github.com/spf13/pflag"
"k8s.io/kubernetes/cmd/kubeadm/app/util"
) )
// phaseSeparator defines the separator to be used when concatenating nested // phaseSeparator defines the separator to be used when concatenating nested
@ -346,10 +347,7 @@ func (e *Runner) BindToCommand(cmd *cobra.Command) {
// overrides the command triggering the Runner using the phaseCmd // overrides the command triggering the Runner using the phaseCmd
e.runCmd = cmd e.runCmd = cmd
e.Options.FilterPhases = []string{phaseSelector} e.Options.FilterPhases = []string{phaseSelector}
if err := e.Run(args); err != nil { util.CheckErr(e.Run(args))
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}, },
} }

View File

@ -62,7 +62,7 @@ func RunVersion(out io.Writer, cmd *cobra.Command) error {
const flag = "output" const flag = "output"
of, err := cmd.Flags().GetString(flag) of, err := cmd.Flags().GetString(flag)
if err != nil { if err != nil {
klog.Fatalf("error accessing flag %s for command %s: %v", flag, cmd.Name(), err) return errors.Wrapf(err, "error accessing flag %s for command %s", flag, cmd.Name())
} }
switch of { switch of {

View File

@ -17,8 +17,10 @@ limitations under the License.
package util package util
import ( import (
"flag"
"fmt" "fmt"
"os" "os"
"strconv"
"strings" "strings"
errorsutil "k8s.io/apimachinery/pkg/util/errors" errorsutil "k8s.io/apimachinery/pkg/util/errors"
@ -64,16 +66,35 @@ type preflightError interface {
// checkErr formats a given error as a string and calls the passed handleErr // checkErr formats a given error as a string and calls the passed handleErr
// func with that string and an exit code. // func with that string and an exit code.
func checkErr(err error, handleErr func(string, int)) { func checkErr(err error, handleErr func(string, int)) {
var msg string
if err != nil {
msg = fmt.Sprintf("%s\nTo see the stack trace of this error execute with --v=5 or higher", err.Error())
// check if the verbosity level in klog is high enough and print a stack trace.
f := flag.CommandLine.Lookup("v")
if f != nil {
// assume that the "v" flag contains a parseable Int32 as per klog's "Level" type alias,
// thus no error from ParseInt is handled here.
if v, e := strconv.ParseInt(f.Value.String(), 10, 32); e == nil {
// https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/logging.md
// klog.V(5) - Trace level verbosity
if v > 4 {
msg = fmt.Sprintf("%+v", err)
}
}
}
}
switch err.(type) { switch err.(type) {
case nil: case nil:
return return
case preflightError: case preflightError:
handleErr(err.Error(), PreFlightExitCode) handleErr(msg, PreFlightExitCode)
case errorsutil.Aggregate: case errorsutil.Aggregate:
handleErr(err.Error(), ValidationExitCode) handleErr(msg, ValidationExitCode)
default: default:
handleErr(err.Error(), DefaultErrorExitCode) handleErr(msg, DefaultErrorExitCode)
} }
} }

View File

@ -17,13 +17,10 @@ limitations under the License.
package main package main
import ( import (
"os"
"k8s.io/kubernetes/cmd/kubeadm/app" "k8s.io/kubernetes/cmd/kubeadm/app"
"k8s.io/kubernetes/cmd/kubeadm/app/util"
) )
func main() { func main() {
if err := app.Run(); err != nil { util.CheckErr(app.Run())
os.Exit(1)
}
} }