Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions pkg/component/controller/controllersleasecounter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package controller

import (
"context"
"errors"
"fmt"
"sync"
"time"
Expand Down Expand Up @@ -63,13 +64,13 @@ func (l *K0sControllersLeaseCounter) Start(context.Context) error {
return err
}

ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancelCause(context.Background())
var wg sync.WaitGroup

wg.Add(2)
go func() { defer wg.Done(); l.runLeaderElection(ctx, client) }()
go func() { defer wg.Done(); l.runLeaseCounter(ctx, kubeClient) }()
l.stop = func() { cancel(); wg.Wait() }
l.stop = func() { cancel(errors.New("component is stopping")); wg.Wait() }

return nil
}
Expand All @@ -88,7 +89,11 @@ func (l *K0sControllersLeaseCounter) runLeaderElection(ctx context.Context, clie
if status == leaderelection.StatusLeading {
l.log.Info("Holding the controller lease")
} else {
l.log.Error("Lost the controller lease")
if err := context.Cause(ctx); err != nil {
l.log.Info("Lost the controller lease: ", err)
} else {
l.log.Error("Lost the controller lease")
}
}
})
}
Expand Down
14 changes: 12 additions & 2 deletions pkg/supervisor/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ func (s *Supervisor) processWaitQuit(ctx context.Context, cmd *exec.Cmd) bool {
case <-ctx.Done():
s.log.Debugf("Attempting to terminate supervised process (%v)", context.Cause(ctx))
if err := s.terminateSupervisedProcess(cmd, waitresult); err != nil {
s.log.WithError(err).Error("Error while terminating process")
if errors.Is(err, errTerminated) {
s.log.Warn("Process ", errTerminated)
} else {
s.log.WithError(err).Error("Error while terminating process")
}
} else {
s.log.Info("Process terminated successfully")
}
Expand All @@ -88,6 +92,12 @@ func (s *Supervisor) processWaitQuit(ctx context.Context, cmd *exec.Cmd) bool {
}
}

// Indicates that a process terminated with SIGTERM. Some processes prefer to
// re-raise SIGTERM instead of exiting with a zero exit code. In that case, k0s
// cannot determine whether the process had a clean shutdown or if it even had a
// proper signal handler to begin with.
var errTerminated = errors.New("terminated with SIGTERM instead of exiting cleanly")

func (s *Supervisor) terminateSupervisedProcess(cmd *exec.Cmd, waitresult <-chan error) error {
err := requestGracefulTermination(cmd.Process)
switch {
Expand All @@ -103,7 +113,7 @@ func (s *Supervisor) terminateSupervisedProcess(cmd *exec.Cmd, waitresult <-chan
return nil
case errors.As(err, &exitErr):
if status, ok := exitErr.Sys().(syscall.WaitStatus); ok && status.Signal() == syscall.SIGTERM {
return errors.New("process terminated without handling SIGTERM")
return errTerminated
}
return exitErr
default:
Expand Down
Loading