Skip to content

Commit

Permalink
Add termination grace period
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Ritter <[email protected]>
  • Loading branch information
Benjamin Ritter authored and l0wl3vel committed Jan 20, 2025
1 parent 4916a47 commit fcd6e13
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import (
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"

"github.com/go-logr/zapr"
Expand Down Expand Up @@ -322,9 +324,30 @@ func innerMain() int {
}
}

setupLog.Error(errors.New("Canary"), "Canary")

// Setup controllers asynchronously, they will block for certificate generation if needed.
setupErr := make(chan error)
ctx := ctrl.SetupSignalHandler()

// Setup termination with grace period. Required to give K8s Services time to disconnect the Pod endpoint on termination.
// Derived from how the controller-runtime sets up a signal handler with ctrl.SetupSignalHandler()
ctx, cancel := context.WithCancel(context.Background())

c := make(chan os.Signal, 2)
signal.Notify(c, []os.Signal{os.Interrupt, syscall.SIGTERM}...)
go func() {
<-c
setupLog.Info("Shutting Down, waiting for 10s")
go func() {
time.Sleep(10* time.Second)
setupLog.Info("Shutdown grace period finished")
cancel()
}()
<-c
setupLog.Info("Second signal received, killing now")
os.Exit(1) // second signal. Exit directly.
}()

go func() {
setupErr <- setupControllers(ctx, mgr, sw, tracker, setupFinished)
}()
Expand Down

0 comments on commit fcd6e13

Please sign in to comment.