@@ -257,14 +257,19 @@ func (g *gatewayType) startLeaderElection(ctx context.Context) error {
257
257
return errors .Wrap (err , "error creating leader election resource lock" )
258
258
}
259
259
260
- go leaderelection .RunOrDie (ctx , leaderelection.LeaderElectionConfig {
260
+ leCtx , cancel := context .WithCancel (ctx )
261
+
262
+ go leaderelection .RunOrDie (leCtx , leaderelection.LeaderElectionConfig {
261
263
Lock : rl ,
262
264
LeaseDuration : g .LeaseDuration ,
263
265
RenewDeadline : g .RenewDeadline ,
264
266
RetryPeriod : g .RetryPeriod ,
265
267
Callbacks : leaderelection.LeaderCallbacks {
266
268
OnStartedLeading : g .onStartedLeading ,
267
- OnStoppedLeading : g .onStoppedLeading ,
269
+ OnStoppedLeading : func () {
270
+ cancel ()
271
+ g .onStoppedLeading (ctx )
272
+ },
268
273
},
269
274
})
270
275
@@ -314,7 +319,7 @@ func (g *gatewayType) onStartedLeading(ctx context.Context) {
314
319
}
315
320
}
316
321
317
- func (g * gatewayType ) onStoppedLeading () {
322
+ func (g * gatewayType ) onStoppedLeading (ctx context. Context ) {
318
323
logger .Info ("Leadership lost" )
319
324
320
325
// Make sure all the components were at least started before we try to restart.
@@ -328,7 +333,9 @@ func (g *gatewayType) onStoppedLeading() {
328
333
329
334
logger .Info ("Controllers stopped" )
330
335
331
- ctx := context .Background ()
336
+ if ctx .Err () != nil {
337
+ return
338
+ }
332
339
333
340
g .updateGatewayHAStatus (ctx , subv1 .HAStatusPassive )
334
341
0 commit comments