Skip to content

Commit 46de743

Browse files
author
Benjamin Ritter
committed
WIP: Add termination grace period
Signed-off-by: Benjamin Ritter <[email protected]>
1 parent cd7201c commit 46de743

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

main.go

+23-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"
@@ -322,9 +324,29 @@ func innerMain() int {
322324
}
323325
}
324326

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

0 commit comments

Comments
 (0)