(go/v4): replace ENABLE_WEBHOOKS with --webhook-port=-1 to run the manager without webhooks
What
The go/v4 scaffold wraps every webhook setup in cmd/main.go in an environment
variable guard:
// nolint:goconst
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
if err := webhookv1.SetupCronJobWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "Failed to create webhook", "webhook", "CronJob")
os.Exit(1)
}
}The book tells users to run export ENABLE_WEBHOOKS=false and make run to test
a controller locally without webhook certificates.
Since #6031 and #6034, the project has a second switch for the same thing. With
controller-runtime v0.25.0 or later, a negative --webhook-port turns the webhook
server off. The scaffolded flag help says "Set -1 to disable the webhook server",
and the Helm chart passes --webhook-port=-1 when webhook.enabled=false.
Two switches for one job is confusing, and the env var only works while a project
keeps the scaffolded guard. The flag works in controller-runtime itself, whatever
the project does in cmd/main.go.
Proof of concept
A project scaffolded from master with an API and defaulting and validation webhooks, run against an envtest API server (Kubernetes 1.37) with no webhook certificates:
Guard in cmd/main.go |
Command | Manager | /readyz |
Webhook logs |
|---|---|---|---|---|
| kept (today) | make run |
exits 2 | none | Starting webhook server, then open .../serving-certs/tls.crt: no such file |
| kept | ENABLE_WEBHOOKS=false make run |
runs | 200 | none (webhooks not registered) |
| kept | go run ./cmd/main.go --webhook-port=-1 |
runs | 200 | Webhook is disabled for both paths, Webhook server is disabled |
| kept | make run ARGS=--webhook-port=-1 |
exits 2 | none | same certificate error; ARGS is ignored |
| dropped | make run |
exits 2 | none | same certificate error |
| dropped | ENABLE_WEBHOOKS=false make run |
exits 2 | none | same certificate error; the env var does nothing now |
| dropped | go run ./cmd/main.go --webhook-port=-1 |
runs | 200 | Webhook is disabled twice, Webhook server is disabled |
| dropped | go run ./cmd/main.go -webhook-port=-1 |
runs | 200 | same as above |
So --webhook-port=-1 fully replaces the guard at runtime: the webhooks register
as disabled, the server never starts, and the controllers and readiness work.
The one gap is make run. Its recipe is go run ./cmd/main.go with no way to
pass arguments, so dropping the guard alone leaves no make command that runs a
project with webhooks locally without certificates.
Proposal
Let
make runpass arguments. In the go/v4 Makefile template:run: manifests generate fmt vet ## Run a controller from your host. go run ./cmd/main.go $(RUN_ARGS)Users then run:
make run RUN_ARGS=--webhook-port=-1Drop the
ENABLE_WEBHOOKSguard from the webhook setup code the go/v4 plugin writes intocmd/main.go, along with its// nolint:goconstline. The setup is always called;--webhook-port=-1keeps the server off.Update the docs:
docs/book/src/cronjob-tutorial/running.md: replaceexport ENABLE_WEBHOOKS=false/make runwithmake run RUN_ARGS=--webhook-port=-1.hack/docs/internal/cronjob-tutorial/main_revisited.go: rewrite the comment above the webhook setup to point at the flag instead of the env var.hack/docs/internal/multiversion-tutorial/generate_multiversion.go: update the text it finds and replaces, or the book generation fails.docs/book/src/reference/webhook-overview.md: replace theENABLE_WEBHOOKS=falseline under "Disabling the webhook server".
Source: kubernetes-sigs/kubebuilder