diff --git a/cmd/nginx/flags.go b/cmd/nginx/flags.go index f9d6702fe7..f620690b55 100644 --- a/cmd/nginx/flags.go +++ b/cmd/nginx/flags.go @@ -197,6 +197,8 @@ Takes the form ":port". If not provided, no admission controller is starte statusUpdateInterval = flags.Int("status-update-interval", status.UpdateInterval, "Time interval in seconds in which the status should check if an update is required. Default is 60 seconds") shutdownGracePeriod = flags.Int("shutdown-grace-period", 0, "Seconds to wait after receiving the shutdown signal, before stopping the nginx process.") + + postShutdownGracePeriod = flags.Int("post-shutdown-grace-period", 10, "Seconds to wait after the nginx process has stopped before controller exits.") ) flags.StringVar(&nginx.MaxmindMirror, "maxmind-mirror", "", `Maxmind mirror url (example: http://geoip.local/databases`) @@ -321,6 +323,7 @@ https://blog.maxmind.com/2019/12/18/significant-changes-to-accessing-and-using-g PublishStatusAddress: *publishStatusAddress, UpdateStatusOnShutdown: *updateStatusOnShutdown, ShutdownGracePeriod: *shutdownGracePeriod, + PostShutdownGracePeriod: *postShutdownGracePeriod, UseNodeInternalIP: *useNodeInternalIP, SyncRateLimit: *syncRateLimit, HealthCheckHost: *healthzHost, diff --git a/cmd/nginx/main.go b/cmd/nginx/main.go index cbfca547ce..7293e6b100 100644 --- a/cmd/nginx/main.go +++ b/cmd/nginx/main.go @@ -155,14 +155,14 @@ func main() { go startHTTPServer(conf.HealthCheckHost, conf.ListenPorts.Health, mux) go ngx.Start() - handleSigterm(ngx, func(code int) { + handleSigterm(ngx, conf.PostShutdownGracePeriod, func(code int) { os.Exit(code) }) } type exiter func(code int) -func handleSigterm(ngx *controller.NGINXController, exit exiter) { +func handleSigterm(ngx *controller.NGINXController, delay int, exit exiter) { signalChan := make(chan os.Signal, 1) signal.Notify(signalChan, syscall.SIGTERM) <-signalChan @@ -174,8 +174,8 @@ func handleSigterm(ngx *controller.NGINXController, exit exiter) { exitCode = 1 } - klog.InfoS("Handled quit, awaiting Pod deletion") - time.Sleep(10 * time.Second) + klog.Infof("Handled quit, delaying controller exit for %d seconds", delay) + time.Sleep(time.Duration(delay) * time.Second) klog.InfoS("Exiting", "code", exitCode) exit(exitCode) diff --git a/cmd/nginx/main_test.go b/cmd/nginx/main_test.go index e6d24b301d..2a29953ad8 100644 --- a/cmd/nginx/main_test.go +++ b/cmd/nginx/main_test.go @@ -105,7 +105,7 @@ func TestHandleSigterm(t *testing.T) { ngx := controller.NewNGINXController(conf, nil) - go handleSigterm(ngx, func(code int) { + go handleSigterm(ngx, 10, func(code int) { if code != 1 { t.Errorf("Expected exit code 1 but %d received", code) } diff --git a/docs/user-guide/cli-arguments.md b/docs/user-guide/cli-arguments.md index bbf06b7202..b9cd0c5642 100644 --- a/docs/user-guide/cli-arguments.md +++ b/docs/user-guide/cli-arguments.md @@ -40,6 +40,7 @@ They are set in the container spec of the `ingress-nginx-controller` Deployment | `--maxmind-retries-count` | Number of attempts to download the GeoIP DB. (default 1) | | `--maxmind-license-key` | Maxmind license key to download GeoLite2 Databases. https://blog.maxmind.com/2019/12/18/significant-changes-to-accessing-and-using-geolite2-databases | | `--metrics-per-host` | Export metrics per-host (default true) | +| `--post-shutdown-grace-period` | Additional delay in seconds before controller container exits. (default 10) | | `--profiler-port` | Port to use for expose the ingress controller Go profiler when it is enabled. (default 10245) | | `--profiling` | Enable profiling via web interface host:port/debug/pprof/ (default true) | | `--publish-service` | Service fronting the Ingress controller. Takes the form "namespace/name". When used together with update-status, the controller mirrors the address of this service's endpoints to the load-balancer status of all Ingress objects it satisfies. | diff --git a/internal/ingress/controller/controller.go b/internal/ingress/controller/controller.go index a4ae4217c0..48a91b67b8 100644 --- a/internal/ingress/controller/controller.go +++ b/internal/ingress/controller/controller.go @@ -118,7 +118,8 @@ type Configuration struct { MonitorMaxBatchSize int - ShutdownGracePeriod int + PostShutdownGracePeriod int + ShutdownGracePeriod int } // GetPublishService returns the Service used to set the load-balancer status of Ingresses.