Skip to content

Commit 2fc1913

Browse files
committed
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
1 parent 2af52db commit 2fc1913

File tree

6 files changed

+35
-15
lines changed

6 files changed

+35
-15
lines changed

cmd/kubeadm/BUILD

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ go_library(
1818
name = "go_default_library",
1919
srcs = ["kubeadm.go"],
2020
importpath = "k8s.io/kubernetes/cmd/kubeadm",
21-
deps = ["//cmd/kubeadm/app:go_default_library"],
21+
deps = [
22+
"//cmd/kubeadm/app:go_default_library",
23+
"//cmd/kubeadm/app/util:go_default_library",
24+
],
2225
)
2326

2427
filegroup(

cmd/kubeadm/app/cmd/phases/workflow/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ go_library(
1010
importpath = "k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow",
1111
visibility = ["//visibility:public"],
1212
deps = [
13+
"//cmd/kubeadm/app/util:go_default_library",
1314
"//vendor/github.com/pkg/errors:go_default_library",
1415
"//vendor/github.com/spf13/cobra:go_default_library",
1516
"//vendor/github.com/spf13/pflag:go_default_library",

cmd/kubeadm/app/cmd/phases/workflow/runner.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ package workflow
1818

1919
import (
2020
"fmt"
21-
"os"
2221
"strings"
2322

2423
"github.com/pkg/errors"
2524
"github.com/spf13/cobra"
2625
"github.com/spf13/pflag"
26+
27+
"k8s.io/kubernetes/cmd/kubeadm/app/util"
2728
)
2829

2930
// phaseSeparator defines the separator to be used when concatenating nested
@@ -346,10 +347,7 @@ func (e *Runner) BindToCommand(cmd *cobra.Command) {
346347
// overrides the command triggering the Runner using the phaseCmd
347348
e.runCmd = cmd
348349
e.Options.FilterPhases = []string{phaseSelector}
349-
if err := e.Run(args); err != nil {
350-
fmt.Fprintln(os.Stderr, err)
351-
os.Exit(1)
352-
}
350+
util.CheckErr(e.Run(args))
353351
},
354352
}
355353

cmd/kubeadm/app/cmd/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func RunVersion(out io.Writer, cmd *cobra.Command) error {
6262
const flag = "output"
6363
of, err := cmd.Flags().GetString(flag)
6464
if err != nil {
65-
klog.Fatalf("error accessing flag %s for command %s: %v", flag, cmd.Name(), err)
65+
return errors.Wrapf(err, "error accessing flag %s for command %s", flag, cmd.Name())
6666
}
6767

6868
switch of {

cmd/kubeadm/app/util/error.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ limitations under the License.
1717
package util
1818

1919
import (
20+
"flag"
2021
"fmt"
2122
"os"
23+
"strconv"
2224
"strings"
2325

2426
errorsutil "k8s.io/apimachinery/pkg/util/errors"
@@ -64,16 +66,35 @@ type preflightError interface {
6466
// checkErr formats a given error as a string and calls the passed handleErr
6567
// func with that string and an exit code.
6668
func checkErr(err error, handleErr func(string, int)) {
69+
70+
var msg string
71+
if err != nil {
72+
msg = fmt.Sprintf("%s\nTo see the stack trace of this error execute with --v=5 or higher", err.Error())
73+
// check if the verbosity level in klog is high enough and print a stack trace.
74+
f := flag.CommandLine.Lookup("v")
75+
if f != nil {
76+
// assume that the "v" flag contains a parseable Int32 as per klog's "Level" type alias,
77+
// thus no error from ParseInt is handled here.
78+
if v, e := strconv.ParseInt(f.Value.String(), 10, 32); e == nil {
79+
// https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/logging.md
80+
// klog.V(5) - Trace level verbosity
81+
if v > 4 {
82+
msg = fmt.Sprintf("%+v", err)
83+
}
84+
}
85+
}
86+
}
87+
6788
switch err.(type) {
6889
case nil:
6990
return
7091
case preflightError:
71-
handleErr(err.Error(), PreFlightExitCode)
92+
handleErr(msg, PreFlightExitCode)
7293
case errorsutil.Aggregate:
73-
handleErr(err.Error(), ValidationExitCode)
94+
handleErr(msg, ValidationExitCode)
7495

7596
default:
76-
handleErr(err.Error(), DefaultErrorExitCode)
97+
handleErr(msg, DefaultErrorExitCode)
7798
}
7899
}
79100

cmd/kubeadm/kubeadm.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@ limitations under the License.
1717
package main
1818

1919
import (
20-
"os"
21-
2220
"k8s.io/kubernetes/cmd/kubeadm/app"
21+
"k8s.io/kubernetes/cmd/kubeadm/app/util"
2322
)
2423

2524
func main() {
26-
if err := app.Run(); err != nil {
27-
os.Exit(1)
28-
}
25+
util.CheckErr(app.Run())
2926
}

0 commit comments

Comments
 (0)