Skip to content

Commit 464aef9

Browse files
authored
Merge pull request #44 from aimuz/master
leaderelection add context
2 parents 3aec4af + f2b6dc9 commit 464aef9

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

leaderelection/leader_election.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ type leaderElection struct {
6060
renewDeadline time.Duration
6161
retryPeriod time.Duration
6262

63+
ctx context.Context
64+
6365
clientset kubernetes.Interface
6466
}
6567

@@ -127,6 +129,11 @@ func (l *leaderElection) WithRetryPeriod(retryPeriod time.Duration) {
127129
l.retryPeriod = retryPeriod
128130
}
129131

132+
// WithContext Add context
133+
func (l *leaderElection) WithContext(ctx context.Context) {
134+
l.ctx = ctx
135+
}
136+
130137
func (l *leaderElection) Run() error {
131138
if l.identity == "" {
132139
id, err := defaultLeaderElectionIdentity()
@@ -174,7 +181,11 @@ func (l *leaderElection) Run() error {
174181
},
175182
}
176183

177-
leaderelection.RunOrDie(context.TODO(), leaderConfig)
184+
ctx := l.ctx
185+
if ctx == nil {
186+
ctx = context.Background()
187+
}
188+
leaderelection.RunOrDie(ctx, leaderConfig)
178189
return nil // should never reach here
179190
}
180191

0 commit comments

Comments
 (0)