@@ -1145,6 +1145,121 @@ var _ = Describe("manger.Manager", func() {
1145
1145
Expect (time .Since (beforeDone )).To (BeNumerically (">=" , 1500 * time .Millisecond ))
1146
1146
})
1147
1147
1148
+ It ("should run prestart hooks before calling Start on leader election runnables" , func () {
1149
+ m , err := New (cfg , options )
1150
+ Expect (err ).NotTo (HaveOccurred ())
1151
+ for _ , cb := range callbacks {
1152
+ cb (m )
1153
+ }
1154
+
1155
+ runnableRan := make (chan struct {})
1156
+
1157
+ Expect (m .Add (RunnableFunc (func (ctx context.Context ) error {
1158
+ close (runnableRan )
1159
+ return nil
1160
+ }))).ToNot (HaveOccurred ())
1161
+
1162
+ Expect (m .Hook (HookPrestartType , RunnableFunc (func (ctx context.Context ) error {
1163
+ Expect (m .Elected ()).ShouldNot (BeClosed ())
1164
+ Consistently (runnableRan ).ShouldNot (BeClosed ())
1165
+ return nil
1166
+ }))).ToNot (HaveOccurred ())
1167
+
1168
+ ctx , cancel := context .WithCancel (context .Background ())
1169
+ defer cancel ()
1170
+ go func () {
1171
+ defer GinkgoRecover ()
1172
+ Expect (m .Elected ()).ShouldNot (BeClosed ())
1173
+ Expect (m .Start (ctx )).NotTo (HaveOccurred ())
1174
+ }()
1175
+
1176
+ <- m .Elected ()
1177
+ })
1178
+
1179
+ It ("should run prestart hooks with timeout" , func () {
1180
+ m , err := New (cfg , options )
1181
+ Expect (err ).NotTo (HaveOccurred ())
1182
+ for _ , cb := range callbacks {
1183
+ cb (m )
1184
+ }
1185
+ m .(* controllerManager ).hookTimeout = 1 * time .Nanosecond
1186
+
1187
+ Expect (m .Hook (HookPrestartType , RunnableFunc (func (ctx context.Context ) error {
1188
+ select {
1189
+ case <- ctx .Done ():
1190
+ return ctx .Err ()
1191
+ case <- time .After (1 * time .Second ):
1192
+ return errors .New ("prestart hook timeout exceeded expected" )
1193
+ }
1194
+ }))).ToNot (HaveOccurred ())
1195
+
1196
+ ctx , cancel := context .WithCancel (context .Background ())
1197
+ defer cancel ()
1198
+
1199
+ Expect (m .Start (ctx )).Should (MatchError (context .DeadlineExceeded ))
1200
+ })
1201
+
1202
+ It ("should run prestart hooks without timeout" , func () {
1203
+ m , err := New (cfg , options )
1204
+ Expect (err ).NotTo (HaveOccurred ())
1205
+ for _ , cb := range callbacks {
1206
+ cb (m )
1207
+ }
1208
+ m .(* controllerManager ).hookTimeout = - 1 * time .Second
1209
+
1210
+ Expect (m .Add (RunnableFunc (func (ctx context.Context ) error {
1211
+ fmt .Println ("runnable returning" )
1212
+ return nil
1213
+ }))).ToNot (HaveOccurred ())
1214
+
1215
+ Expect (m .Hook (HookPrestartType , RunnableFunc (func (ctx context.Context ) error {
1216
+ select {
1217
+ case <- ctx .Done ():
1218
+ return ctx .Err ()
1219
+ case <- time .After (1 * time .Second ):
1220
+ fmt .Println ("prestart hook returning" )
1221
+ return nil
1222
+ }
1223
+ }))).ToNot (HaveOccurred ())
1224
+
1225
+ ctx , cancel := context .WithCancel (context .Background ())
1226
+ defer cancel ()
1227
+
1228
+ go func () {
1229
+ defer GinkgoRecover ()
1230
+ Expect (m .Elected ()).ShouldNot (BeClosed ())
1231
+ Expect (m .Start (ctx )).NotTo (HaveOccurred ())
1232
+ }()
1233
+
1234
+ <- m .Elected ()
1235
+ })
1236
+
1237
+ It ("should not run leader election runnables if prestart hooks fail" , func () {
1238
+ m , err := New (cfg , options )
1239
+ Expect (err ).NotTo (HaveOccurred ())
1240
+ for _ , cb := range callbacks {
1241
+ cb (m )
1242
+ }
1243
+
1244
+ runnableRan := make (chan struct {})
1245
+
1246
+ Expect (m .Add (RunnableFunc (func (ctx context.Context ) error {
1247
+ close (runnableRan )
1248
+ return nil
1249
+ }))).ToNot (HaveOccurred ())
1250
+
1251
+ Expect (m .Hook (HookPrestartType , RunnableFunc (func (ctx context.Context ) error {
1252
+ Expect (m .Elected ()).ShouldNot (BeClosed ())
1253
+ Consistently (runnableRan ).ShouldNot (BeClosed ())
1254
+ return errors .New ("prestart hook failed" )
1255
+ }))).ToNot (HaveOccurred ())
1256
+
1257
+ ctx , cancel := context .WithCancel (context .Background ())
1258
+ defer cancel ()
1259
+
1260
+ Expect (m .Elected ()).ShouldNot (BeClosed ())
1261
+ Expect (m .Start (ctx )).Should (MatchError (ContainSubstring ("prestart hook failed" )))
1262
+ })
1148
1263
}
1149
1264
1150
1265
Context ("with defaults" , func () {
0 commit comments