|
| 1 | +# multicluster-runtime/v1-alpha Plugin |
| 2 | + |
| 3 | +The `multicluster-runtime/v1-alpha` plugin adds |
| 4 | +[sigs.k8s.io/multicluster-runtime](https://github.com/kubernetes-sigs/multicluster-runtime) |
| 5 | +support to a Kubebuilder project. Instead of reconciling objects in a single cluster, your |
| 6 | +controller will reconcile objects across all clusters registered with the chosen provider. |
| 7 | + |
| 8 | +This plugin is built into the `kubebuilder` binary — no separate installation is required. |
| 9 | + |
| 10 | +## When to use this plugin |
| 11 | + |
| 12 | +Use `multicluster-runtime/v1-alpha` when you need to: |
| 13 | + |
| 14 | +- Manage resources across **multiple Kubernetes clusters** from a single operator binary |
| 15 | +- React to cluster lifecycle events (clusters joining or leaving the fleet) |
| 16 | +- Run **fleet-wide controllers** while still using familiar controller-runtime patterns |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | + |
| 20 | +- Kubebuilder v4+ |
| 21 | +- Go 1.22+ |
| 22 | + |
| 23 | +## Project initialization |
| 24 | + |
| 25 | +Chain `multicluster-runtime/v1-alpha` after `go/v4`: |
| 26 | + |
| 27 | +```bash |
| 28 | +kubebuilder init \ |
| 29 | + --plugins go/v4,multicluster-runtime/v1-alpha \ |
| 30 | + --domain example.com \ |
| 31 | + --repo github.com/example/myop \ |
| 32 | + --provider kubeconfig |
| 33 | +``` |
| 34 | + |
| 35 | +The plugin rewrites `cmd/main.go` to use `mcmanager.New(...)` instead of `ctrl.NewManager(...)`. |
| 36 | + |
| 37 | +## Provider selection guide |
| 38 | + |
| 39 | +The `--provider` flag (default: `kubeconfig`) controls how clusters are discovered. |
| 40 | + |
| 41 | +| Provider | Flag value | Use case | |
| 42 | +|---|---|---| |
| 43 | +| **Kubeconfig secrets** | `kubeconfig` | Dynamic fleet — clusters join/leave at runtime by creating kubeconfig Secrets | |
| 44 | +| **Namespace** | `namespace` | Single cluster, namespace-per-tenant; each namespace is treated as a "cluster" | |
| 45 | +| **Cluster API** | `cluster-api` | Fleet managed by [Cluster API](https://cluster-api.sigs.k8s.io/) controllers | |
| 46 | +| **File** | `file` | Static cluster list; one kubeconfig file per cluster in a directory (great for CI) | |
| 47 | + |
| 48 | +### Kubeconfig provider (default) |
| 49 | + |
| 50 | +```bash |
| 51 | +kubebuilder init --plugins go/v4,multicluster-runtime/v1-alpha \ |
| 52 | + --domain example.com --repo github.com/example/myop \ |
| 53 | + --provider kubeconfig |
| 54 | +``` |
| 55 | + |
| 56 | +Add a cluster at runtime by creating a Secret with the kubeconfig: |
| 57 | + |
| 58 | +```yaml |
| 59 | +apiVersion: v1 |
| 60 | +kind: Secret |
| 61 | +metadata: |
| 62 | + name: my-cluster |
| 63 | + namespace: default |
| 64 | + labels: |
| 65 | + # label recognized by the kubeconfig provider |
| 66 | + multicluster.x-k8s.io/cluster-name: my-cluster |
| 67 | +type: Opaque |
| 68 | +data: |
| 69 | + kubeconfig: <base64-encoded-kubeconfig> |
| 70 | +``` |
| 71 | +
|
| 72 | +### Namespace provider |
| 73 | +
|
| 74 | +```bash |
| 75 | +kubebuilder init --plugins go/v4,multicluster-runtime/v1-alpha \ |
| 76 | + --domain example.com --repo github.com/example/myop \ |
| 77 | + --provider namespace |
| 78 | +``` |
| 79 | + |
| 80 | +The generated `cmd/main.go` starts the provider and manager concurrently using `errgroup`. |
| 81 | + |
| 82 | +### File provider |
| 83 | + |
| 84 | +```bash |
| 85 | +kubebuilder init --plugins go/v4,multicluster-runtime/v1-alpha \ |
| 86 | + --domain example.com --repo github.com/example/myop \ |
| 87 | + --provider file \ |
| 88 | + --kubeconfig-dir /etc/kubeconfig |
| 89 | +``` |
| 90 | + |
| 91 | +Place one kubeconfig file per cluster in `--kubeconfig-dir`. Each file name becomes the |
| 92 | +cluster name. |
| 93 | + |
| 94 | +## Create a multicluster controller |
| 95 | + |
| 96 | +```bash |
| 97 | +kubebuilder create api \ |
| 98 | + --plugins go/v4,multicluster-runtime/v1-alpha \ |
| 99 | + --group foo --version v1 --kind Foo \ |
| 100 | + --controller --resource |
| 101 | +``` |
| 102 | + |
| 103 | +The generated controller (`internal/controller/foo_controller.go`) uses: |
| 104 | + |
| 105 | +- `mcreconcile.Request` — carries `ClusterName` in addition to the usual `NamespacedName` |
| 106 | +- `mcbuilder.ControllerManagedBy(mgr)` — watches objects across all registered clusters |
| 107 | +- `mcmanager.Manager` — the multicluster-aware manager type |
| 108 | + |
| 109 | +### Using `req.ClusterName` |
| 110 | + |
| 111 | +```go |
| 112 | +func (r *FooReconciler) Reconcile(ctx context.Context, req mcreconcile.Request) (ctrl.Result, error) { |
| 113 | + log := log.FromContext(ctx).WithValues("cluster", req.ClusterName) |
| 114 | + |
| 115 | + // Fetch the object from the correct cluster's cache. |
| 116 | + foo := &foov1.Foo{} |
| 117 | + if err := r.Get(ctx, req.NamespacedName, foo); err != nil { |
| 118 | + return ctrl.Result{}, client.IgnoreNotFound(err) |
| 119 | + } |
| 120 | + |
| 121 | + log.Info("Reconciling Foo", "name", foo.Name) |
| 122 | + // ... your business logic ... |
| 123 | + return ctrl.Result{}, nil |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +## Webhooks |
| 128 | + |
| 129 | +Webhooks register with the **local cluster's** API server — they do not need multicluster |
| 130 | +changes. You can scaffold them normally: |
| 131 | + |
| 132 | +```bash |
| 133 | +kubebuilder create webhook \ |
| 134 | + --plugins go/v4,multicluster-runtime/v1-alpha \ |
| 135 | + --group foo --version v1 --kind Foo \ |
| 136 | + --defaulting --programmatic-validation |
| 137 | +``` |
| 138 | + |
| 139 | +The `multicluster-runtime/v1-alpha` plugin does not modify webhook files. The webhook |
| 140 | +scaffolding is handled entirely by `go/v4` and the output is identical to a single-cluster |
| 141 | +project. Webhooks register with the **local** cluster's API server and do not need |
| 142 | +multicluster changes. |
| 143 | + |
| 144 | +## Switching providers |
| 145 | + |
| 146 | +Use `kubebuilder edit` to replace the provider in an existing project: |
| 147 | + |
| 148 | +```bash |
| 149 | +kubebuilder edit --plugins multicluster-runtime/v1-alpha --provider namespace |
| 150 | +``` |
| 151 | + |
| 152 | +This rewrites `cmd/main.go` while preserving all `// +kubebuilder:scaffold:*` markers so |
| 153 | +that future `kubebuilder create api` and `kubebuilder create webhook` commands still work. |
| 154 | + |
| 155 | +## Plugin chain note |
| 156 | + |
| 157 | +This plugin is designed to run **after** `go/v4`. The plugin chain `go/v4,multicluster-runtime/v1-alpha` |
| 158 | +means: |
| 159 | + |
| 160 | +1. `go/v4` scaffolds the standard project structure |
| 161 | +2. `multicluster-runtime/v1-alpha` rewrites `cmd/main.go` to use the multicluster manager |
| 162 | + |
| 163 | +If `go/v4` is absent from the chain, the scaffolded `cmd/main.go` will not compile |
| 164 | +because the standard project structure (`api/`, `internal/controller/`, `Makefile`, etc.) |
| 165 | +will be missing. Always chain `go/v4` first. |
0 commit comments