@@ -17,8 +17,10 @@ limitations under the License.
17
17
package util
18
18
19
19
import (
20
+ "flag"
20
21
"fmt"
21
22
"os"
23
+ "strconv"
22
24
"strings"
23
25
24
26
errorsutil "k8s.io/apimachinery/pkg/util/errors"
@@ -64,16 +66,35 @@ type preflightError interface {
64
66
// checkErr formats a given error as a string and calls the passed handleErr
65
67
// func with that string and an exit code.
66
68
func checkErr (err error , handleErr func (string , int )) {
69
+
70
+ var msg string
71
+ if err != nil {
72
+ msg = fmt .Sprintf ("%s\n To 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
+
67
88
switch err .(type ) {
68
89
case nil :
69
90
return
70
91
case preflightError :
71
- handleErr (err . Error () , PreFlightExitCode )
92
+ handleErr (msg , PreFlightExitCode )
72
93
case errorsutil.Aggregate :
73
- handleErr (err . Error () , ValidationExitCode )
94
+ handleErr (msg , ValidationExitCode )
74
95
75
96
default :
76
- handleErr (err . Error () , DefaultErrorExitCode )
97
+ handleErr (msg , DefaultErrorExitCode )
77
98
}
78
99
}
79
100
0 commit comments