Skip to content

Commit 10d4886

Browse files
Benjamin Ritterl0wl3vel
Benjamin Ritter
authored andcommitted
Add termination grace period
Signed-off-by: Benjamin Ritter <benjamin.ritter@stackit.cloud>
1 parent 5b973ad commit 10d4886

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

main.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ import (
2626
"net/http"
2727
_ "net/http/pprof"
2828
"os"
29+
"os/signal"
2930
"path/filepath"
31+
"syscall"
3032
"time"
3133

3234
"github.com/go-logr/zapr"
@@ -319,9 +321,30 @@ func innerMain() int {
319321
}
320322
}
321323

324+
setupLog.Error(errors.New("Canary"), "Canary")
325+
322326
// Setup controllers asynchronously, they will block for certificate generation if needed.
323327
setupErr := make(chan error)
324-
ctx := ctrl.SetupSignalHandler()
328+
329+
// Setup termination with grace period. Required to give K8s Services time to disconnect the Pod endpoint on termination.
330+
// Derived from how the controller-runtime sets up a signal handler with ctrl.SetupSignalHandler()
331+
ctx, cancel := context.WithCancel(context.Background())
332+
333+
c := make(chan os.Signal, 2)
334+
signal.Notify(c, []os.Signal{os.Interrupt, syscall.SIGTERM}...)
335+
go func() {
336+
<-c
337+
setupLog.Info("Shutting Down, waiting for 10s")
338+
go func() {
339+
time.Sleep(10* time.Second)
340+
setupLog.Info("Shutdown grace period finished")
341+
cancel()
342+
}()
343+
<-c
344+
setupLog.Info("Second signal received, killing now")
345+
os.Exit(1) // second signal. Exit directly.
346+
}()
347+
325348
go func() {
326349
setupErr <- setupControllers(ctx, mgr, tracker, setupFinished)
327350
}()

0 commit comments

Comments
 (0)