Browse code

panic() instead of logrus.Fatal() in init funcs

Some packages were using `logrus.Fatal()` in init functions (which logs the error,
and (by default) calls `os.Exit(1)` after logging).

Given that logrus formatting and outputs have not yet been configured during the
initialization stage, it does not provide much benefits over a plain `panic()`.

This patch replaces some instances of `logrus.Fatal()` with `panic()`, which has
the added benefits of not introducing logrus as a dependency in some of these
packages, and also produces a stacktrace, which could help locating the problem
in the unlikely event an `init()` fails.

Before this change, an error would look like:

$ dockerd
FATA[0000] something bad happened

After this change, the same error looks like:

$ dockerd
panic: something bad happened

goroutine 1 [running]:
github.com/docker/docker/daemon/logger/awslogs.init.0()
/go/src/github.com/docker/docker/daemon/logger/awslogs/cloudwatchlogs.go:128 +0x89

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2022/04/21 18:50:37
Showing 13 changed files
... ...
@@ -120,10 +120,10 @@ type byTimestamp []wrappedEvent
120 120
 // init registers the awslogs driver
121 121
 func init() {
122 122
 	if err := logger.RegisterLogDriver(name, New); err != nil {
123
-		logrus.Fatal(err)
123
+		panic(err)
124 124
 	}
125 125
 	if err := logger.RegisterLogOptValidator(name, ValidateLogOpt); err != nil {
126
-		logrus.Fatal(err)
126
+		panic(err)
127 127
 	}
128 128
 }
129 129
 
... ...
@@ -48,7 +48,7 @@ var mu sync.Mutex
48 48
 func init() {
49 49
 	providerHandle = windows.InvalidHandle
50 50
 	if err := logger.RegisterLogDriver(name, New); err != nil {
51
-		logrus.Fatal(err)
51
+		panic(err)
52 52
 	}
53 53
 }
54 54
 
... ...
@@ -62,10 +62,10 @@ const (
62 62
 
63 63
 func init() {
64 64
 	if err := logger.RegisterLogDriver(name, New); err != nil {
65
-		logrus.Fatal(err)
65
+		panic(err)
66 66
 	}
67 67
 	if err := logger.RegisterLogOptValidator(name, ValidateLogOpt); err != nil {
68
-		logrus.Fatal(err)
68
+		panic(err)
69 69
 	}
70 70
 }
71 71
 
... ...
@@ -43,13 +43,12 @@ var (
43 43
 )
44 44
 
45 45
 func init() {
46
-
47 46
 	if err := logger.RegisterLogDriver(name, New); err != nil {
48
-		logrus.Fatal(err)
47
+		panic(err)
49 48
 	}
50 49
 
51 50
 	if err := logger.RegisterLogOptValidator(name, ValidateLogOpts); err != nil {
52
-		logrus.Fatal(err)
51
+		panic(err)
53 52
 	}
54 53
 }
55 54
 
... ...
@@ -14,7 +14,6 @@ import (
14 14
 	"github.com/Graylog2/go-gelf/gelf"
15 15
 	"github.com/docker/docker/daemon/logger"
16 16
 	"github.com/docker/docker/daemon/logger/loggerutils"
17
-	"github.com/sirupsen/logrus"
18 17
 )
19 18
 
20 19
 const name = "gelf"
... ...
@@ -28,10 +27,10 @@ type gelfLogger struct {
28 28
 
29 29
 func init() {
30 30
 	if err := logger.RegisterLogDriver(name, New); err != nil {
31
-		logrus.Fatal(err)
31
+		panic(err)
32 32
 	}
33 33
 	if err := logger.RegisterLogOptValidator(name, ValidateLogOpt); err != nil {
34
-		logrus.Fatal(err)
34
+		panic(err)
35 35
 	}
36 36
 }
37 37
 
... ...
@@ -14,7 +14,6 @@ import (
14 14
 	"github.com/coreos/go-systemd/v22/journal"
15 15
 	"github.com/docker/docker/daemon/logger"
16 16
 	"github.com/docker/docker/daemon/logger/loggerutils"
17
-	"github.com/sirupsen/logrus"
18 17
 )
19 18
 
20 19
 const name = "journald"
... ...
@@ -27,10 +26,10 @@ type journald struct {
27 27
 
28 28
 func init() {
29 29
 	if err := logger.RegisterLogDriver(name, New); err != nil {
30
-		logrus.Fatal(err)
30
+		panic(err)
31 31
 	}
32 32
 	if err := logger.RegisterLogOptValidator(name, validateLogOpt); err != nil {
33
-		logrus.Fatal(err)
33
+		panic(err)
34 34
 	}
35 35
 }
36 36
 
... ...
@@ -15,7 +15,6 @@ import (
15 15
 	"github.com/docker/docker/daemon/logger/loggerutils"
16 16
 	units "github.com/docker/go-units"
17 17
 	"github.com/pkg/errors"
18
-	"github.com/sirupsen/logrus"
19 18
 )
20 19
 
21 20
 // Name is the name of the file that the jsonlogger logs to.
... ...
@@ -32,10 +31,10 @@ type JSONFileLogger struct {
32 32
 
33 33
 func init() {
34 34
 	if err := logger.RegisterLogDriver(Name, New); err != nil {
35
-		logrus.Fatal(err)
35
+		panic(err)
36 36
 	}
37 37
 	if err := logger.RegisterLogOptValidator(Name, ValidateLogOpt); err != nil {
38
-		logrus.Fatal(err)
38
+		panic(err)
39 39
 	}
40 40
 }
41 41
 
... ...
@@ -14,7 +14,6 @@ import (
14 14
 	"github.com/docker/docker/errdefs"
15 15
 	units "github.com/docker/go-units"
16 16
 	"github.com/pkg/errors"
17
-	"github.com/sirupsen/logrus"
18 17
 )
19 18
 
20 19
 const (
... ...
@@ -49,10 +48,10 @@ func ValidateLogOpt(cfg map[string]string) error {
49 49
 
50 50
 func init() {
51 51
 	if err := logger.RegisterLogDriver(Name, New); err != nil {
52
-		logrus.Fatal(err)
52
+		panic(err)
53 53
 	}
54 54
 	if err := logger.RegisterLogOptValidator(Name, ValidateLogOpt); err != nil {
55
-		logrus.Fatal(err)
55
+		panic(err)
56 56
 	}
57 57
 }
58 58
 
... ...
@@ -29,10 +29,10 @@ const (
29 29
 
30 30
 func init() {
31 31
 	if err := logger.RegisterLogDriver(name, New); err != nil {
32
-		logrus.Fatal(err)
32
+		panic(err)
33 33
 	}
34 34
 	if err := logger.RegisterLogOptValidator(name, ValidateLogOpt); err != nil {
35
-		logrus.Fatal(err)
35
+		panic(err)
36 36
 	}
37 37
 }
38 38
 
... ...
@@ -142,10 +142,10 @@ const (
142 142
 
143 143
 func init() {
144 144
 	if err := logger.RegisterLogDriver(driverName, New); err != nil {
145
-		logrus.Fatal(err)
145
+		panic(err)
146 146
 	}
147 147
 	if err := logger.RegisterLogOptValidator(driverName, ValidateLogOpt); err != nil {
148
-		logrus.Fatal(err)
148
+		panic(err)
149 149
 	}
150 150
 }
151 151
 
... ...
@@ -16,7 +16,6 @@ import (
16 16
 	"github.com/docker/docker/daemon/logger"
17 17
 	"github.com/docker/docker/daemon/logger/loggerutils"
18 18
 	"github.com/docker/go-connections/tlsconfig"
19
-	"github.com/sirupsen/logrus"
20 19
 )
21 20
 
22 21
 const (
... ...
@@ -54,10 +53,10 @@ type syslogger struct {
54 54
 
55 55
 func init() {
56 56
 	if err := logger.RegisterLogDriver(name, New); err != nil {
57
-		logrus.Fatal(err)
57
+		panic(err)
58 58
 	}
59 59
 	if err := logger.RegisterLogOptValidator(name, ValidateLogOpt); err != nil {
60
-		logrus.Fatal(err)
60
+		panic(err)
61 61
 	}
62 62
 }
63 63
 
... ...
@@ -54,8 +54,7 @@ func init() {
54 54
 
55 55
 	testEnv, err = environment.New()
56 56
 	if err != nil {
57
-		fmt.Println(err)
58
-		os.Exit(1)
57
+		panic(err)
59 58
 	}
60 59
 }
61 60
 
... ...
@@ -34,13 +34,11 @@ type NetworkToSplit struct {
34 34
 func init() {
35 35
 	var err error
36 36
 	if PredefinedGlobalScopeDefaultNetworks, err = splitNetworks(globalScopeDefaultNetworks); err != nil {
37
-		//we are going to panic in case of error as we should never get into this state
38
-		panic("InitAddressPools failed to initialize the global scope default address pool")
37
+		panic("failed to initialize the global scope default address pool: " + err.Error())
39 38
 	}
40 39
 
41 40
 	if PredefinedLocalScopeDefaultNetworks, err = splitNetworks(localScopeDefaultNetworks); err != nil {
42
-		//we are going to panic in case of error as we should never get into this state
43
-		panic("InitAddressPools failed to initialize the local scope default address pool")
41
+		panic("failed to initialize the local scope default address pool: " + err.Error())
44 42
 	}
45 43
 }
46 44