From 208f2e97d32f20c776dd5c149b4c97ca45e3b7c6 Mon Sep 17 00:00:00 2001 From: Fernando Marino Date: Fri, 6 Nov 2026 08:15:41 +0100 Subject: [PATCH 1/3] charts/agentmesh: routers announce their own address with several replicas; router.bootstrapTokenMaxUsages With router.replicaCount > 1 every router announced the shared Service name (/dns4/-router/tcp/4501). A node dials that name, the Service sends it to either pod, and the libp2p handshake fails with "peer id mismatch" because a router is identified by its peer id. Each router now announces its own pod IP (downward API) when replicaCount > 1 and neither router.externalAddrs nor router.hostPort is set; a single replica is unchanged. A router keeps no credential across restarts and enrolls again on every start, so the bootstrap token (one use per replica) is used up by the first restart and the restarted router crash-loops with "Bootstrap token max usages exceeded". New value router.bootstrapTokenMaxUsages (default 0 = unchanged, one use per replica) lets the operator cover the restarts they expect; the README also points to router.useOidcToken as the way to avoid the token. Tested on kind (v1.37) with two routers and a node: the node attaches to both, and a deleted router pod re-enrolls and rejoins. Fixes #881. Fixes #882. --- charts/agentmesh/README.md | 6 +++ charts/agentmesh/templates/bootstrap-job.yaml | 2 +- .../templates/router-statefulset.yaml | 15 +++++++- .../agentmesh/tests/bootstrap-job_test.yaml | 12 ++++++ .../tests/router-statefulset_test.yaml | 37 +++++++++++++++++++ 5 files changed, 70 insertions(+), 2 deletions(-) diff --git a/charts/agentmesh/README.md b/charts/agentmesh/README.md index a840ecacb..4e4173f55 100644 --- a/charts/agentmesh/README.md +++ b/charts/agentmesh/README.md @@ -168,3 +168,9 @@ Mesh size and state come from the control plane `agentmesh_control_plane_enrolled_nodes{role,state}`); the router adds its live view (`agentmesh_router_authenticated_peers`, `agentmesh_router_auth_handshakes_total{result}`) alongside libp2p's own relay and connection metrics. + +## Several routers (`router.replicaCount` > 1) + +Each router announces its own pod IP (`--external-addr=/ip4/$(POD_IP)/tcp/4501`), because a router is identified by its peer id and the shared `-router` Service would send a dial to whichever pod it likes (the handshake then fails with "peer id mismatch"). With one replica, or with `router.externalAddrs` / `router.hostPort` set, nothing changes. The pod IP changes when a pod restarts; the router announces the new one when it enrolls. + +A router keeps no credential across restarts, so it enrolls again every time it starts. With the default bootstrap token (one use per replica) a restarted router is refused. Set `router.bootstrapTokenMaxUsages` to cover the restarts you expect, or avoid the token: `router.useOidcToken=true` with the cluster's service-account issuer trusted by the control plane (see the workload issuer section above). diff --git a/charts/agentmesh/templates/bootstrap-job.yaml b/charts/agentmesh/templates/bootstrap-job.yaml index d4c87e05b..b1624f94b 100644 --- a/charts/agentmesh/templates/bootstrap-job.yaml +++ b/charts/agentmesh/templates/bootstrap-job.yaml @@ -136,7 +136,7 @@ spec: TOKEN_RESP=$(curl -sf -X POST \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${ADMIN_TOKEN}" \ - -d '{"role": "mesh:role:router", "max_usages": {{ .Values.router.replicaCount | int }}}' \ + -d '{"role": "mesh:role:router", "max_usages": {{ .Values.router.bootstrapTokenMaxUsages | default .Values.router.replicaCount | int }}}' \ "${CP_URL}/admin/bootstrap-tokens") ROUTER_TOKEN=$(echo "${TOKEN_RESP}" | sed -n 's/.*"token"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p') diff --git a/charts/agentmesh/templates/router-statefulset.yaml b/charts/agentmesh/templates/router-statefulset.yaml index 93c2df2c8..e88fad5eb 100644 --- a/charts/agentmesh/templates/router-statefulset.yaml +++ b/charts/agentmesh/templates/router-statefulset.yaml @@ -114,14 +114,22 @@ spec: periodSeconds: 15 resources: {{- toYaml .Values.router.resources | nindent 10 }} - {{- if .Values.router.hostPort }} + {{- if or .Values.router.hostPort (gt (int .Values.router.replicaCount) 1) }} env: + {{- if .Values.router.hostPort }} # kubelet expands $(HOST_IP) in the args below. - name: HOST_IP valueFrom: fieldRef: fieldPath: status.hostIP {{- end }} + {{- if gt (int .Values.router.replicaCount) 1 }} + - name: POD_IP + valueFrom: + fieldRef: + fieldPath: status.podIP + {{- end }} + {{- end }} args: - "--control-plane=http://{{ include "agentmesh.fullname" . }}-control-plane:{{ .Values.controlPlane.service.port }}" # In-cluster plaintext to the chart's own control-plane Service; the @@ -148,6 +156,11 @@ spec: {{- else if .Values.router.hostPort }} - "--external-addr=/ip4/$(HOST_IP)/tcp/{{ .Values.router.hostPort }}" - "--external-addr=/ip4/$(HOST_IP)/udp/{{ .Values.router.hostPort }}/quic-v1" + {{- else if gt (int .Values.router.replicaCount) 1 }} + # one address per pod: a router's identity is its peer id, and the shared Service name sends a dial to whichever pod + # it likes ("peer id mismatch"). (Per-pod DNS would need serviceName to be a headless Service, which a StatefulSet + # cannot change on upgrade.) The pod IP changes on restart; the router announces the new one when it enrolls. + - "--external-addr=/ip4/$(POD_IP)/tcp/4501" {{- else }} - "--external-addr=/dns4/{{ include "agentmesh.fullname" . }}-router/tcp/{{ .Values.router.service.port }}" {{- end }} diff --git a/charts/agentmesh/tests/bootstrap-job_test.yaml b/charts/agentmesh/tests/bootstrap-job_test.yaml index 0676e9935..974619571 100644 --- a/charts/agentmesh/tests/bootstrap-job_test.yaml +++ b/charts/agentmesh/tests/bootstrap-job_test.yaml @@ -128,3 +128,15 @@ tests: - notMatchRegex: path: spec.template.spec.containers[0].args[0] pattern: 'agentmesh-box' + + - it: router.bootstrapTokenMaxUsages overrides the one-use-per-replica default + set: + router.replicaCount: 2 + router.bootstrapTokenMaxUsages: 20 + documentSelector: + path: kind + value: Job + asserts: + - matchRegex: + path: spec.template.spec.containers[0].args[0] + pattern: '"role": "mesh:role:router", "max_usages": 20\}' diff --git a/charts/agentmesh/tests/router-statefulset_test.yaml b/charts/agentmesh/tests/router-statefulset_test.yaml index fa8f19628..8c1c96fbc 100644 --- a/charts/agentmesh/tests/router-statefulset_test.yaml +++ b/charts/agentmesh/tests/router-statefulset_test.yaml @@ -34,6 +34,43 @@ tests: - notExists: path: spec.template.spec.containers[0].ports[0].hostPort + - it: several routers each announce their own pod IP, not the shared Service name + set: + router.replicaCount: 2 + documentSelector: + path: kind + value: StatefulSet + asserts: + - contains: + path: spec.template.spec.containers[0].args + content: --external-addr=/ip4/$(POD_IP)/tcp/4501 + - notContains: + path: spec.template.spec.containers[0].args + content: --external-addr=/dns4/agentmesh-router/tcp/4501 + - contains: + path: spec.template.spec.containers[0].env + content: + name: POD_IP + valueFrom: + fieldRef: + fieldPath: status.podIP + + - it: explicit externalAddrs still win with several routers + set: + router.replicaCount: 2 + router.externalAddrs: + - /dns4/routers.example.com/tcp/4501 + documentSelector: + path: kind + value: StatefulSet + asserts: + - contains: + path: spec.template.spec.containers[0].args + content: --external-addr=/dns4/routers.example.com/tcp/4501 + - notContains: + path: spec.template.spec.containers[0].args + content: --external-addr=/ip4/$(POD_IP)/tcp/4501 + - it: announced DNS address follows router.service.port set: router.service.port: 9999 From 4c1c67a9cb674b78b6a14219db13c399b422313d Mon Sep 17 00:00:00 2001 From: Fernando Marino Date: Fri, 6 Nov 2026 09:37:15 +0100 Subject: [PATCH 2/3] agentmesh chart: inject POD_IP only when the router announces it --- .../templates/router-statefulset.yaml | 5 ++-- .../tests/router-statefulset_test.yaml | 25 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/charts/agentmesh/templates/router-statefulset.yaml b/charts/agentmesh/templates/router-statefulset.yaml index e88fad5eb..b30ce498e 100644 --- a/charts/agentmesh/templates/router-statefulset.yaml +++ b/charts/agentmesh/templates/router-statefulset.yaml @@ -114,7 +114,8 @@ spec: periodSeconds: 15 resources: {{- toYaml .Values.router.resources | nindent 10 }} - {{- if or .Values.router.hostPort (gt (int .Values.router.replicaCount) 1) }} + {{- $podIP := and (gt (int .Values.router.replicaCount) 1) (not .Values.router.externalAddrs) (not .Values.router.hostPort) }} + {{- if or .Values.router.hostPort $podIP }} env: {{- if .Values.router.hostPort }} # kubelet expands $(HOST_IP) in the args below. @@ -123,7 +124,7 @@ spec: fieldRef: fieldPath: status.hostIP {{- end }} - {{- if gt (int .Values.router.replicaCount) 1 }} + {{- if $podIP }} - name: POD_IP valueFrom: fieldRef: diff --git a/charts/agentmesh/tests/router-statefulset_test.yaml b/charts/agentmesh/tests/router-statefulset_test.yaml index 8c1c96fbc..464c458ac 100644 --- a/charts/agentmesh/tests/router-statefulset_test.yaml +++ b/charts/agentmesh/tests/router-statefulset_test.yaml @@ -70,6 +70,31 @@ tests: - notContains: path: spec.template.spec.containers[0].args content: --external-addr=/ip4/$(POD_IP)/tcp/4501 + - notExists: + path: spec.template.spec.containers[0].env + + - it: POD_IP is only injected when the router announces it + set: + router.replicaCount: 2 + router.hostPort: 4501 + documentSelector: + path: kind + value: StatefulSet + asserts: + - contains: + path: spec.template.spec.containers[0].env + content: + name: HOST_IP + valueFrom: + fieldRef: + fieldPath: status.hostIP + - notContains: + path: spec.template.spec.containers[0].env + content: + name: POD_IP + valueFrom: + fieldRef: + fieldPath: status.podIP - it: announced DNS address follows router.service.port set: From f6de1cd33dd6064a4bba9b89d754c5e5a9b2bb2c Mon Sep 17 00:00:00 2001 From: Fernando Marino Date: Tue, 17 Nov 2026 08:47:02 +0100 Subject: [PATCH 3/3] charts/agentmesh: with several router replicas, leave out --external-addr so each router announces its own pod IP; drop bootstrapTokenMaxUsages --- charts/agentmesh/README.md | 4 +- charts/agentmesh/templates/bootstrap-job.yaml | 2 +- .../templates/router-statefulset.yaml | 17 +--- .../agentmesh/tests/bootstrap-job_test.yaml | 12 --- .../tests/router-statefulset_test.yaml | 88 ++++++------------- site/content/docs/guides/kubernetes.md | 1 + 6 files changed, 33 insertions(+), 91 deletions(-) diff --git a/charts/agentmesh/README.md b/charts/agentmesh/README.md index 4e4173f55..1fa9a3441 100644 --- a/charts/agentmesh/README.md +++ b/charts/agentmesh/README.md @@ -171,6 +171,6 @@ alongside libp2p's own relay and connection metrics. ## Several routers (`router.replicaCount` > 1) -Each router announces its own pod IP (`--external-addr=/ip4/$(POD_IP)/tcp/4501`), because a router is identified by its peer id and the shared `-router` Service would send a dial to whichever pod it likes (the handshake then fails with "peer id mismatch"). With one replica, or with `router.externalAddrs` / `router.hostPort` set, nothing changes. The pod IP changes when a pod restarts; the router announces the new one when it enrolls. +With more than one replica the chart leaves out `--external-addr`, so each router announces the addresses it listens on: its own pod IP, for TCP and QUIC. A router is identified by its peer id, and the shared `-router` Service would send a dial to whichever pod it likes (the handshake then fails with "peer id mismatch"). The router puts its address in its lease, which it sends on its first sync with the control plane after it starts and renews from then on. With one replica, or with `router.externalAddrs` / `router.hostPort` set, nothing changes. -A router keeps no credential across restarts, so it enrolls again every time it starts. With the default bootstrap token (one use per replica) a restarted router is refused. Set `router.bootstrapTokenMaxUsages` to cover the restarts you expect, or avoid the token: `router.useOidcToken=true` with the cluster's service-account issuer trusted by the control plane (see the workload issuer section above). +Change `replicaCount` through Helm (`helm upgrade --set router.replicaCount=N`). `kubectl scale` does not render the template again, so the routers keep the address the chart rendered before. diff --git a/charts/agentmesh/templates/bootstrap-job.yaml b/charts/agentmesh/templates/bootstrap-job.yaml index b1624f94b..d4c87e05b 100644 --- a/charts/agentmesh/templates/bootstrap-job.yaml +++ b/charts/agentmesh/templates/bootstrap-job.yaml @@ -136,7 +136,7 @@ spec: TOKEN_RESP=$(curl -sf -X POST \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${ADMIN_TOKEN}" \ - -d '{"role": "mesh:role:router", "max_usages": {{ .Values.router.bootstrapTokenMaxUsages | default .Values.router.replicaCount | int }}}' \ + -d '{"role": "mesh:role:router", "max_usages": {{ .Values.router.replicaCount | int }}}' \ "${CP_URL}/admin/bootstrap-tokens") ROUTER_TOKEN=$(echo "${TOKEN_RESP}" | sed -n 's/.*"token"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p') diff --git a/charts/agentmesh/templates/router-statefulset.yaml b/charts/agentmesh/templates/router-statefulset.yaml index b30ce498e..d04613b38 100644 --- a/charts/agentmesh/templates/router-statefulset.yaml +++ b/charts/agentmesh/templates/router-statefulset.yaml @@ -114,23 +114,14 @@ spec: periodSeconds: 15 resources: {{- toYaml .Values.router.resources | nindent 10 }} - {{- $podIP := and (gt (int .Values.router.replicaCount) 1) (not .Values.router.externalAddrs) (not .Values.router.hostPort) }} - {{- if or .Values.router.hostPort $podIP }} - env: {{- if .Values.router.hostPort }} + env: # kubelet expands $(HOST_IP) in the args below. - name: HOST_IP valueFrom: fieldRef: fieldPath: status.hostIP {{- end }} - {{- if $podIP }} - - name: POD_IP - valueFrom: - fieldRef: - fieldPath: status.podIP - {{- end }} - {{- end }} args: - "--control-plane=http://{{ include "agentmesh.fullname" . }}-control-plane:{{ .Values.controlPlane.service.port }}" # In-cluster plaintext to the chart's own control-plane Service; the @@ -158,10 +149,8 @@ spec: - "--external-addr=/ip4/$(HOST_IP)/tcp/{{ .Values.router.hostPort }}" - "--external-addr=/ip4/$(HOST_IP)/udp/{{ .Values.router.hostPort }}/quic-v1" {{- else if gt (int .Values.router.replicaCount) 1 }} - # one address per pod: a router's identity is its peer id, and the shared Service name sends a dial to whichever pod - # it likes ("peer id mismatch"). (Per-pod DNS would need serviceName to be a headless Service, which a StatefulSet - # cannot change on upgrade.) The pod IP changes on restart; the router announces the new one when it enrolls. - - "--external-addr=/ip4/$(POD_IP)/tcp/4501" + # No --external-addr: each router announces the addresses it listens on, which are its own pod IP (TCP and QUIC). + # The shared Service name would send a dial to whichever pod it likes ("peer id mismatch"). {{- else }} - "--external-addr=/dns4/{{ include "agentmesh.fullname" . }}-router/tcp/{{ .Values.router.service.port }}" {{- end }} diff --git a/charts/agentmesh/tests/bootstrap-job_test.yaml b/charts/agentmesh/tests/bootstrap-job_test.yaml index 974619571..0676e9935 100644 --- a/charts/agentmesh/tests/bootstrap-job_test.yaml +++ b/charts/agentmesh/tests/bootstrap-job_test.yaml @@ -128,15 +128,3 @@ tests: - notMatchRegex: path: spec.template.spec.containers[0].args[0] pattern: 'agentmesh-box' - - - it: router.bootstrapTokenMaxUsages overrides the one-use-per-replica default - set: - router.replicaCount: 2 - router.bootstrapTokenMaxUsages: 20 - documentSelector: - path: kind - value: Job - asserts: - - matchRegex: - path: spec.template.spec.containers[0].args[0] - pattern: '"role": "mesh:role:router", "max_usages": 20\}' diff --git a/charts/agentmesh/tests/router-statefulset_test.yaml b/charts/agentmesh/tests/router-statefulset_test.yaml index 464c458ac..4ce61bc8e 100644 --- a/charts/agentmesh/tests/router-statefulset_test.yaml +++ b/charts/agentmesh/tests/router-statefulset_test.yaml @@ -34,68 +34,6 @@ tests: - notExists: path: spec.template.spec.containers[0].ports[0].hostPort - - it: several routers each announce their own pod IP, not the shared Service name - set: - router.replicaCount: 2 - documentSelector: - path: kind - value: StatefulSet - asserts: - - contains: - path: spec.template.spec.containers[0].args - content: --external-addr=/ip4/$(POD_IP)/tcp/4501 - - notContains: - path: spec.template.spec.containers[0].args - content: --external-addr=/dns4/agentmesh-router/tcp/4501 - - contains: - path: spec.template.spec.containers[0].env - content: - name: POD_IP - valueFrom: - fieldRef: - fieldPath: status.podIP - - - it: explicit externalAddrs still win with several routers - set: - router.replicaCount: 2 - router.externalAddrs: - - /dns4/routers.example.com/tcp/4501 - documentSelector: - path: kind - value: StatefulSet - asserts: - - contains: - path: spec.template.spec.containers[0].args - content: --external-addr=/dns4/routers.example.com/tcp/4501 - - notContains: - path: spec.template.spec.containers[0].args - content: --external-addr=/ip4/$(POD_IP)/tcp/4501 - - notExists: - path: spec.template.spec.containers[0].env - - - it: POD_IP is only injected when the router announces it - set: - router.replicaCount: 2 - router.hostPort: 4501 - documentSelector: - path: kind - value: StatefulSet - asserts: - - contains: - path: spec.template.spec.containers[0].env - content: - name: HOST_IP - valueFrom: - fieldRef: - fieldPath: status.hostIP - - notContains: - path: spec.template.spec.containers[0].env - content: - name: POD_IP - valueFrom: - fieldRef: - fieldPath: status.podIP - - it: announced DNS address follows router.service.port set: router.service.port: 9999 @@ -143,6 +81,32 @@ tests: path: spec.template.spec.containers[0].args content: --external-addr=/ip4/$(HOST_IP)/tcp/4501 + - it: with several replicas announces no external address, so each router announces its own pod IP + set: + router.replicaCount: 2 + documentSelector: + path: kind + value: StatefulSet + asserts: + - notContains: + path: spec.template.spec.containers[0].args + content: --external-addr=/dns4/agentmesh-router/tcp/4501 + - isNull: + path: spec.template.spec.containers[0].env + + - it: externalAddrs still wins with several replicas + set: + router.replicaCount: 2 + router.externalAddrs: + - /dns4/x/tcp/1 + documentSelector: + path: kind + value: StatefulSet + asserts: + - contains: + path: spec.template.spec.containers[0].args + content: --external-addr=/dns4/x/tcp/1 + - it: keeps the key on a PVC, not emptyDir documentSelector: path: kind diff --git a/site/content/docs/guides/kubernetes.md b/site/content/docs/guides/kubernetes.md index c5f630c61..e5012b731 100644 --- a/site/content/docs/guides/kubernetes.md +++ b/site/content/docs/guides/kubernetes.md @@ -119,6 +119,7 @@ which is meant for development clusters. For the router, `router.hostPort: 4501` binds TCP and UDP port 4501 on the Kubernetes node where the router pod runs, and announces that node's IP. +With more than one router replica, each router announces its own pod IP. `router.externalAddrs` replaces the announced addresses, for example with a fixed DNS name: