@@ -1197,6 +1197,121 @@ var _ = Describe("manger.Manager", func() {
1197
1197
Expect (time .Since (beforeDone )).To (BeNumerically (">=" , 1500 * time .Millisecond ))
1198
1198
})
1199
1199
1200
+ It ("should run prestart hooks before calling Start on leader election runnables" , func () {
1201
+ m , err := New (cfg , options )
1202
+ Expect (err ).NotTo (HaveOccurred ())
1203
+ for _ , cb := range callbacks {
1204
+ cb (m )
1205
+ }
1206
+
1207
+ runnableRan := make (chan struct {})
1208
+
1209
+ Expect (m .Add (RunnableFunc (func (ctx context.Context ) error {
1210
+ close (runnableRan )
1211
+ return nil
1212
+ }))).ToNot (HaveOccurred ())
1213
+
1214
+ Expect (m .Hook (HookPrestartType , RunnableFunc (func (ctx context.Context ) error {
1215
+ Expect (m .Elected ()).ShouldNot (BeClosed ())
1216
+ Consistently (runnableRan ).ShouldNot (BeClosed ())
1217
+ return nil
1218
+ }))).ToNot (HaveOccurred ())
1219
+
1220
+ ctx , cancel := context .WithCancel (context .Background ())
1221
+ defer cancel ()
1222
+ go func () {
1223
+ defer GinkgoRecover ()
1224
+ Expect (m .Elected ()).ShouldNot (BeClosed ())
1225
+ Expect (m .Start (ctx )).NotTo (HaveOccurred ())
1226
+ }()
1227
+
1228
+ <- m .Elected ()
1229
+ })
1230
+
1231
+ It ("should run prestart hooks with timeout" , func () {
1232
+ m , err := New (cfg , options )
1233
+ Expect (err ).NotTo (HaveOccurred ())
1234
+ for _ , cb := range callbacks {
1235
+ cb (m )
1236
+ }
1237
+ m .(* controllerManager ).hookTimeout = 1 * time .Nanosecond
1238
+
1239
+ Expect (m .Hook (HookPrestartType , RunnableFunc (func (ctx context.Context ) error {
1240
+ select {
1241
+ case <- ctx .Done ():
1242
+ return ctx .Err ()
1243
+ case <- time .After (1 * time .Second ):
1244
+ return errors .New ("prestart hook timeout exceeded expected" )
1245
+ }
1246
+ }))).ToNot (HaveOccurred ())
1247
+
1248
+ ctx , cancel := context .WithCancel (context .Background ())
1249
+ defer cancel ()
1250
+
1251
+ Expect (m .Start (ctx )).Should (MatchError (context .DeadlineExceeded ))
1252
+ })
1253
+
1254
+ It ("should run prestart hooks without timeout" , func () {
1255
+ m , err := New (cfg , options )
1256
+ Expect (err ).NotTo (HaveOccurred ())
1257
+ for _ , cb := range callbacks {
1258
+ cb (m )
1259
+ }
1260
+ m .(* controllerManager ).hookTimeout = - 1 * time .Second
1261
+
1262
+ Expect (m .Add (RunnableFunc (func (ctx context.Context ) error {
1263
+ fmt .Println ("runnable returning" )
1264
+ return nil
1265
+ }))).ToNot (HaveOccurred ())
1266
+
1267
+ Expect (m .Hook (HookPrestartType , RunnableFunc (func (ctx context.Context ) error {
1268
+ select {
1269
+ case <- ctx .Done ():
1270
+ return ctx .Err ()
1271
+ case <- time .After (1 * time .Second ):
1272
+ fmt .Println ("prestart hook returning" )
1273
+ return nil
1274
+ }
1275
+ }))).ToNot (HaveOccurred ())
1276
+
1277
+ ctx , cancel := context .WithCancel (context .Background ())
1278
+ defer cancel ()
1279
+
1280
+ go func () {
1281
+ defer GinkgoRecover ()
1282
+ Expect (m .Elected ()).ShouldNot (BeClosed ())
1283
+ Expect (m .Start (ctx )).NotTo (HaveOccurred ())
1284
+ }()
1285
+
1286
+ <- m .Elected ()
1287
+ })
1288
+
1289
+ It ("should not run leader election runnables if prestart hooks fail" , func () {
1290
+ m , err := New (cfg , options )
1291
+ Expect (err ).NotTo (HaveOccurred ())
1292
+ for _ , cb := range callbacks {
1293
+ cb (m )
1294
+ }
1295
+
1296
+ runnableRan := make (chan struct {})
1297
+
1298
+ Expect (m .Add (RunnableFunc (func (ctx context.Context ) error {
1299
+ close (runnableRan )
1300
+ return nil
1301
+ }))).ToNot (HaveOccurred ())
1302
+
1303
+ Expect (m .Hook (HookPrestartType , RunnableFunc (func (ctx context.Context ) error {
1304
+ Expect (m .Elected ()).ShouldNot (BeClosed ())
1305
+ Consistently (runnableRan ).ShouldNot (BeClosed ())
1306
+ return errors .New ("prestart hook failed" )
1307
+ }))).ToNot (HaveOccurred ())
1308
+
1309
+ ctx , cancel := context .WithCancel (context .Background ())
1310
+ defer cancel ()
1311
+
1312
+ Expect (m .Elected ()).ShouldNot (BeClosed ())
1313
+ Expect (m .Start (ctx )).Should (MatchError (ContainSubstring ("prestart hook failed" )))
1314
+ })
1200
1315
}
1201
1316
1202
1317
Context ("with defaults" , func () {
0 commit comments