Repository navigation
perf(router): update network aliases in place instead of recreating on start, for #8132 (#8561) [skip ci] - #8561
Conversation
|
Download the artifacts for this pull request:
See Testing a PR. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
|
I'm annoyed that the router recreation thing resurfaced :) |
This comment was marked as off-topic.
This comment was marked as off-topic.
I forgot about this, reopening. |
a82c03c to
56451d1
Compare
|
This is a little confusing to sort through as the PR changed purpose, focus and code, would have been better as a new PR. But glad you sorted it out nicely. Trying to make sure I fully understand it. |
56451d1 to
3f645ed
Compare
893f36c to
7993bd0
Compare
|
I added a new test |
7993bd0 to
539ff3c
Compare
rfay
left a comment
There was a problem hiding this comment.
Thank you for this!
I looked through this carefully, thanks for the test. I did a bunch of manual testing and stepped through it. It seems perfect.
I pushed a trivial comment in networks_test.go:144 that said the router had to be restarted.
There is one possibililty claude caught that may be worth exploring if you think it's relevant:
Sticky failure after a failed reconnect (my main concern): if NetworkDisconnect succeeds but NetworkConnect fails, the router is left running but off the network, and every subsequent ddev start fails at the disconnect step ("not connected") because GetRouterNetworkAliases returns an empty list without error for a disconnected container. Tolerating "not connected" on disconnect, or falling back to full router recreation when the in-place update errors, would fix it.
|
I'm fine with this going in as-is, I don't think the risk Claude pointed out is significant, and there is adequate error-reporting. The path forward is what you see fit or have time for. |
…n start ## The Issue - Follow-up to ddev#8132, which fixed ddev#8110 PR ddev#8132 made the router recreate itself whenever project hostnames (network aliases) changed, so inter-project HTTP/S communication kept working. That fixed correctness, but it also meant that starting an additional project (`ddev start`) recreated the whole router even though its published ports were unchanged. Router recreation is disruptive and slower than necessary for the common "start another project" case. ## How This PR Solves The Issue Recreation is now reserved for the cases that actually require it, and hostname-only changes are handled without recreating the container: 1. **Added `UpdateContainerNetworkAliases()`** in `pkg/dockerutil/containers.go` - Docker only lets network aliases be set at connect time, so this disconnects and reconnects the router on the `ddev_default` network with the new alias set. No container recreation. 2. **Reworked the recreation decision** in `StartDdevRouter()` (`pkg/ddevapp/router.go`) - The router is recreated only when it is not running, its state can't be read, or its published ports changed (published ports can't be changed on a live container). A hostname-only change no longer triggers recreation. 3. **Update aliases in place when already running** - When the router is already running and only hostnames changed, the network aliases are refreshed in place via `UpdateContainerNetworkAliases()` before the Traefik config is pushed. The correctness guarantee from ddev#8132 is preserved (aliases are always current), while the common case of starting an extra project no longer pays for a full router recreation. ## Manual Testing Instructions ```bash # Clean slate ddev poweroff mkdir -p ~/tmp/t1 ~/tmp/t2 # First project starts (router is created) cd ~/tmp/t1 && ddev config --auto && ddev start printf '<?php\nprint "this is t1\n";' >index.php # Second project starts: router is NOT recreated, aliases updated in place cd ~/tmp/t2 && ddev config --auto && ddev start printf '<?php\nprint "BUT this is t2\n";' >index.php # Aliases include both projects docker inspect ddev-router --format '{{json .NetworkSettings.Networks.ddev_default.Aliases}}' | jq # Should show both t1.ddev.site and t2.ddev.site # Inter-project communication still works cd ~/tmp/t1 ddev exec curl -I https://t2.ddev.site # Router container id stays the same across the second start # (compare `docker inspect ddev-router --format '{{.Id}}'` before/after) ``` ## Automated Testing Overview Existing `TestGetRouterNetworkAliases()` (`pkg/dockerutil`) and `TestHostnamesMatch()` (`pkg/ddevapp`) continue to cover alias extraction and comparison. `TestNetworkAliases()` in `pkg/dockerutil/networks_test.go` exercises the disconnect/reconnect alias update path end-to-end. ## Release/Deployment Notes No user-facing configuration changes. Starting additional projects is faster because the router is no longer recreated for hostname-only changes; recreation still happens when published ports change or the router is not running. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
If NetworkConnect fails or is interrupted after NetworkDisconnect succeeds, the router keeps running but is left off ddev_default. GetRouterNetworkAliases then returns an empty list without error, so StartDdevRouter takes the in-place update path again and every later ddev start fails at the disconnect step with "is not connected to network ddev_default". UpdateContainerNetworkAliases now inspects the container and skips the disconnect when it isn't attached to the network, so the reconnect runs and repairs the state instead of erroring permanently. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
7fabd62 to
dd50b11
Compare
Thank you, I pushed one more commit for this. |
The Issue
PR #8132 made the router recreate itself whenever project hostnames (network aliases) changed so that inter-project HTTP/S communication continued to work correctly. That fixed the correctness issue, but it also meant that starting an additional project (
ddev start) recreated the router even though its published ports were unchanged. Router recreation is disruptive and slower than necessary for the common case of starting another project.How This PR Solves The Issue
This PR preserves the correctness guarantee introduced in #8132 while limiting router recreation to the cases where it is actually required. Hostname-only changes are now handled without recreating the router container.
A new
UpdateContainerNetworkAliases()function was added topkg/dockerutil/containers.go. Since Docker only allows network aliases to be assigned when a container connects to a network, this function updates the aliases by disconnecting and reconnecting the router to theddev_defaultnetwork with the new alias set, avoiding container recreation.The recreation logic in
StartDdevRouter()(pkg/ddevapp/router.go) was also reworked. The router is now recreated only when it is not running, its state cannot be determined, or its published ports have changed, since published ports cannot be modified on a running container. Changes to hostnames alone no longer trigger recreation.When the router is already running and only the hostname list has changed, its network aliases are updated in place using
UpdateContainerNetworkAliases()before the Traefik configuration is pushed.As a result, the correctness guarantee from #8132 is preserved because the aliases are always kept up to date, while the common case of starting an additional project no longer requires a full router recreation.
Manual Testing Instructions
Automated Testing Overview
New test
TestRouterNotRebuiltOnHostnameChange.Release/Deployment Notes
There are no user-facing configuration changes. Starting additional projects is now faster because the router is no longer recreated when only project hostnames change. The router is still recreated when its published ports change or when it is not running.