Skip to content

Commit

Permalink
Merge pull request #4905 from twz123/exensions-controller-contexts
Browse files Browse the repository at this point in the history
Fix panic in ExtensionsController when reacquring the lease
  • Loading branch information
twz123 authored Aug 29, 2024
2 parents 0b39082 + 3a4e2b5 commit 04e0ad7
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions pkg/component/controller/extensions_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ type ExtensionsController struct {
startChan chan struct{}
mux sync.Mutex
mgr crman.Manager
mgrCtx context.Context
mgrCancelFn context.CancelFunc
controllerCtx context.Context
}

var _ manager.Component = (*ExtensionsController)(nil)
Expand Down Expand Up @@ -388,12 +386,10 @@ func (ec *ExtensionsController) Start(ctx context.Context) error {
ec.startChan = make(chan struct{}, 1)

// Do the first validation before setting callbacks
ec.mgrCtx, ec.mgrCancelFn = context.WithCancel(ctx)
var err error
ec.mgr, err = ec.instantiateManager(ec.mgrCtx)
ec.mgr, err = ec.instantiateManager(ctx)
if err != nil {
ec.L.WithError(err).Error("Can't instantiate controller-runtime manager")
ec.mgrCancelFn()
return err
}

Expand Down Expand Up @@ -425,19 +421,20 @@ func (ec *ExtensionsController) watchStartChan() {
for range ec.startChan {
ec.L.Info("Acquired leader lease")
ec.mux.Lock()
ctx, cancel := context.WithCancel(context.Background())
ec.mgrCancelFn = cancel
if ec.mgr == nil {
ec.L.Info("Instantiating controller-runtime manager")
ec.mgrCtx, ec.mgrCancelFn = context.WithCancel(ec.controllerCtx)
var err error
ec.mgr, err = ec.instantiateManager(ec.controllerCtx)
ec.mgr, err = ec.instantiateManager(ctx)
if err != nil {
ec.L.WithError(err).Error("Can't instantiate controller-runtime manager")
ec.mux.Unlock()
return
}
}
ec.mux.Unlock()
ec.startControllerManager()
ec.startControllerManager(ctx)
}
ec.L.Info("Start channel closed, stopping controller-manager")
}
Expand Down Expand Up @@ -506,10 +503,10 @@ func (ec *ExtensionsController) instantiateManager(ctx context.Context) (crman.M
return mgr, nil
}

func (ec *ExtensionsController) startControllerManager() {
func (ec *ExtensionsController) startControllerManager(ctx context.Context) {
go func() {
ec.L.Info("Starting controller-manager")
if err := ec.mgr.Start(ec.mgrCtx); err != nil {
if err := ec.mgr.Start(ctx); err != nil {
ec.L.WithError(err).Error("Controller manager working loop exited")
}
}()
Expand Down

0 comments on commit 04e0ad7

Please sign in to comment.