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