Run the preStop hooks without a shell
What does this MR do?
Every preStop hook in the chart runs through /bin/bash -c. On an image without bash the hook fails: kubelet records FailedPreStopHook and sends TERM immediately, which is the same as having no hook. The hardened Nix images (https://gitlab.com/gitlab-org/software-supply-chain-security/hardened-builds) ship no shell, so webservice and workhorse lose their drain there, and rollouts return user-visible errors that the Debian images don't.
This MR keeps the shutdown behaviour of the current hooks, without a shell:
| Container | Before | After |
|---|---|---|
webservice |
pkill -SIGINT -o ruby |
ruby -e 'Process.kill("INT", 1)': PID 1 is tini, which forwards INT to the puma master, or puma itself with USE_TINI=0 |
gitlab-workhorse (listener off) |
sleep $SHUTDOWN_BLACKOUT_SECONDS |
native preStop.sleep, shutdown.blackoutSeconds |
registry |
sleep 5 |
native preStop.sleep, 5 s |
sidekiq, mailroom, gitlab-exporter |
pkill -f <name> (TERM) |
no hook; kubelet sends TERM itself |
geo-logcursor |
pkill -SIGQUIT -f geo_log_cursor |
no hook; kubelet sends TERM itself |
geo-logcursor is different from the other three: its hook sends SIGQUIT, but Gitlab::Geo::LogCursor::Daemon#trap_signals traps only TERM and INT. Neither signal gives a graceful stop today. QUIT is untrapped, and TERM calls quit!, whose warn raises ThreadError in trap context because the warning gem takes a mutex (gitlab-org/gitlab#381482). Either way the daemon exits wherever it was. Dropping the hook removes the FailedPreStopHook on images without bash, and leaves kubelet's TERM, the signal the daemon is written for, so a fix to quit! makes the shutdown graceful with no chart change.
Notes:
pkill -o rubytargets the oldestrubyprocess. That is the puma master on the Debian image, but not wherever another Ruby process starts first. On the Nix image the oldest is the metrics server, and the puma master's process name isbundle.- The mailroom pattern
-f mail_roomalso matches PID 1, since tini's command line containsmail_room. - The native
sleepaction is on by default since Kubernetes 1.30 and GA in 1.34. On a 1.29 cluster the API server rejects it andhelm upgradefails. That is below the supported releases. The 10.6.0 upgrade notes call it out.
Relates to gitlab-org/software-supply-chain-security/hardened-builds#24 (closed), gitlab-org/software-supply-chain-security/hardened-builds#68 and gitlab-org/build/CNG#2120.
How was this tested?
Each variant was tested on a kind cluster and again on GKE, with both the Nix images and the Debian images from the same CNG pipeline. Settings were like GitLab.com's: 2 webservice pods, blackoutSeconds: 10, a 65 s grace period, and sidekiq timeout: 25 with a 30 s grace period. Load was constant through the ingress and the Service, with slow requests and sidekiq jobs in flight. Each variant got 3 pod deletions and 1 rollout restart. Full results: gitlab-org/software-supply-chain-security/hardened-builds#24 (comment 4104130362).
kind
User-facing errors per pod deletion and on the rollout restart:
| Hook | Nix | Debian |
|---|---|---|
webservice, current (pkill) |
1 to 2 / 21 (hook fails) | 0 / 0 |
webservice, this MR (ruby -e) |
0 / 0 | 0 / 0 |
workhorse, current (sleep via bash) |
0 / 31 (hook fails) | 0 / 0 |
| workhorse, this MR (native sleep) | 0 / 0 | 0 / 0 |
- The first version of the
ruby -ehook looked up the puma master in /proc. It closed puma at about 13 s, the same as the current hook on Debian. After review it was replaced with a direct INT to PID 1. That hook was re-run on GKE with the same settings, measuring from puma receiving INT to the first refused connection on its port 8080. Nix gave 11.01, 11.02 and 11.03 s over 3 deletions. Debian gave 11.02 s with the new hook and 11.04 s with the current hook. PID 1 wastiniin both, with the puma master as its only puma child, and there was noFailedPreStopHook. - Registry: native sleep delays TERM by 5 s, the same as the current hook (Debian).
- Sidekiq: no job was lost with the current hook, with a working
pkill, or with no hook. All three gave the same timing on both image families. - Mailroom and gitlab-exporter: each exits 0 within about 1 s of kubelet's TERM, with or without the hook (Debian).
GKE
GKE 1.36 with Dataplane V2, 3 nodes across zones, load also sent from outside the cluster through the Envoy Gateway's GCP L4 load balancer. Same settings and images as on kind. The hooks this MR renders were applied to the running Deployments byte for byte, against the current chart's hooks as the baseline.
Errors through the Service per pod deletion, and on the rollout restart. Workhorse rows are with healthcheckListener off, the chart default, where the workhorse hook renders:
| Hook | Nix | Debian |
|---|---|---|
webservice, current (pkill) |
0 to 21 / 2 (hook fails, puma closes at about 1.5 s) | 0 / 0 |
webservice, this MR (ruby -e) |
0 / 0 | 0 / 0 |
workhorse, current (sleep via bash) |
14 to 37 / 19 (hook fails, workhorse closes at about 2 s) | 0 / 0 |
| workhorse, this MR (native sleep) | 0 / 0 | 0 / 0 |
- This MR's hooks close puma at 12 to 13 s and keep workhorse up until just before it, on both image families, the same as the current hooks on Debian (first version of the webservice hook; the PID 1 version is measured above).
- Registry: TERM arrives about 5 s after kubelet's Killing event with both the current hook and native sleep (Debian registry image in both runs).
- No
FailedPreStopHookwith this MR's hooks.
Registry and mailroom were also run on Nix images that carry the pending image fixes (gitlab-org/software-supply-chain-security/hardened-builds!271 (merged) for the registry scripts, gitlab-org/software-supply-chain-security/hardened-builds!273 (merged) for mailroom), 3 pod deletions per hook:
| Component | Current hook on Nix | This MR on Nix |
|---|---|---|
| registry | FailedPreStopHook, TERM at 0.5 to 0.6 s (the 5 s wait is lost) |
native sleep, TERM at 5.5 to 6.9 s |
| mailroom | FailedPreStopHook; exits cleanly about 1 s after kubelet's TERM |
no hook; same clean exit, no hook failure |
- The few errors outside the shutdown windows (cold starts during rollout restarts, and brief network drops on the test client) appear with the current and the new hooks alike, and were excluded.
On GitLab.com, HAProxy sends web and API traffic to one GCP internal load balancer per zone, which is the webservice deployment's own LoadBalancer Service (HAProxy health-checks that address, not individual pods). The zonal clusters run Dataplane V2, so a terminating pod leaves rotation through endpoint removal, the same mechanism as the Service path measured above. GitLab.com runs workhorse with healthcheckListener.enabled: true, so the workhorse hook is not rendered there; the webservice hook is the part of this MR that applies to GitLab.com. HAProxy retries and keepalives into the internal load balancer were not modelled.
Not covered:
- gitlab-exporter on Nix: the image does not start yet (gitlab-org/software-supply-chain-security/hardened-builds#55 (closed)). Dropping its hook does not depend on the image.
- geo-logcursor was run on kind only, as a Geo secondary with injected events, on an older Nix image built from HB d25abede. With the current hook every termination gave
FailedPreStopHook(/bin/bashmissing). There was no real Geo primary or replication, and no Debian comparison.
The webservice, registry, sidekiq, mailroom, gitlab-exporter and geo-logcursor specs pass. The new specs fail against the current templates.
Checklist
- Changelog trailers on the commits
- Documentation: 10.6.0 upgrade notice in doc/releases/10_0.md