Skip to content

Commit

Permalink
Allow to configure delay before controller exits (#8143)
Browse files Browse the repository at this point in the history
* Allow to configure delay before controller exits

Signed-off-by: Aditya Kamath <[email protected]>

* Address comments

Signed-off-by: Aditya Kamath <[email protected]>
  • Loading branch information
theunrealgeek authored Jan 17, 2022
1 parent 4badf20 commit 2aa3420
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 6 deletions.
3 changes: 3 additions & 0 deletions cmd/nginx/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ Takes the form "<host>: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`)
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions cmd/nginx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/nginx/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions docs/user-guide/cli-arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
3 changes: 2 additions & 1 deletion internal/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 2aa3420

Please sign in to comment.