diff --git a/changelog/unreleased/1001-port-pools.md b/changelog/unreleased/1001-port-pools.md new file mode 100644 index 000000000..afc965f51 --- /dev/null +++ b/changelog/unreleased/1001-port-pools.md @@ -0,0 +1,12 @@ +# Port pools for servers behind one public address + +`operator.portPools` defines ranges of ports on a shared endpoint, such as a +relay or an edge that forwards ports, and the templates whose servers draw +from each. Every such server gets a contiguous block of Service ports of its +own, kept for its life, even when the pool later shrinks, and shown by the new +`PortAllocation` condition. Servers whose games send no hostname no longer +need their ports set by hand behind one address. A full pool never takes +ports from a server that has them: new servers wait, and template updates +that need more ports wait on the current version. See +[Port pools](../../docs/networking.md#port-pools). +[PR #1001](https://github.com/GameplanePanel/Gameplane/pull/1001) diff --git a/charts/gameplane/templates/operator.yaml b/charts/gameplane/templates/operator.yaml index 36c938669..eb61d308f 100644 --- a/charts/gameplane/templates/operator.yaml +++ b/charts/gameplane/templates/operator.yaml @@ -424,6 +424,13 @@ spec: {{- end }} - --address-manager={{ .Values.operator.addressManager }} - --metallb-namespace={{ .Values.operator.metalLBNamespace | default "metallb-system" }} + {{- range .Values.operator.portPools }} + - --port-pool={{ .name }}={{ .ports }} + {{- $pool := .name }} + {{- range .templates }} + - --port-pool-template={{ . }}={{ $pool }} + {{- end }} + {{- end }} - --zap-log-level={{ .Values.operator.logLevel }} {{- if .Values.operator.localModules.enabled }} - --module-local-root={{ .Values.operator.localModules.mountPath }} diff --git a/charts/gameplane/values.yaml b/charts/gameplane/values.yaml index e9bac8ee5..7df4eeac3 100644 --- a/charts/gameplane/values.yaml +++ b/charts/gameplane/values.yaml @@ -156,6 +156,21 @@ operator: # MetalLB event. "metallb-system" is MetalLB's own install convention, not # a contract, hence configurable. metalLBNamespace: metallb-system + # Port pools: ranges of Service ports on one shared public endpoint, such + # as a relay or an edge that forwards ports to the cluster, for games whose + # clients send no hostname and so need ports of their own behind one + # address. Every server of a listed template gets a contiguous block of the + # pool, one port per advertised port, recorded in its + # gameplane.local/pool-ports annotation and kept for the server's life. A + # (protocol, port) pair goes to one server, whatever its template. Servers' + # explicit servicePort overrides are reserved too, and refused when another + # server holds the port. Shrinking a range leaves existing servers their + # ports; keep forwarding them. A template draws from at most one pool. See docs/networking.md#port-pools. Example: + # portPools: + # - name: edge + # ports: "2456-2499" + # templates: [valheim, factorio] + portPools: [] # Tunnel provider images: relay binaries wrapped in OCI images for reaching # players through relay services when the cluster has no public IP or port- # forwarding. Each wraps a third-party relay binary (frpc, tailscaled, playit) diff --git a/deploy/kind/e2e.sh b/deploy/kind/e2e.sh index 0cc72d89c..3edff743e 100755 --- a/deploy/kind/e2e.sh +++ b/deploy/kind/e2e.sh @@ -416,6 +416,7 @@ EOF # CI or dev installs reach the project provider (spec 022 R17). # The legacy fixtures exercise Hostport, in-pod capture and root init. # The restricted-workloads bucket upgrades this same installation to PSA. + # The port pool serves TestGameServer_PortPool* (port_pool_e2e_test.go). helm upgrade --install gameplane "${CHART_DIR}" \ --namespace gameplane-system --create-namespace \ --set "image.registry=gameplane-test" \ @@ -435,6 +436,13 @@ EOF --set "api.oidc.roleMappings.admin[0]=gameplane-e2e-oidc-admins" \ --set "operator.leaderElect=false" \ --set "operator.addressManager=metallb" \ + --set "operator.portPools[0].name=e2e-pool" \ + --set "operator.portPools[0].ports=31000-31005" \ + --set "operator.portPools[0].templates[0]=e2e-portpool-a" \ + --set "operator.portPools[0].templates[1]=e2e-portpool-b" \ + --set "operator.portPools[1].name=e2e-pool-serving" \ + --set "operator.portPools[1].ports=31010-31012" \ + --set "operator.portPools[1].templates[0]=e2e-portpool-c" \ --set "operator.gameDataStorage.storageClassName=gameplane-e2e-install-default" \ --set "defaultModuleSource.enabled=false" \ --set "api.telemetry.receiver.enabled=true" \ diff --git a/docs/install.md b/docs/install.md index 03b3a1e8d..01236330e 100644 --- a/docs/install.md +++ b/docs/install.md @@ -409,6 +409,12 @@ Top-level knobs (see `values.yaml` for the full list): Any other value fails the operator at startup. The operator never writes the deprecated `service.spec.loadBalancerIP`. +- `operator.portPools` — port pools: ranges of ports on one shared public + endpoint, each with the templates whose servers draw from it + (`[{name: edge, ports: "2456-2499", templates: [valheim, factorio]}]`). + Each server gets a contiguous block, kept for its life. Use it when servers + are reached through a relay or an edge that forwards ports, as games whose + clients send no hostname must be. See [Networking](networking.md#port-pools). - `capture.enabled` — the optional network packet capture sidecar [optional] for GameServers (default `false`). When enabled cluster-wide, admins can opt individual GameServers into live AF_PACKET capture with BPF filtering and download PCAPNG files. Captures diff --git a/docs/networking.md b/docs/networking.md index e60750523..7cd5ae5b2 100644 --- a/docs/networking.md +++ b/docs/networking.md @@ -327,6 +327,64 @@ configured address manager; a Service of another class is outside that manager's control, so leave them unset with a class of another implementation. +## Port Pools + +Some installs reach every game server through one public address, a relay +or an edge that forwards a range of ports to the cluster's load balancer. +Games whose clients send the hostname they connect to (Minecraft Java) can +share a port behind a router that picks the server by name, but most games +send no hostname, so behind one address each server needs ports of its own. +A port pool hands them out. + +A pool is a range of ports on one shared endpoint. Name it for the endpoint, +and list the templates whose servers draw from it, with the Helm value +`operator.portPools`: + +```yaml +operator: + portPools: + - name: edge + ports: "2456-2499" + templates: [valheim, factorio] +``` + +Every server of those templates then gets a contiguous block of the pool, one +port per advertised port, in template order: the first Valheim server +2456-2457, a Factorio server next 2458, the next Valheim server 2459-2460. +The pool decides who shares: a port goes to one server whatever its +template, and ports are reserved per protocol, so TCP 2456 and UDP 2456 are +two ports. A template draws from at most one pool. + +The operator records each server's ports in its `gameplane.local/pool-ports` +annotation and keeps them for the server's life, so players' saved addresses +keep working. The Service publishes those ports and forwards them to the +game's own, and `status.endpoints` and the dashboard show them. Relays (frp, +Tailscale, Playit) forward to those ports too. The `PortAllocation` +condition lists them. + +A server's explicit `spec.networking.portOverrides[].servicePort` is +reserved too, so the pool never hands it to another server. An override that +names a port another server already holds is refused: the server reports +`PortAllocation` `ServicePortConflict` and keeps the ports it had. + +When no block is free, a new server reports `PoolExhausted` and gets no +Service or address until a block frees up, for example when a server is deleted. A +server that already has ports keeps them, and keeps working, sleeping and +waking, while a change it needs waits (`ChangePending`). A template update +that needs other ports reserves them before the server is restarted for it; +when the pool can't, the server stays on its current version and reports +`RolloutPending`. Servers that wait check again every 30 seconds, and their +games keep running. + +Forward the whole range at the relay or edge, to the cluster's load +balancer. When you shrink a pool, existing servers keep their ports, even +ones outside the new range, and report `AllocatedOutsideRange` with those +ports. Keep forwarding them until you move those servers. New servers, and +servers that need new ports, for example for a template update, are given +ports from the smaller range. Removing a template from its pool returns its servers +to the template's ports. The full rules, including template revisions and +recovery, are in [spec 036](../specs/036-port-pools/spec.md). + ## Related - [`install.md`](install.md) — Helm chart values for `operator.addressManager` and cluster prerequisites. diff --git a/i18n/locales/en/app.json b/i18n/locales/en/app.json index 25e857847..ee8dfd872 100644 --- a/i18n/locales/en/app.json +++ b/i18n/locales/en/app.json @@ -379,6 +379,38 @@ "defaultMessage": "Module translations could not be stored.", "description": "LocalesValid condition on a GameTemplate when the translations ConfigMap could not be written. The message holds the English error." }, + "condition.portallocation.allocated": { + "defaultMessage": "Ports {ports} assigned from port pool ''{pool}''.", + "description": "PortAllocation condition once a server has Service ports from its port pool. ports lists them as port/protocol." + }, + "condition.portallocation.allocatedoutsiderange": { + "defaultMessage": "Ports {ports} assigned from port pool ''{pool}''. Ports {outside} are outside the pool's range and stay in use until the server is moved; keep forwarding them.", + "description": "PortAllocation condition when the pool shrank and the server keeps ports outside the new range. outside lists them as port/protocol." + }, + "condition.portallocation.changepending": { + "defaultMessage": "Port pool ''{pool}'' has no free block of {count} ports for the requested change, so the server keeps ports {ports} until one frees up.", + "description": "PortAllocation condition when a change to the server's ports waits for pool capacity. count is how many pooled ports the change needs." + }, + "condition.portallocation.poolexhausted": { + "defaultMessage": "Port pool ''{pool}'' has no free block of {count} ports for this server, so it has no Service until a block frees up.", + "description": "PortAllocation condition when the server's port pool is full. count is how many ports the server needs." + }, + "condition.portallocation.rolloutpending": { + "defaultMessage": "The template update waits for {count} free ports in port pool ''{pool}''; the server keeps its current revision on ports {ports}.", + "description": "PortAllocation condition when a template revision needs other ports and the pool can't reserve them yet." + }, + "condition.portallocation.rolloutportconflict": { + "defaultMessage": "The template update waits for Service port {port}, held by {holder} in port pool ''{pool}''; the server keeps its current revision on ports {ports}.", + "description": "PortAllocation condition when a template revision's servicePort override names a port another server holds. holder is that server as namespace/name." + }, + "condition.portallocation.serviceportconflict": { + "defaultMessage": "Service port {port} is held by {holder} in port pool ''{pool}'', so the server keeps ports {ports} until it is free.", + "description": "PortAllocation condition when a servicePort override names a port another server holds. holder is that server as namespace/name; ports are the ones the server keeps." + }, + "condition.portallocation.serviceportconflictunpublished": { + "defaultMessage": "Service port {port} is held by {holder} in port pool ''{pool}'', so the server has no Service until it is free.", + "description": "PortAllocation condition when a servicePort override names a port another server holds and the server has no ports yet. holder is that server as namespace/name." + }, "condition.progressing.containercreating": { "defaultMessage": "Creating the container.", "description": "Progressing detail while the kubelet creates the container." diff --git a/operator/api/v1alpha1/gameserver_types.go b/operator/api/v1alpha1/gameserver_types.go index 184b13f26..030669917 100644 --- a/operator/api/v1alpha1/gameserver_types.go +++ b/operator/api/v1alpha1/gameserver_types.go @@ -35,6 +35,13 @@ const ( // volume (e.g. a permission error on a subdirectory it doesn't own). // Absent once a wipe has succeeded and been acked. GameServerConditionDataWipe = "DataWipe" + + // GameServerConditionPortAllocation reports the Service ports a server + // holds in its template's port pool (operator --port-pool): True with + // Reason "Allocated", or False with Reason "PoolExhausted" when the pool + // has no free block, in which case the server has no Service. Absent + // when the template draws from no pool. + GameServerConditionPortAllocation = "PortAllocation" ) // GameServerSpec is the desired state of a single game server instance. diff --git a/operator/cmd/main.go b/operator/cmd/main.go index 3be6f4deb..c6bd1ee8f 100644 --- a/operator/cmd/main.go +++ b/operator/cmd/main.go @@ -151,6 +151,8 @@ func main() { gameIngressFromCIDR cidrListFlag probeIngressPolicy bool probeIngressFromCIDR cidrListFlag + portPoolFlags cidrListFlag + portPoolTemplateFlags cidrListFlag gameDataStorageClass string captureEnabled bool restrictedWorkloads bool @@ -304,6 +306,12 @@ func main() { flag.BoolVar(&probeIngressPolicy, "probe-ingress-policy", false, "Reconcile a per-GameServer NetworkPolicy admitting kubelet probes to the game container's "+ "probe ports only. When false, the operator ensures the policy is absent.") + flag.Var(&portPoolFlags, "port-pool", + "A port pool, =-: a range of Service ports on one shared endpoint, such as a "+ + "relay or port-forwarding edge, that its servers share without collisions. Repeatable.") + flag.Var(&portPoolTemplateFlags, "port-pool-template", + "