@@ -20,6 +20,7 @@ import (
2020 "crypto/tls"
2121 "flag"
2222 "os"
23+ "runtime/debug"
2324 "time"
2425
2526 "go.uber.org/zap/zapcore"
@@ -86,6 +87,15 @@ func main() {
8687
8788 ctrl .SetLogger (zap .New (zap .UseFlagOptions (& opts )))
8889
90+ // Recover any panic and log using the configured logger. This ensures that panics get logged in JSON format if
91+ // JSON logging is enabled.
92+ defer func () {
93+ if r := recover (); r != nil {
94+ setupLog .Error (nil , "recovered from panic" , "panic" , r , "trace" , string (debug .Stack ()))
95+ os .Exit (1 )
96+ }
97+ }()
98+
8999 // if the enable-http2 flag is false (the default), http/2 should be disabled
90100 // due to its vulnerabilities. More specifically, disabling http/2 will
91101 // prevent from being vulnerable to the HTTP/2 Stream Cancellation and
@@ -130,8 +140,7 @@ func main() {
130140 // LeaderElectionReleaseOnCancel: true,
131141 })
132142 if err != nil || mgr == nil {
133- setupLog .Error (err , "unable to start manager" )
134- os .Exit (1 )
143+ panic ("unable to start manager" )
135144 }
136145
137146 // TODO: Create secret informer, and possibly ScmProvider Informer to pass into controllers
@@ -145,30 +154,26 @@ func main() {
145154 Scheme : mgr .GetScheme (),
146155 Recorder : mgr .GetEventRecorderFor ("PullRequest" ),
147156 }).SetupWithManager (mgr ); err != nil {
148- setupLog .Error (err , "unable to create controller" , "controller" , "PullRequest" )
149- os .Exit (1 )
157+ panic ("unable to create PullRequest controller" )
150158 }
151159 if err = (& controller.CommitStatusReconciler {
152160 Client : mgr .GetClient (),
153161 Scheme : mgr .GetScheme (),
154162 Recorder : mgr .GetEventRecorderFor ("CommitStatus" ),
155163 }).SetupWithManager (mgr ); err != nil {
156- setupLog .Error (err , "unable to create controller" , "controller" , "CommitStatus" )
157- os .Exit (1 )
164+ panic ("unable to create CommitStatus controller" )
158165 }
159166 if err = (& controller.RevertCommitReconciler {
160167 Client : mgr .GetClient (),
161168 Scheme : mgr .GetScheme (),
162169 Recorder : mgr .GetEventRecorderFor ("RevertCommit" ),
163170 }).SetupWithManager (mgr ); err != nil {
164- setupLog .Error (err , "unable to create controller" , "controller" , "RevertCommit" )
165- os .Exit (1 )
171+ panic ("unable to create RevertCommit controller" )
166172 }
167173
168174 promotionStrategyRequeueDuration , err := time .ParseDuration (promotionStrategyRequeue )
169175 if err != nil {
170- setupLog .Error (err , "failed to parse promotion strategy requeue duration" )
171- os .Exit (1 )
176+ panic ("failed to parse promotion strategy requeue duration" )
172177 }
173178 if err = (& controller.PromotionStrategyReconciler {
174179 Client : mgr .GetClient (),
@@ -178,29 +183,25 @@ func main() {
178183 RequeueDuration : promotionStrategyRequeueDuration ,
179184 },
180185 }).SetupWithManager (mgr ); err != nil {
181- setupLog .Error (err , "unable to create controller" , "controller" , "PromotionStrategy" )
182- os .Exit (1 )
186+ panic ("unable to create PromotionStrategy controller" )
183187 }
184188 if err = (& controller.ScmProviderReconciler {
185189 Client : mgr .GetClient (),
186190 Scheme : mgr .GetScheme (),
187191 Recorder : mgr .GetEventRecorderFor ("ScmProvider" ),
188192 }).SetupWithManager (mgr ); err != nil {
189- setupLog .Error (err , "unable to create controller" , "controller" , "ScmProvider" )
190- os .Exit (1 )
193+ panic ("unable to create ScmProvider controller" )
191194 }
192195 if err = (& controller.GitRepositoryReconciler {
193196 Client : mgr .GetClient (),
194197 Scheme : mgr .GetScheme (),
195198 }).SetupWithManager (mgr ); err != nil {
196- setupLog .Error (err , "unable to create controller" , "controller" , "GitRepository" )
197- os .Exit (1 )
199+ panic ("unable to create GitRepository controller" )
198200 }
199201
200202 ctpRequeueDuration , err := time .ParseDuration (changeTransferPolicyRequeue )
201203 if err != nil {
202- setupLog .Error (err , "failed to parse proposed commit requeue duration" )
203- os .Exit (1 )
204+ panic ("failed to parse proposed commit requeue duration" )
204205 }
205206 if err = (& controller.ChangeTransferPolicyReconciler {
206207 Client : mgr .GetClient (),
@@ -211,18 +212,15 @@ func main() {
211212 RequeueDuration : ctpRequeueDuration ,
212213 },
213214 }).SetupWithManager (mgr ); err != nil {
214- setupLog .Error (err , "unable to create controller" , "controller" , "ChangeTransferPolicy" )
215- os .Exit (1 )
215+ panic ("unable to create ChangeTransferPolicy controller" )
216216 }
217217 //+kubebuilder:scaffold:builder
218218
219219 if err := mgr .AddHealthzCheck ("healthz" , healthz .Ping ); err != nil {
220- setupLog .Error (err , "unable to set up health check" )
221- os .Exit (1 )
220+ panic ("unable to set up health check" )
222221 }
223222 if err := mgr .AddReadyzCheck ("readyz" , healthz .Ping ); err != nil {
224- setupLog .Error (err , "unable to set up ready check" )
225- os .Exit (1 )
223+ panic ("unable to set up ready check" )
226224 }
227225
228226 processSignals := ctrl .SetupSignalHandler ()
@@ -233,8 +231,7 @@ func main() {
233231
234232 setupLog .Info ("starting manager" )
235233 if err := mgr .Start (processSignals ); err != nil {
236- setupLog .Error (err , "problem running manager" )
237- os .Exit (1 )
234+ panic ("problem running manager" )
238235 }
239236 setupLog .Info ("Cleaning up cloned directories" )
240237
0 commit comments