Skip to content

Support changing the WireGuard listen port via uc machine update - #430

Open
dasunsrule32 wants to merge 4 commits into
psviderski:mainfrom
dasunsrule32:feat/wg-port-update
Open

Support changing the WireGuard listen port via uc machine update#430
dasunsrule32 wants to merge 4 commits into
psviderski:mainfrom
dasunsrule32:feat/wg-port-update

Conversation

@dasunsrule32

@dasunsrule32 dasunsrule32 commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds a --wg-port flag to uc machine update so an operator can change a running machine's WireGuard listen port without removing and re-adding it to the cluster. The change is applied live, with no daemon restart required.

Motivation

Previously the WireGuard listen port could only be set at machine init/add time (via the earlier --wg-port work in #366). There was no way to change it afterward short of re-provisioning the machine.

What's included

  • --wg-port <port> on uc machine update, with validation (1–65535).
  • -y/--yes to skip the confirmation prompt (with UNCLOUD_AUTO_CONFIRM env binding), consistent with init/add/rm.
  • Layered endpoint handling:
    • --wg-port alone: advertised endpoints using the old port are auto-adjusted to the new port.
    • --wg-port + --wg-endpoint: explicit endpoints win; bare IPs default to the new port.
    • --wg-endpoint alone: unchanged existing behavior.
  • Safe apply ordering: the new endpoint is advertised in the cluster store first (while still reachable on the old port), gossip is given time to propagate, and only then is the WireGuard socket rebound. This runs asynchronously so the RPC response returns before the socket moves, preventing a self-partition and a CLI hang.
  • Concurrency guard: rejects a second --wg-port change while one is already in progress on the same machine, avoiding overlapping reconfigurations.
  • Adds wireguard_port to UpdateMachineRequest (regenerated protobuf) and refreshes the CLI reference docs.

Examples

# Change the listen port; advertised endpoints on the old port are adjusted automatically.
uc machine update machine1 --wg-port 51821

# Change the port and set explicit endpoints (e.g. behind NAT / port forwarding).
uc machine update machine1 --wg-port 51821 --wg-endpoint 203.0.113.10:9821

Testing

Validated on a live 7-machine cluster:

  • Single port change applies in ~0.2s, endpoint auto-adjusts, socket rebinds, state persists, machine stays reachable, cluster reconverges to all-Up.
  • Combined --wg-port + --wg-endpoint override works.
  • Restoring to a prior port works.
  • Concurrency guard rejects a second in-flight port change.

Add --wg-port to `uc machine update` so an admin can change a running
machine's WireGuard listen port without re-adding it to the cluster.

- Add optional wireguard_port field to UpdateMachineRequest (regenerated pb)
- Add --wg-port (validated 1-65535) and -y/--yes flags to the update command
- Apply the port in applyMachineUpdate and auto-adjust advertised endpoints
  that used the old port; explicit --wg-endpoint values take precedence
- Reconfigure the firewall and WireGuard interface live via a new
  clusterController.ReconfigureNetwork, avoiding a daemon restart
- Warn when no advertised endpoint uses the new port
- Regenerate CLI reference docs
certmagic is referenced by the caddystorage API, so `go mod tidy`
(Go 1.26.1) moves it from an indirect to a direct dependency. Pre-existing
on main and unrelated to the feature, but required for the go-tests CI gate.
Changing the listen port rebound the WireGuard socket before the new
endpoint reached the cluster store, so peers kept dialing the old port
and the machine partitioned itself (and the RPC response hung).

Reorder UpdateMachine's port-change path: sync the new endpoint to the
store first, wait for gossip to propagate, then rebind the socket. Run it
in a goroutine so the RPC returns immediately, and only on a port change
(other updates are unaffected). Bump the delay to 2s.
Advertise the new endpoint to the cluster store first, let gossip
propagate, then rebind the WireGuard socket asynchronously so the RPC
returns before the socket moves. Avoids partitioning the machine and the
CLI hang. Guard against concurrent port changes on the same machine.
@dasunsrule32 dasunsrule32 changed the title feat: support changing WireGuard listen port via machine update Support changing the WireGuard listen port via uc machine update Sep 11, 2026
@dasunsrule32
dasunsrule32 marked this pull request as ready for review September 11, 2026 20:42
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.

1 participant