# control-plane-e2e

Local closed-loop debugging for the `global -> regional -> create metrics key` path.

What it does:

- starts a local `regional` server against the current `kubectl` cluster
- starts a local `global` server with `regionalExecutor` pointing to the local regional
- uses `mirrord port-forward` to expose the global-side gRPC dependencies needed by `CreateMetricsKey`
- calls `CreateMetricsKey` through the local global gRPC server
- verifies the expected ConfigMap is created in `prometheus-integration`

How it runs:

- `regional` runs as a local `go run` process on `127.0.0.1:18085` (gateway) and `127.0.0.1:18086` (gRPC)
- `global` runs as a local `go run` process on `127.0.0.1:18080` (gateway) and `127.0.0.1:18081` (gRPC)
- both are plain local processes, not containers
- the script keeps repo config unchanged and generates a temp `global` config file for the run

mirrord wiring:

- `mirrord port-forward` exposes `tidb-mgmt-svc-server-grpc` to `127.0.0.1:10001`
- `mirrord port-forward` exposes `account-provider-grpc` to `127.0.0.1:10000`
- `mirrord port-forward` exposes the dev metadb host to `127.0.0.1:13306`
- local `global` therefore still talks to real dev dependencies, just through localhost ports

Temp global config changes:

- sets `server.enableEventBusConsumer=false`
- sets `debugConfig.disableServerWorkflowCli=true`
- rewrites MySQL to `127.0.0.1:$MYSQL_PORT`
- rewrites `regionalExecutor.servers` to the local `regional` gateway
- fills `regionalExecutor.prometheusQueryEndpoints` with the dev mapping used by deployed dev config

Request trigger:

- `global/cmd/control-plane-e2e-client/main.go` is a small local gRPC client used only to trigger `CreateMetricsKey`
- it dials the local `global` gRPC server directly instead of going through HTTP
- it injects `grpc-metadata-x-metrics-integration-user` so the `global` user interceptor sees a valid user context
- on success it prints `query_endpoint`, `key_id`, and `key`

Prerequisites:

- current `kubectl` context points to the target regional cluster
- `mirrord`, `go`, `kubectl`, `jq`, `nc` are installed
- AWS SSO/session used by `mirrord` is valid for the `centralized-storage-dev-global` cluster

Usage:

```bash
bash tools/control-plane-e2e/run-create-metrics-key.sh \
  --cluster-id 10451069541598173914 \
  --org-id 60205 \
  --project-id 3199728
```

Optional flags:

- `--user-email`
- `--tenant-id`
- `--global-gateway-port`
- `--global-grpc-port`
- `--regional-gateway-port`
- `--regional-grpc-port`
- `--mysql-port`

Notes:

- The script patches a temp copy of `global/deployment/config/local/config.yaml`; it does not modify repo config.
- `mirrord port-forward` exposes `tidb-mgmt-svc-server-grpc`, `account-provider-grpc`, and the dev metadb to localhost for the local `global` process.
- The temp config also fills in the dev `prometheusQueryEndpoints` mapping that is present in the deployed dev config but missing from the local config template.
- The request is sent over gRPC with injected user metadata, so it bypasses the outer HTTP auth layer and focuses on the control-plane path itself.
- The intent is to validate the real `global -> regional -> k8s` control-plane path without mocking the downstream cluster-side behavior.
