|
| 1 | +# resource-broker |
| 2 | + |
| 3 | +Kubernetes operator for multi-cluster resource brokering. Kubebuilder v4, multigroup layout. Built on `sigs.k8s.io/multicluster-runtime`. |
| 4 | + |
| 5 | +Read the org-wide [AGENTS.md](https://github.com/platform-mesh/.github/blob/main/AGENTS.md) for general conventions. Read [CONTRIBUTING.md](CONTRIBUTING.md) for contribution workflow. |
| 6 | + |
| 7 | +## Commands |
| 8 | + |
| 9 | +- `make check` — codegen + lint + test + test-e2e (CI gate) |
| 10 | +- `make test` — unit tests |
| 11 | +- `make test TEST_ARGS="-run TestName"` — single unit test |
| 12 | +- `make test-e2e` — e2e tests (uses envtest) |
| 13 | +- `make test-e2e TEST_ARGS="-run TestName"` — single e2e test |
| 14 | +- `make codegen` — regenerate CRDs, RBAC, DeepCopy from API types |
| 15 | +- `make lint` / `make lint-fix` — golangci-lint v2 |
| 16 | +- `make build` — build broker binary |
| 17 | +- `make build-operator` — build operator binary |
| 18 | +- `make run` — run broker locally (debug logging) |
| 19 | +- `make run-operator` — run operator locally (debug logging) |
| 20 | +- `make examples` — run all example walkthroughs |
| 21 | +- `make help` — list all targets |
| 22 | + |
| 23 | +## Structure |
| 24 | + |
| 25 | +``` |
| 26 | +api/ CRD types, grouped by domain |
| 27 | + broker/ AcceptAPI, Migration, MigrationConfiguration |
| 28 | + generic/ Cross-cloud APIs (databases, networking, storage, compute, etc.) |
| 29 | + example/ Example CRDs (Certificate, VM) |
| 30 | + operator/ Broker operator CRD |
| 31 | +cmd/ Entrypoints |
| 32 | + main.go resource-broker |
| 33 | + operator/ resource-broker-operator |
| 34 | +pkg/ Core logic |
| 35 | + broker/ Reconcilers (acceptapi, generic, migration) |
| 36 | + sync/ Resource copy and condition tracking |
| 37 | + conditions/ Status condition utilities |
| 38 | + kubernetes/ Annotation and map helpers |
| 39 | +config/ Kustomize manifests (CRDs, RBAC, deployment) |
| 40 | +test/ E2e tests and test utilities |
| 41 | +examples/ Runnable walkthroughs (certs, kcp-certs, platform-mesh) |
| 42 | +contrib/kcp/ kcp-specific Dockerfile and config |
| 43 | +``` |
| 44 | + |
| 45 | +## Architecture |
| 46 | + |
| 47 | +Three cluster roles: coordination, consumer, provider. Two separate binaries: **operator** (single-cluster, manages Broker CRD, deploys broker) and **broker manager** (multi-cluster, runs reconcilers). |
| 48 | + |
| 49 | +### Reconciliation Flow |
| 50 | + |
| 51 | +1. Consumer resource created → consumer watch triggers GenericReconciler |
| 52 | +2. `decorateInConsumer()` adds finalizer + annotations |
| 53 | +3. `providerAcceptsObj()` queries AcceptAPI registry (in-memory map protected by lock) |
| 54 | +4. Resource copied to provider with hashed cluster prefix in name (`SanitizeClusterName()` → 12-char hex) to avoid multi-consumer collisions |
| 55 | +5. Provider watch triggers via `providerEventHandler` which maps back to consumer using annotations |
| 56 | +6. Status synced back to consumer via status subresource, with prefixes stripped by `StatusTransformer` |
| 57 | +7. Related resources (declared in `status.relatedResources`) copied to consumer with owner references |
| 58 | + |
| 59 | +### Provider Churn |
| 60 | + |
| 61 | +If AcceptAPI no longer matches, reconciler sets `new-provider-cluster` annotation, syncs to new provider, waits for readiness, then deletes from old provider. |
| 62 | + |
| 63 | +### Migration |
| 64 | + |
| 65 | +Staged state machine: Unknown → Pending → InitialInProgress → InitialCompleted → CutoverInProgress → CutoverCompleted. Each stage deploys template resources and evaluates CEL expressions against them before progressing. |
| 66 | + |
| 67 | +### Key Annotations (control plane) |
| 68 | + |
| 69 | +- `broker.platform-mesh.io/consumer-cluster`, `consumer-name` — origin tracking |
| 70 | +- `broker.platform-mesh.io/provider-cluster` — current provider |
| 71 | +- `broker.platform-mesh.io/new-provider-cluster` — migration target |
| 72 | + |
| 73 | +### Constants |
| 74 | + |
| 75 | +- Cluster prefixes: `consumer`, `provider`, `coordination` (`pkg/broker/broker.go`) |
| 76 | +- Finalizers: `broker.platform-mesh.io/acceptapi-finalizer`, `broker.platform-mesh.io/generic-finalizer` |
| 77 | +- AcceptAPI filters: ValueIn (enum), Boundary (min/max), Suffix (string suffix) — keys use dot notation for nested spec fields |
| 78 | + |
| 79 | +## Code Conventions |
| 80 | + |
| 81 | +License header on all `.go` files — Apache-2.0, "The Platform Mesh Authors". Enforced by goheader linter. |
| 82 | + |
| 83 | +Import order (enforced by gci): |
| 84 | +1. stdlib |
| 85 | +2. third-party |
| 86 | +3. `k8s.io/` |
| 87 | +4. `sigs.k8s.io/controller-runtime/` |
| 88 | +5. `sigs.k8s.io/multicluster-runtime/` |
| 89 | +6. `github.com/platform-mesh/` |
| 90 | + |
| 91 | +Tests: testify (require/assert). E2e: controller-runtime envtest. All tests must be parallel (paralleltest linter). |
| 92 | + |
| 93 | +Run `make codegen` after any change to `api/` types. |
| 94 | + |
| 95 | +## Git |
| 96 | + |
| 97 | +Conventional Commits: `feat:`, `fix:`, `chore(deps):`, `ci:`, `refactor:`, `test:`, `docs:` |
| 98 | + |
| 99 | +Never add AI attribution to commits or PRs. |
| 100 | + |
| 101 | +## Do Not |
| 102 | + |
| 103 | +- Edit `zz_generated.deepcopy.go` — generated by controller-gen |
| 104 | +- Edit `config/*/crd/*.yaml` directly — generated by `make manifests` |
| 105 | +- Skip the license header — CI enforces it |
| 106 | +- Add tool-specific instruction files (`CLAUDE.md`, `.cursorrules`, etc.) — use this file |
| 107 | +- Commit without running `make codegen` after API type changes |
0 commit comments