Skip to content

Commit 67d2e47

Browse files
Benjamin Ritterl0wl3vel
Benjamin Ritter
authored andcommitted
Add termination grace period
Signed-off-by: Benjamin Ritter <[email protected]>
1 parent bba6342 commit 67d2e47

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

main.go

+24-1
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"
@@ -318,9 +320,30 @@ func innerMain() int {
318320
}
319321
}
320322

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

0 commit comments

Comments
 (0)