Skip to content

Commit

Permalink
Use context.TODO() to be explicit that cancellation is not implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
ash2k committed Jun 7, 2018
1 parent 3252beb commit 102090d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 17 deletions.
7 changes: 2 additions & 5 deletions cmd/cloud-controller-manager/app/controllermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,8 @@ func Run(c *cloudcontrollerconfig.CompletedConfig) error {
}
}

runCtx, cancel := context.WithCancel(context.Background())
defer cancel()

if !c.ComponentConfig.GenericComponent.LeaderElection.LeaderElect {
run(runCtx)
run(context.TODO())
panic("unreachable")
}

Expand All @@ -187,7 +184,7 @@ func Run(c *cloudcontrollerconfig.CompletedConfig) error {
}

// Try and become the leader and start cloud controller manager loops
leaderelection.RunOrDie(runCtx, leaderelection.LeaderElectionConfig{
leaderelection.RunOrDie(context.TODO(), leaderelection.LeaderElectionConfig{
Lock: rl,
LeaseDuration: c.ComponentConfig.GenericComponent.LeaderElection.LeaseDuration.Duration,
RenewDeadline: c.ComponentConfig.GenericComponent.LeaderElection.RenewDeadline.Duration,
Expand Down
7 changes: 2 additions & 5 deletions cmd/kube-controller-manager/app/controllermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,8 @@ func Run(c *config.CompletedConfig) error {
select {}
}

runCtx, cancel := context.WithCancel(context.Background())
defer cancel()

if !c.ComponentConfig.GenericComponent.LeaderElection.LeaderElect {
run(runCtx)
run(context.TODO())
panic("unreachable")
}

Expand All @@ -208,7 +205,7 @@ func Run(c *config.CompletedConfig) error {
glog.Fatalf("error creating lock: %v", err)
}

leaderelection.RunOrDie(runCtx, leaderelection.LeaderElectionConfig{
leaderelection.RunOrDie(context.TODO(), leaderelection.LeaderElectionConfig{
Lock: rl,
LeaseDuration: c.ComponentConfig.GenericComponent.LeaderElection.LeaseDuration.Duration,
RenewDeadline: c.ComponentConfig.GenericComponent.LeaderElection.RenewDeadline.Duration,
Expand Down
8 changes: 4 additions & 4 deletions cmd/kube-scheduler/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,14 @@ func Run(c schedulerserverconfig.CompletedConfig, stopCh <-chan struct{}) error
<-ctx.Done()
}

runCtx, cancel := context.WithCancel(context.TODO()) // once Run() accepts a context, it should be used here
ctx, cancel := context.WithCancel(context.TODO()) // TODO once Run() accepts a context, it should be used here
defer cancel()

go func() {
select {
case <-stopCh:
cancel()
case <-runCtx.Done():
case <-ctx.Done():
}
}()

Expand All @@ -211,13 +211,13 @@ func Run(c schedulerserverconfig.CompletedConfig, stopCh <-chan struct{}) error
return fmt.Errorf("couldn't create leader elector: %v", err)
}

leaderElector.Run(runCtx)
leaderElector.Run(ctx)

return fmt.Errorf("lost lease")
}

// Leader election is disabled, so run inline until done.
run(runCtx)
run(ctx)
return fmt.Errorf("finished without leader elect")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,9 @@ func (le *LeaderElector) renew(ctx context.Context) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
wait.Until(func() {
// PollUntil() sleeps for "interval" duration before calling the function so we need to increase the timeout by le.config.RetryPeriod
timeoutCtx, timeoutCancel := context.WithTimeout(ctx, le.config.RetryPeriod+le.config.RenewDeadline)
timeoutCtx, timeoutCancel := context.WithTimeout(ctx, le.config.RenewDeadline)
defer timeoutCancel()
err := wait.PollUntil(le.config.RetryPeriod, func() (bool, error) {
err := wait.PollImmediateUntil(le.config.RetryPeriod, func() (bool, error) {
return le.tryAcquireOrRenew(), nil
}, timeoutCtx.Done())
le.maybeReportTransition()
Expand Down

0 comments on commit 102090d

Please sign in to comment.