Skip to content

fix(agent): a route change is one Caddy configuration load (closes #438) - #566

Draft
samuelmbabhazi wants to merge 3 commits into
mainfrom
fix/438-one-caddy-load
Draft

samuelmbabhazi wants to merge 3 commits into
mainfrom
fix/438-one-caddy-load

Conversation

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator

What

A route change is now one write of the Shipkeep server to Caddy's admin API, so Caddy loads one configuration for it.

  • writeServer (agent/internal/caddy) is a new helper that reads /config/apps/http/servers/shipkeep with its ETag, lets a change rewrite the routes and automatic_https.skip, and writes the server back in one PATCH with If-Match.
    • When the configuration changed in between, Caddy answers 412, and the write is made again from a new read, at most 3 times.
    • The server's other fields, and the fields of routes this client does not model, are kept as Caddy returned them.
    • A change with nothing to do writes nothing.
  • ApplyRoute writes the route and its hosts without TLS in that single write. Before, it wrote the hosts to remove, the route, then the hosts to add: up to three writes, two of them changing the configuration. A refused write now leaves the previous configuration whole, so the old ordering argument for keeping hosts reachable is no longer needed.
  • RemoveRoute(ctx, id, plainRemove) takes the route's hosts out in the same write. The removal action no longer calls SetPlainHosts, which is gone from the Router interface and from the client.
  • EnsureControlPlaneRoutes (the repair of the control plane's routes on a single machine) is one write too: the list in its order, and the control plane's name taken off the skip list.
  • do goes through a new send, which carries If-Match and returns the ETag. do lost its out parameter, which every remaining caller passed as nil.
  • SPEC 3.2 describes the single write.

Why

#438 (found in #437): each configuration load makes Caddy's previous HTTP server close its idle keep-alive connections. A client that sends a request on one of them at that instant gets a reset. Two loads 1 ms apart for one route change doubled those moments, and between the two writes the route and the skip list disagreed.

How it was tested

Against a real Caddy 2.11.9 (the image pinned in compose.yml, admin on a Unix socket), counting autosaved config lines in its log:

Step Loads
EnsureServer on an empty configuration 1
A route under automatic HTTPS 1
A route switched to TLS off, its host added to the skip list 1
The same route switched to HTTPS, its host removed 1
Back to TLS off 1
RemoveRoute with its host 1
RemoveRoute of a route already gone 0
EnsureControlPlaneRoutes 2: the first is EnsureServer securing a hand-written route without the Host bound (PATCH /id/…/match), the second the repair's single write
EnsureServer and the repair again 0

The same Caddy answered 412 (If-Match header did not match current config hash) to a write with a stale ETag. That lab test was not committed.

Unit tests (caddy_test.go, with a fake admin that serves the server path with an ETag and checks If-Match):

  • a switch and a removal are exactly one PATCH /config/apps/http/servers/shipkeep, keep the other route's place and the server's other fields;
  • a write refused once with 412 succeeds on the next read, and one refused every time fails after 3 attempts without changing anything;
  • a refused write changes nothing;
  • the securing of old routes, and the control plane route repair, wait while a route write is held under the lock;
  • the repair is one write.

go test -race -count=20 ./agent/internal/caddy/, the agent's other packages and golangci-lint (with and without the integration tag) pass.

Real Caddy test in CI: TestRealCaddySwitchesUnderLoadWithoutAFailedRequest now reads Caddy's log (SHIPKEEP_IT_CADDY_CONTAINER, sk-it-caddy by default). It checks one load for the first route without TLS, one for the switch to v2 under load, and one for the route's removal. I could not run that test locally: it needs the Docker socket, which this machine's sandbox does not expose. The job "Agent against Docker and Caddy" runs it.

Security considerations

  • Inputs: no new one. The client writes only the Shipkeep server, at the same admin socket.
  • Concurrent writes: If-Match keeps a write from replacing a configuration it did not read, such as one changed by another writer between the read and the write.
  • Errors: Caddy's error text is still not passed on: status codes only.

Closes #438.

ApplyRoute wrote the route, then the hosts without TLS, and RemoveRoute deleted the route before the removal action took its hosts out: two loads for one change, and each load closes the previous configuration's idle connections, so a client sending on one at that instant gets a reset (#437). Both now change the route and automatic_https.skip in one PATCH of the Shipkeep server, under the read's ETag (If-Match), made again from a new read when Caddy answers 412, at most 3 times. The control plane route repair is one write too. The real Caddy test counts the loads in Caddy's log.

Signed-off-by: samuel mbabhazi <[email protected]>
@coderabbitai

coderabbitai Bot commented Nov 28, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

…unts loads

CI starts Caddy empty: the first route action created the Shipkeep server, a load of its own that the agent makes once at start, then wrote the route. Part of #438.

Signed-off-by: samuel mbabhazi <[email protected]>
…load

Docker stamps a log line when it reads it, a few milliseconds after Caddy wrote it: the load that created the server could land after the time the count started from. The test now waits until that load is in the log before it starts counting. Part of #438.

Signed-off-by: samuel mbabhazi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Apply a route switch in one Caddy configuration load

1 participant