Skip to content

test(agent): accept container actions and routes on real Docker and Caddy (#16, #17) - #424

Merged
samuelmbabhazi merged 3 commits into
mainfrom
test/16-17-real-docker-caddy
Nov 23, 2026
Merged

samuelmbabhazi merged 3 commits into
mainfrom
test/16-17-real-docker-caddy

Conversation

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator

Closes #16 and closes #17: the acceptance of M1.4 and M1.5, which the plan allows in CI against a real Docker and Caddy. It also fixes a production bug that the first of these tests found.

The bug

An HTTP health check probed the container's single exposed TCP port, and failed when there were several. The image's own EXPOSE counts. So any image that exposes another port than the application's failed its health check, and with it the deployment:

  • traefik/whoami exposes 80 while the application listens on 8080;
  • gitea exposes 22 and 3000, minio 9000 and 9001, keycloak 8080 and 8443.

The lab never hit it: its applications listen on the port their image exposes.

Fix (agent only, no contract change):

  • When it creates a container, the agent records the one TCP port the spec declares, in its own label sk.probe_port, and the HTTP probe uses it.
  • Labels from the control plane cannot start with sk..
  • A container created before keeps the previous rule.
  • The spec fingerprint is computed from the spec, so idempotent creation is unchanged.
  • The SPEC's health check section says so.

The acceptance tests

They sit behind the integration build tag and are run by a new CI job, "Agent against Docker and Caddy".

real_docker_test.go (#16): every container action through the agent's own handlers and Engine API client, against the runner's Docker.

  • The actions: network, pull and pull again, volume, release command (exit codes 0 and 1 reported), create and create again (same container), start, health check, stop and stop again (the intent is cleared), remove, then the volume removed with a confirmation.
  • Docker's own answer, read without the agent's client, shows:
    • not privileged, no bind, no host PID or user namespace;
    • no-new-privileges, read-only root, the user asked for;
    • memory, CPU and PID limits, private IPC, init;
    • only volume and tmpfs mounts.
  • A mount path with .. is refused and never reaches Docker.

real_caddy_test.go (#17): in the Caddy that compose.yml pins, v1 serves.

  • Under constant load from eight clients, the route switches to v2, drains, and v1 is stopped and removed, as the deployment pipeline does.
  • Result locally: 5,738 requests, none failed (1,677 answered by v1, then 4,061 by v2).
  • The same test with the switch left out fails with 674 "Bad Gateway". The load is generated by the test itself rather than k6 or vegeta, so no tool is added.

The CI job starts Caddy on the test network as the runner's user, so that the test reaches its admin socket. It reads the Caddy image from compose.yml, so the pin stays in one place.

golangci-lint now reads the integration tag, so these files are linted like the others.

PLAN: M1.4 and M1.5 are checked, and taken out of the list that waits for a server.

How it was tested

  • Both tests on the user's machine, against Docker 29 and the pinned Caddy:
    • they pass;
    • without the probe fix, the Docker test fails at the health check;
    • without the switch, the Caddy test fails.
  • Nothing is left behind (containers and volumes are removed in t.Cleanup).
  • go test ./agent/..., golangci-lint (0 issues) and actionlint pass.

Security considerations

  • No new action and no change to an action's input.
  • The probe still reaches only the container's private network address, now on the port its spec declared.
  • The new CI job runs public images pinned by digest.

An HTTP health probe took the container's single exposed TCP port and
failed when there were several. The image's own EXPOSE counts too, so
any image exposing another port than the application's (whoami exposes
80 while the application listens on 8080; gitea, minio and many others
expose several) failed its health check, and with it the deployment.

The agent now records the one TCP port the spec declares in the label
sk.probe_port when it creates the container, and the probe uses it.
Containers created before keep the previous rule. No contract change:
the label is the agent's own, and labels from the control plane cannot
start with "sk.".

Found by the acceptance test against a real Docker daemon (#16).

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

Two tests behind the integration build tag, run by a new CI job:

- Every container action through the agent's own handlers against the
  runner's Docker: network, pulls, volume, release command with its exit
  code, create (idempotent), start, health check, stop, remove, volume
  removal. Docker's own view is checked: not privileged, no bind, no
  host namespace, no-new-privileges, read-only root, user, limits, only
  volume and tmpfs mounts. A path traversal never reaches Docker.
- A route switched from v1 to v2 under constant load from eight clients
  in the Caddy that compose.yml pins, then the replaced replica stopped
  and removed: no failed request. Without the switch, the same test
  fails with 502s.

golangci-lint now reads the integration tag. Closes #16, closes #17.

Signed-off-by: samuel mbabhazi <[email protected]>
@samuelmbabhazi samuelmbabhazi added track:agent v0.1-stable In the scope of the first stable version (docs/PLAN.md) labels Nov 23, 2026
@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 40 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 50fc17f and 1870887.

Files selected for processing: 10

  • .github/workflows/ci.yml
  • .golangci.yml
  • agent/internal/actions/container_spec.go
  • agent/internal/actions/container_test.go
  • agent/internal/actions/health.go
  • agent/internal/actions/health_test.go
  • agent/internal/actions/real_caddy_test.go
  • agent/internal/actions/real_docker_test.go
  • docs/PLAN.md
  • docs/SPEC.md
  • 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

@samuelmbabhazi
samuelmbabhazi merged commit f2cd838 into main Nov 23, 2026
25 checks passed
@samuelmbabhazi
samuelmbabhazi deleted the test/16-17-real-docker-caddy branch November 23, 2026 10:50
samuelmbabhazi added a commit that referenced this pull request Nov 23, 2026
…art of #18) (#426)

Part of #18 (M1.6, self-reconciliation). It reuses the CI job and the
real-Docker helpers of #424, now merged.

## The defect

Caddy holds a route's upstreams as resolved addresses (ADR 0017), and
Docker gives a restarted container whatever address is free. If another
container starts while the first is down, it takes the old address. The
route then sends the first application's traffic **to the second**: one
application's requests are answered by another on the same environment
network.

It happens after a crash, an out-of-memory kill, a disk budget stop or a
reboot, and it does not need the control plane to be down. I reproduced
it on Docker 29:
1. container A had `.2`;
2. A was killed, and B started and got `.2`;
3. A restarted with `.3`.

ADR 0017 had left "re-applying routes during reconciliation" to M1.6.

## The fix (agent only, no contract change)

**The journal** (`route_records`) keeps one record per route that
proxies to local containers:
- the route's upstreams as asked: a container and its port, or an
address, each with its weight;
- the dial lists the agent wrote to Caddy, or is about to write: one or
two.

**The route actions:**
- `UpsertRoute` writes the record *before* Caddy gets the route.
- The maintenance page and address-only routes have no record.
- `RemoveRoute` forgets the record only once Caddy has removed the
route.

**`actions.RouteRefresher`** runs at the end of every reconciliation
pass (every 30 s, with or without the control plane), after containers
are started again. For each recorded route:
- It resolves every container again, and builds the whole upstream list
in the route's order, with each upstream's weight. Replicas that swapped
addresses keep their weights.
- **A container that does not run gets no traffic**, since its old
address may be another container's now. A route none of whose containers
runs answers **503** until one runs again (`caddy.UnavailableHandler`).
- It changes the route only while Caddy holds a dial list the agent
recorded. A route written by anything else is left alone.
- It records the new list before writing it to Caddy, and keeps only
that list once Caddy has it. Whichever write fails, the next pass takes
it up again.
- If Docker does not answer, or two upstreams would share an address, it
changes nothing and reports the error.

**One lock** (`actions.RouteBook`, which the agent creates once) is
shared by the route actions and the refresher. A route's write to Caddy
and the write of its record happen under it together.

**`caddy.Client.UpdateRoute`** reads and rewrites one route under the
Caddy client's lock.

**Docs:**
- SPEC (self-reconciliation);
- ADR 0017 (consequences);
- THREAT_MODEL T5, which also states what remains: when Docker restarts
a container itself, its route keeps the old address until the next pass;
- PLAN M1.6.

## How it was tested

**Unit tests:**
- the journal record: written, replaced, read, forgotten, and invalid
input refused;
- the record written before Caddy gets the route, cleared for the
maintenance page and on removal, and kept when Caddy fails to remove the
route;
- a route Caddy refused is not completed by the refresh;
- the refresh:
  - follows a container that moved;
  - leaves a route the agent did not write;
  - follows replicas that swapped addresses with their weights;
- sends nothing to a stopped container, answers 503 with none running,
and proxies again once one runs;
  - changes nothing without Docker or with a duplicate address;
  - recovers from a failure at each of its three writes;
- every route write, by the refresh and by both actions, happens under
the shared lock.

The lock, removal and write-ahead tests fail when those protections are
taken out again.

**Real Docker and Caddy** (`TestRealCaddyFollowsARestartedContainer`, in
the CI job of #424), with no control plane:
1. A serves, then A is stopped behind the agent's back. B starts and
takes A's address, and A's route now reaches B: the defect itself.
2. The refresh: the route answers 503 instead of B.
3. One reconciliation pass: the reconciler has started A, and the route
reaches A again.

**Locally:** the three real tests pass against Caddy 2.11.9 and the
host's Docker. The switch test ran 8,878 requests with no failure.

**Checks:** `go test ./agent/... ./lib/...`, `go vet -tags integration`
and `golangci-lint` (0 issues).

What is left for #18 is the acceptance of a whole machine reboot on a
VM.

## Security considerations

- This closes a path where an application could receive another
application's requests after a restart, including while the first
container stays down.
- The refresh only ever points a route at the private addresses of the
managed containers that route already named, resolved the way
`UpsertRoute` resolves them (ADR 0017), and at the addresses it was
given. It never adds an upstream. The 503 answer is a fixed static
response built into the agent.
- An absent or unmanaged container under a recorded name gets no
traffic.


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **New Features**
* Routes to managed containers now refresh automatically when containers
restart with different addresses, helping traffic continue reaching
active instances.
* When no managed containers are running for a route, requests receive a
503 response with a retry interval.
* **Bug Fixes**
* Route updates now avoid overwriting changes made outside the agent,
and route records are kept in sync with successful changes.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

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

track:agent v0.1-stable In the scope of the first stable version (docs/PLAN.md)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[M1.5] Caddy route management [M1.4] Container actions

1 participant