Skip to content

Commit

Permalink
fix: multi controller run concurrently after leadership lost (#2309)
Browse files Browse the repository at this point in the history
Co-authored-by: Pedro Juarez <[email protected]>
  • Loading branch information
drivebyer and pjuarezd authored Sep 11, 2024
1 parent b28a0b4 commit fee9a79
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions pkg/controller/main-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,12 +492,12 @@ func leaderRun(ctx context.Context, c *Controller, threadiness int, stopCh <-cha
for {
select {
case oerr := <-notificationChannel:
if !errors.Is(oerr.Err, http.ErrServerClosed) {
if oerr != nil && !errors.Is(oerr.Err, http.ErrServerClosed) {
klog.Errorf("STS API Server stopped: %v, going to restart", oerr.Err)
go c.startSTSAPIServer(ctx, notificationChannel)
}
case err := <-upgradeServerChannel:
if err != http.ErrServerClosed {
if err != nil && !errors.Is(err, http.ErrServerClosed) {
klog.Errorf("Upgrade Server stopped: %v, going to restart", err)
upgradeServerChannel = c.startUpgradeServer()
}
Expand Down Expand Up @@ -584,8 +584,24 @@ func (c *Controller) Start(threadiness int, stopCh <-chan struct{}) error {
leaderRun(ctx, c, threadiness, stopCh, notificationChannel)
},
OnStoppedLeading: func() {
// we can do cleanup here
klog.Infof("leader lost: %s", c.podName)
klog.Infof("leader lost, removing any leader labels that I '%s' might have", c.podName)
p := []patchAnnotation{{
Op: "remove",
Path: "/metadata/labels/operator",
}}

payloadBytes, err := json.Marshal(p)
if err != nil {
klog.Errorf("failed to marshal patch: %#v", err)
} else {
c.kubeClientSet.CoreV1().Pods(leaseLockNamespace).Patch(ctx, c.podName, types.JSONPatchType, payloadBytes, metav1.PatchOptions{})
}
// Even if Stop() is called twice, stopping it here ensures the sync handler no longer is handling events,
// in case SIGTERM fails or the controller takes longer to exit.
c.Stop()
if err := syscall.Kill(os.Getpid(), syscall.SIGTERM); err != nil {
klog.Errorf("error sending SIGTERM: %v", err)
}
},
OnNewLeader: func(identity string) {
// we're notified when new leader elected
Expand Down

0 comments on commit fee9a79

Please sign in to comment.