Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
development/kind: deploy the mesh behind the gateway surface
Tomas Tormoaade64fec92b
diff --git a/Makefile b/Makefileindex 58fc5b4c..f5c6a237 100644--- a/Makefile+++ b/Makefile@@ -118,7 +118,7 @@ kind-logs:./development/kind/run.sh -lkind-down:- kind delete cluster --name sam-kind+ ./development/kind/run.sh -d.PHONY: kind-local-nodekind-local-node:diff --git a/development/kind/dex.yaml b/development/kind/dex.yamlnew file mode 100644index 00000000..b068d31b--- /dev/null+++ b/development/kind/dex.yaml@@ -0,0 +1,107 @@+# Dev-only Dex for the kind mesh. Dex is an independent component, so it lives here, not+# in the Helm chart. run.sh applies this with envsubst, filling ${DEX_ISSUER} and+# ${CONSOLE_REDIRECT_URI} once the gateway addresses exist.+apiVersion: v1+kind: ConfigMap+metadata:+ name: sam-mesh-dex-config+data:+ config.yaml: |+ issuer: ${DEX_ISSUER}+ storage:+ type: memory+ web:+ http: 0.0.0.0:5556+ oauth2:+ skipApprovalScreen: true+ staticClients:+ - id: sam-console+ redirectURIs:+ - ${CONSOLE_REDIRECT_URI}+ name: 'SAM Console'+ public: true+ enablePasswordDB: true+ staticPasswords:+ # The control plane auto-registers a first-time OIDC login with the "user" role, so+ # this account is not an admin however it is named.+ - email: [email protected]+ # bcrypt hash for 'password'+ hash: "$2a$10$TUOgJVdtu0AL6Y6TcNBQXOSjRWRKhIkC0kXDzN9kOkPB4hg2aFPE2"+ username: user+ userID: "0898fa90-3453-4311-8566-020953250212"+---+apiVersion: apps/v1+kind: Deployment+metadata:+ name: sam-mesh-dex+spec:+ replicas: 1+ selector:+ matchLabels:+ app: sam-mesh-dex+ template:+ metadata:+ labels:+ app: sam-mesh-dex+ spec:+ nodeSelector:+ sam-role: control-plane+ containers:+ - name: dex+ image: dexidp/dex:v2.39.0+ args:+ - dex+ - serve+ - /etc/dex/cfg/config.yaml+ ports:+ - containerPort: 5556+ name: http+ volumeMounts:+ - name: config+ mountPath: /etc/dex/cfg+ volumes:+ - name: config+ configMap:+ name: sam-mesh-dex-config+---+apiVersion: v1+kind: Service+metadata:+ name: sam-mesh-dex+spec:+ selector:+ app: sam-mesh-dex+ ports:+ - name: http+ port: 5556+ targetPort: 5556+---+apiVersion: gateway.networking.k8s.io/v1+kind: Gateway+metadata:+ name: sam-mesh-dex-gateway+spec:+ gatewayClassName: cloud-provider-kind+ listeners:+ - name: http+ protocol: HTTP+ port: 80+ allowedRoutes:+ namespaces:+ from: Same+---+apiVersion: gateway.networking.k8s.io/v1+kind: HTTPRoute+metadata:+ name: sam-mesh-dex-route+spec:+ parentRefs:+ - name: sam-mesh-dex-gateway+ rules:+ - matches:+ - path:+ type: PathPrefix+ value: /+ backendRefs:+ - name: sam-mesh-dex+ port: 5556diff --git a/development/kind/kind-config.yaml b/development/kind/kind-config.yamlindex 58f08211..80906475 100644--- a/development/kind/kind-config.yaml+++ b/development/kind/kind-config.yaml@@ -2,21 +2,6 @@ kind: ClusterapiVersion: kind.x-k8s.io/v1alpha4nodes:- role: control-plane- # Expose the control plane and router to the host (via the sam-control-plane-nodeport NodePort) so a- # locally-built sam-node can enroll: host:9090->HTTP, host:4001->libp2p TCP.- extraPortMappings:- - containerPort: 30090- hostPort: 9090- protocol: TCP- - containerPort: 30401- hostPort: 4001- protocol: TCP- - containerPort: 30081- hostPort: 9092- protocol: TCP- - containerPort: 30556- hostPort: 5556- protocol: TCP- role: workerlabels:sam-role: control-plane@@ -26,12 +11,3 @@ nodes:- role: workerlabels:sam-role: node-b- - role: worker- labels:- sam-role: node-c- - role: worker- labels:- sam-role: node-d- - role: worker- labels:- sam-role: node-ediff --git a/development/kind/mesh-config.e2e.yaml b/development/kind/mesh-config.e2e.yamlindex 3f8bf904..86753d6c 100644--- a/development/kind/mesh-config.e2e.yaml+++ b/development/kind/mesh-config.e2e.yaml@@ -2,6 +2,3 @@# mcp://calculator/add. Select it with MESH_CONFIG=development/kind/mesh-config.e2e.yaml.node-a:node-b: calc-mcp-node-c:-node-d:-node-e:diff --git a/development/kind/mesh-config.yaml b/development/kind/mesh-config.yamlindex 6f6e4587..58430ebb 100644--- a/development/kind/mesh-config.yaml+++ b/development/kind/mesh-config.yaml@@ -1,11 +1,8 @@# node -> service. A blank value means a bare node (no service, e.g. a caller).# Set a value to host a service on that node; the value is a folder path under# development/examples/, e.g:-# node-b: calc-mcp-# node-c: code-reviewer-pool/reviewer+# node-a: calc-mcp+# node-b: code-reviewer-pool/reviewer# All nodes ship bare by default — assign services to suit what you're testing.node-a:node-b:-node-c:-node-d:-node-e:diff --git a/development/kind/run-local-node.sh b/development/kind/run-local-node.shindex f56a31a2..469d746b 100755--- a/development/kind/run-local-node.sh+++ b/development/kind/run-local-node.sh@@ -1,8 +1,7 @@#!/usr/bin/env bash-# Enroll a locally-built ./bin/sam-node into the kind mesh for local testing.-# The control plane (exposed via sam-control-plane-nodeport + kind extraPortMappings) is reached at-# 127.0.0.1:9090 (HTTP enroll) and 127.0.0.1:4001 (libp2p TCP); the node relays-# peer traffic through the router. Extra args pass through, e.g. to host a service:+# Enroll a locally-built ./bin/sam-node into the kind mesh the way a real external node joins:+# a bootstrap token over the control plane's gateway address, peer traffic through the router's+# node-IP multiaddrs. Extra args pass through, e.g. to host a service:# ARGS="--config development/examples/calc-mcp/sam-node-config.yaml"set -euo pipefail@@ -14,25 +13,38 @@ PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"cd "${PROJECT_ROOT}"# Prereqs-for bin in kubectl kind; do+for bin in kubectl kind curl jq; docommand -v "$bin" >/dev/null 2>&1 || { echo "missing prerequisite: $bin" >&2; exit 1; }donekind get clusters 2>/dev/null | grep -qx "${CLUSTER}" || {echo "kind cluster '${CLUSTER}' not found; start it first: make kind-up" >&2; exit 1; }[[ -x ./bin/sam-node ]] || { echo "./bin/sam-node not found; build it first: make build" >&2; exit 1; }-# Identity + token-kubectl --context "${KCTX}" -n "${NAMESPACE}" create serviceaccount local-node-sa \- >/dev/null 2>&1 || true-JWT="$(kubectl --context "${KCTX}" -n "${NAMESPACE}" create token local-node-sa \- --audience=sam-mesh-audience --duration=1h)"+MAIN_IP="$(kubectl --context "${KCTX}" -n "${NAMESPACE}" get gateway sam-mesh-gateway \+ -o jsonpath='{.status.addresses[0].value}' 2>/dev/null || true)"+[[ -n "${MAIN_IP}" ]] || {+ echo "gateway 'sam-mesh-gateway' has no LoadBalancer address; is cloud-provider-kind running?" >&2+ exit 1; }+CONTROL_PLANE_URL="http://${MAIN_IP}"-echo "Enrolling local ./bin/sam-node into the mesh control plane at http://127.0.0.1:9090…"+# Mint an enrollment credential the way an operator would, through the dev-routed /admin API.+ADMIN_TOKEN="$(kubectl --context "${KCTX}" -n "${NAMESPACE}" get secret sam-mesh-secrets \+ -o jsonpath='{.data.admin-token}' | base64 -d)"+TOKEN_RESPONSE="$(curl -s -X POST "${CONTROL_PLANE_URL}/admin/bootstrap-tokens" \+ -H "Authorization: Bearer ${ADMIN_TOKEN}" \+ -H "Content-Type: application/json" \+ -d '{"role":"sam:role:node","ttl_hours":1,"max_usages":1,"description":"local sam-node"}' || true)"+BOOTSTRAP_TOKEN="$(printf '%s' "${TOKEN_RESPONSE}" | jq -r '.token // empty' 2>/dev/null || true)"+[[ -n "${BOOTSTRAP_TOKEN}" ]] || {+ echo "could not mint a bootstrap token at ${CONTROL_PLANE_URL}/admin/bootstrap-tokens: ${TOKEN_RESPONSE}" >&2+ exit 1; }++echo "Enrolling local ./bin/sam-node into the mesh control plane at ${CONTROL_PLANE_URL}…"echo " MCP/sidecar API on 127.0.0.1:9099"export SAM_API_TOKEN=devtokenexec ./bin/sam-node run \- --control-plane http://127.0.0.1:9090 \- --jwt "${JWT}" \+ --control-plane "${CONTROL_PLANE_URL}" \+ --bootstrap-token "${BOOTSTRAP_TOKEN}" \--listen /ip4/0.0.0.0/tcp/0 \--bind-addr 127.0.0.1:9099 \--discovery-interval 200ms \diff --git a/development/kind/run.sh b/development/kind/run.shindex 3712b28a..35b94939 100755--- a/development/kind/run.sh+++ b/development/kind/run.sh@@ -1,6 +1,7 @@#!/usr/bin/env bash-# Kind dev mesh: a control plane and router plus the nodes declared in mesh-config.yaml, each pinned-# to its own k8s node, with live per-pod logs in named tmux panes.+# Kind dev mesh: a control plane and router plus the nodes from mesh-config.yaml, each pinned to its+# own k8s node, with live per-pod logs in named tmux panes. Control plane, console and Dex are reached+# over Gateway API LoadBalancer addresses from cloud-provider-kind (started here); the router at its node IP.set -euo pipefailCLUSTER="sam-kind"@@ -10,6 +11,13 @@ KCTX="kind-${CLUSTER}"IMAGE_TAG="local"CONTROL_PLANE_URL="http://sam-mesh-control-plane:8080"HELM="helm"+# Serves the gateway LoadBalancer IPs. It installs the Gateway API CRDs and the+# cloud-provider-kind GatewayClass itself, so the cluster needs no CRD step.+CPK_CONTAINER="cloud-provider-kind"+CPK_IMAGE="registry.k8s.io/cloud-provider-kind/cloud-controller-manager:v0.11.1"+# The console shares the control plane's gateway under this prefix, as it does on the+# cloud: the route rewrites the prefix away before the console sees the request.+CONSOLE_BASE_PATH="/console"@@ -39,6 +47,77 @@ check_prereqs() {fi}+# cloud-provider-kind names each gateway's envoy container after a hash of the gateway, so+# one left behind by a deleted cluster is adopted by the next run and reported Programmed+# while its config stream is dead and it serves nothing.+remove_lb_containers() {+ local containers+ containers="$(docker ps -aq -f "label=io.x-k8s.cloud-provider-kind.cluster=${CLUSTER}")"+ if [[ -n "${containers}" ]]; then+ # shellcheck disable=SC2086 # one container id per argument+ docker rm -f ${containers} >/dev/null+ fi+}++# Must run after the cluster, so the 'kind' docker network exists.+start_cloud_provider_kind() {+ remove_lb_containers+ if [[ -n "$(docker ps -q -f "name=^${CPK_CONTAINER}$")" ]]; then+ echo "cloud-provider-kind already running"+ return+ fi+ docker rm -f "${CPK_CONTAINER}" >/dev/null 2>&1 || true+ docker run -d --name "${CPK_CONTAINER}" --network kind \+ -v /var/run/docker.sock:/var/run/docker.sock "${CPK_IMAGE}" >/dev/null+}++# Stop cloud-provider-kind before its containers, or it recreates them, and remove them before+# deleting the cluster, or deleting it makes cloud-provider-kind race us to the same containers.+teardown() {+ docker rm -f "${CPK_CONTAINER}" >/dev/null 2>&1 || true+ remove_lb_containers+ kind delete cluster --name "${CLUSTER}"+}++# gateway_ip <gateway>: the LoadBalancer address cloud-provider-kind assigned, waited for.+gateway_ip() {+ local gateway="$1" ip=""+ for _ in $(seq 1 60); do+ ip="$(kubectl --context "${KCTX}" -n "${NAMESPACE}" get gateway "${gateway}" \+ -o jsonpath='{.status.addresses[0].value}' 2>/dev/null || true)"+ [[ -n "$ip" ]] && { echo "$ip"; return 0; }+ sleep 2+ done+ echo "gateway '${gateway}' never got a LoadBalancer address; is cloud-provider-kind running?" >&2+ return 1+}++# deploy_chart [extra --set flags]: later flags win, so phase 2 overrides phase 1.+# TLS verification is skipped because one issuer is the kind cluster's own API server,+# served with a self-signed cert.+deploy_chart() {+ "${HELM}" --kube-context "${KCTX}" upgrade --install sam-mesh "${PROJECT_ROOT}/charts/sam-mesh" --timeout 10m \+ --namespace "${NAMESPACE}" \+ --set global.imageTag="${IMAGE_TAG}" \+ --set controlPlane.oidcIssuer="${CONTROL_PLANE_ISSUERS//,/\\,}" \+ --set controlPlane.allowedAudiences="${ALLOWED_AUDIENCES//,/\\,}" \+ --set controlPlane.insecureSkipTlsVerify=true \+ --set gateway.enabled=true \+ --set gateway.className=cloud-provider-kind \+ --set gateway.adminRoute=true \+ --set router.hostPort=4501 \+ --set router.nodeSelector.sam-role=control-plane \+ --set router.useOidcToken=true \+ "$@"+}++# apply_dex <issuer> <console redirect URI>: Dex is an independent component, deployed+# from dex.yaml rather than the chart, with the URLs of the moment substituted in.+apply_dex() {+ DEX_ISSUER="$1" CONSOLE_REDIRECT_URI="$2" \+ envsubst '${DEX_ISSUER} ${CONSOLE_REDIRECT_URI}' < "${SCRIPT_DIR}/dex.yaml" \+ | kubectl --context "${KCTX}" -n "${NAMESPACE}" apply -f -+}render_and_apply() {local node="$1" svc="$2"@@ -107,8 +186,8 @@ read_mesh_nodes() {### MAIN ###-if [[ $# -gt 0 && "$1" != "-s" && "$1" != "-l" ]]; then- echo "usage: $(basename "$0") [-s]" >&2+if [[ $# -gt 0 && "$1" != "-s" && "$1" != "-l" && "$1" != "-d" ]]; then+ echo "usage: $(basename "$0") [-s|-l|-d]" >&2exit 1fi@@ -118,11 +197,19 @@ if [[ "${1:-}" == "-l" ]]; thenexit 0fi+if [[ "${1:-}" == "-d" ]]; then+ teardown+ exit 0+fi+check_prereqs "${1:-}"echo "== Creating kind cluster '${CLUSTER}' =="kind create cluster --name "${CLUSTER}" --config "${SCRIPT_DIR}/kind-config.yaml"+echo "== Starting cloud-provider-kind =="+start_cloud_provider_kind+echo "== Building sam images =="make docker-build-control-plane docker-build-router docker-build-node docker-build-sam-consoleecho "== Loading sam images into kind =="@@ -134,48 +221,68 @@ read_mesh_nodesISSUER="$(kubectl --context "${KCTX}" get --raw /.well-known/openid-configuration | jq -r .issuer)"[[ -n "$ISSUER" ]] || { echo "could not determine cluster OIDC issuer" >&2; exit 1; }-OIDC_ISSUER="${OIDC_ISSUER:-http://sam-mesh-dex:5556/dex}"-OIDC_CLIENT_ID="${OIDC_CLIENT_ID:-sam-console}"-OIDC_CLIENT_SECRET="${OIDC_CLIENT_SECRET:-}"-OIDC_REDIRECT_URL="${OIDC_REDIRECT_URL:-}"-+# Dex's own address isn't known until its gateway has one, so phase 1 trusts only the+# cluster issuer and phase 2 prepends Dex.CONTROL_PLANE_ISSUERS="${ISSUER}"-if [[ -n "${OIDC_ISSUER}" ]]; then- CONTROL_PLANE_ISSUERS="${OIDC_ISSUER},${ISSUER}"-fi-ALLOWED_AUDIENCES="sam-mesh-audience,sam-control-plane-audience"-if [[ -n "${OIDC_CLIENT_ID}" ]]; then- ALLOWED_AUDIENCES="${OIDC_CLIENT_ID},${ALLOWED_AUDIENCES}"-fi+# The first audience is what the control plane reports as the OIDC client id, so it has to+# match Dex's static client.+ALLOWED_AUDIENCES="sam-console,sam-mesh-audience,sam-control-plane-audience"-export NAMESPACE ISSUER IMAGE_TAG OIDC_ISSUER OIDC_CLIENT_ID OIDC_CLIENT_SECRET OIDC_REDIRECT_URL CONTROL_PLANE_ISSUERS ALLOWED_AUDIENCES+# Only the node template's envsubst reads these from the environment.+export NAMESPACE IMAGE_TAGecho "== Applying namespace and RBAC cluster rules =="envsubst '${NAMESPACE}' < "${SCRIPT_DIR}/00-namespace-rbac.yaml" | kubectl --context "${KCTX}" apply -f -echo "== Deploying SAM Mesh via Helm =="-"${HELM}" --kube-context "${KCTX}" upgrade --install sam-mesh "${PROJECT_ROOT}/charts/sam-mesh" --timeout 10m \- --namespace "${NAMESPACE}" \- --set global.imageTag="${IMAGE_TAG}" \- --set controlPlane.oidcIssuer="${CONTROL_PLANE_ISSUERS//,/\\,}" \- --set controlPlane.allowedAudiences="${ALLOWED_AUDIENCES//,/\\,}" \- --set controlPlane.service.type=NodePort \- --set controlPlane.service.nodePort=30090 \- --set router.service.type=NodePort \- --set router.service.nodePort=30401 \- --set router.useOidcToken=true \- --set router.externalAddrs[0]="/dns4/sam-mesh-router/tcp/4501" \- --set router.externalAddrs[1]="/ip4/127.0.0.1/tcp/4001" \- --set console.service.type=NodePort \- --set console.service.nodePort=30081 \- --set dex.enabled=true \- --set controlPlane.insecureSkipTlsVerify=true # ISSUER is the kind cluster's own API server, served with a self-signed cert+deploy_chart++# The real URLs aren't known until Dex's gateway exists, so deploy it with placeholders+# first and rewire it below.+echo "== Deploying Dex =="+apply_dex "http://sam-mesh-dex:5556/dex" "http://127.0.0.1${CONSOLE_BASE_PATH}/auth/callback"++# The OIDC URLs are the gateway addresses, which only exist once the gateways do — so+# resolve them, then redeploy with the URLs everything must agree on.+echo "== Waiting for gateway LoadBalancer addresses =="+MAIN_IP="$(gateway_ip sam-mesh-gateway)"+DEX_IP="$(gateway_ip sam-mesh-dex-gateway)"+CONSOLE_URL="http://${MAIN_IP}${CONSOLE_BASE_PATH}/"+echo "control plane: http://${MAIN_IP} console: ${CONSOLE_URL} dex: http://${DEX_IP}"++OIDC_ISSUER="http://${DEX_IP}/dex"++echo "== Wiring the OIDC URLs into Dex =="+apply_dex "${OIDC_ISSUER}" "http://${MAIN_IP}${CONSOLE_BASE_PATH}/auth/callback"+# A config-only change doesn't roll the pods, and Dex reads its config at startup.+kubectl --context "${KCTX}" -n "${NAMESPACE}" rollout restart deployment/sam-mesh-dex+kubectl --context "${KCTX}" -n "${NAMESPACE}" rollout status deployment/sam-mesh-dex --timeout=180s++# The control plane discovers the issuer at startup and refuses to start if it can't, so+# prove a pod can reach the gateway address before pinning the mesh to it.+echo "== Checking Dex discovery from inside the cluster =="+# A pod left behind by an interrupted run fails the next one with AlreadyExists, which+# reads exactly like a routing failure.+kubectl --context "${KCTX}" -n "${NAMESPACE}" delete pod dex-discovery-check --ignore-not-found >/dev/null+kubectl --context "${KCTX}" -n "${NAMESPACE}" run dex-discovery-check \+ --rm -i --restart=Never --quiet --image=curlimages/curl:8.6.0 -- \+ curl -sf --retry 10 --retry-delay 2 --retry-connrefused --connect-timeout 5 --max-time 20 \+ "${OIDC_ISSUER}/.well-known/openid-configuration" >/dev/null || {+ echo "cannot reach ${OIDC_ISSUER} from inside the cluster; pods must be able to route to the cloud-provider-kind LoadBalancer addresses" >&2+ exit 1+ }++echo "== Wiring the OIDC URLs into the control plane =="+CONTROL_PLANE_ISSUERS="${OIDC_ISSUER},${ISSUER}"+deploy_chartecho "== Waiting for database to be ready =="kubectl --context "${KCTX}" -n "${NAMESPACE}" wait --for=condition=ready --timeout=180s pod -l app=sam-mesh-dbecho "== Waiting for control plane to be ready =="-kubectl --context "${KCTX}" -n "${NAMESPACE}" wait --for=condition=available --timeout=180s deployment/sam-mesh-control-plane+# rollout status, not wait --for=available: every pod must serve the Dex issuer before+# the console reads it below.+kubectl --context "${KCTX}" -n "${NAMESPACE}" rollout status deployment/sam-mesh-control-plane --timeout=180s# Policy seeding is handled automatically by the Helm chart bootstrap job, we just wait for it to complete.echo "== Waiting for bootstrap job to complete =="@@ -183,10 +290,12 @@ kubectl --context "${KCTX}" -n "${NAMESPACE}" wait --for=condition=complete --tiecho "== Waiting for router to be ready =="kubectl --context "${KCTX}" -n "${NAMESPACE}" wait --for=condition=ready --timeout=180s pod -l app=sam-mesh-router-echo "== Waiting for console to be ready =="-kubectl --context "${KCTX}" -n "${NAMESPACE}" wait --for=condition=available --timeout=180s deployment/sam-mesh-console-echo "== Waiting for Dex to be ready =="-kubectl --context "${KCTX}" -n "${NAMESPACE}" wait --for=condition=available --timeout=180s deployment/sam-mesh-dex++# The console discovers the issuer from the control plane's /info once, at startup, so+# restart it now that the control plane serves the Dex issuer.+echo "== Restarting the console with the final issuer =="+kubectl --context "${KCTX}" -n "${NAMESPACE}" rollout restart deployment/sam-mesh-console+kubectl --context "${KCTX}" -n "${NAMESPACE}" rollout status deployment/sam-mesh-console --timeout=180secho "== Applying sam-nodes =="@@ -201,13 +310,16 @@ for node in "${NODES[@]}"; dodoneecho-echo "Mesh up. To call a node's MCP API, port-forward it in another shell, e.g.:"-echo " kubectl --context ${KCTX} -n ${NAMESPACE} port-forward deploy/node-a 9091:8080"-echo "then:"-echo " ./bin/mcp-client -url http://127.0.0.1:9091/mcp -token devtoken -tool find_remote_tools -args '{}'"-echo ""-echo "You can access the SAM Web Console at:"-echo " http://localhost:9092/"+echo "Mesh up."+echo " console: ${CONSOLE_URL}"+echo " control plane: http://${MAIN_IP}"+echo " dex: ${OIDC_ISSUER}"+echo+echo "To drive the mesh, enroll a local node in another shell (it stays in the foreground):"+echo " make build && make kind-local-node"+echo "then call its MCP API on 127.0.0.1:9099:"+echo " ./bin/mcp-client -url http://127.0.0.1:9099/mcp -token devtoken -tool get_mesh_info -args '{}'"+echo " ./bin/mcp-client -url http://127.0.0.1:9099/mcp -token devtoken -tool find_remote_tools -args '{}'"if [[ "${1:-}" != "-s" ]]; thenshow_cluster_logs