Skip to content

Commit 78e75e2

Browse files
committed
Pass ctx to onStoppedLeading in gatewayType
...so operations initiated can be cancelled on re-election. Signed-off-by: Tom Pantelis <[email protected]>
1 parent f6fea40 commit 78e75e2

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

pkg/gateway/gateway.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,19 @@ func (g *gatewayType) startLeaderElection(ctx context.Context) error {
257257
return errors.Wrap(err, "error creating leader election resource lock")
258258
}
259259

260-
go leaderelection.RunOrDie(ctx, leaderelection.LeaderElectionConfig{
260+
leCtx, cancel := context.WithCancel(ctx)
261+
262+
go leaderelection.RunOrDie(leCtx, leaderelection.LeaderElectionConfig{
261263
Lock: rl,
262264
LeaseDuration: g.LeaseDuration,
263265
RenewDeadline: g.RenewDeadline,
264266
RetryPeriod: g.RetryPeriod,
265267
Callbacks: leaderelection.LeaderCallbacks{
266268
OnStartedLeading: g.onStartedLeading,
267-
OnStoppedLeading: g.onStoppedLeading,
269+
OnStoppedLeading: func() {
270+
cancel()
271+
g.onStoppedLeading(ctx)
272+
},
268273
},
269274
})
270275

@@ -314,7 +319,7 @@ func (g *gatewayType) onStartedLeading(ctx context.Context) {
314319
}
315320
}
316321

317-
func (g *gatewayType) onStoppedLeading() {
322+
func (g *gatewayType) onStoppedLeading(ctx context.Context) {
318323
logger.Info("Leadership lost")
319324

320325
// Make sure all the components were at least started before we try to restart.
@@ -328,7 +333,9 @@ func (g *gatewayType) onStoppedLeading() {
328333

329334
logger.Info("Controllers stopped")
330335

331-
ctx := context.Background()
336+
if ctx.Err() != nil {
337+
return
338+
}
332339

333340
g.updateGatewayHAStatus(ctx, subv1.HAStatusPassive)
334341

0 commit comments

Comments
 (0)