Skip to content

Commit 497be37

Browse files
authored
Merge pull request #2337 from vincepri/add-docs-tenancy
📖 Add documentation on how to support multi-tenancy with webhooks
2 parents 31a8b87 + bfd4873 commit 497be37

File tree

2 files changed

+184
-11
lines changed

2 files changed

+184
-11
lines changed

config/webhook/manager_pull_policy.yaml

Lines changed: 0 additions & 11 deletions
This file was deleted.

docs/book/src/developer/providers/v1alpha2-to-v1alpha3.md

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,187 @@ unique keys to `failureDomainSpec`s as well as respecting a set `Machine.Spec.Fa
154154
instances.
155155

156156
Please see the cluster and machine infrastructure provider specifications for more detail.
157+
158+
## Refactor kustomize `config/` folder to support multi-tenancy when using webhooks.
159+
160+
> Pre-Requisites: Upgrade to CRD v1.
161+
162+
More details and background can be found in [Issue #2275](https://github.com/kubernetes-sigs/cluster-api/issues/2275) and [PR #2279](https://github.com/kubernetes-sigs/cluster-api/pull/2279).
163+
164+
Goals:
165+
- Have all webhook related components in the `capi-webhook-system` namespace.
166+
- Achieves multi-tenancy and guarantees that both CRD and webhook resources can live globally and can be patched in future iterations.
167+
- Run a new manager instance that ONLY runs webhooks and doesn't install any reconcilers.
168+
169+
Steps:
170+
- In `config/certmanager/`
171+
- **Patch**
172+
- **certificate.yaml**: The `secretName` value MUST be set to `$(SERVICE_NAME)-cert`.
173+
- **kustomization.yaml**: Add the following to `varReference`
174+
```yaml
175+
- kind: Certificate
176+
group: cert-manager.io
177+
path: spec/secretName
178+
```
179+
180+
- In `config/`
181+
- **Create**
182+
- **kustomization.yaml**: This file is going to function as the new entrypoint to run `kustomize build`.
183+
`PROVIDER_NAME` is the name of your provider, e.g. `aws`.
184+
`PROVIDER_TYPE` is the type of your provider, e.g. `control-plane`, `bootstrap`, `infrastructure`.
185+
```yaml
186+
namePrefix: {{e.g. capa-, capi-, etc.}}
187+
188+
commonLabels:
189+
cluster.x-k8s.io/provider: "{{PROVIDER_TYPE}}-{{PROVIDER_NAME}}"
190+
191+
bases:
192+
- crd
193+
- webhook # Disable this if you're not using the webhook functionality.
194+
- default
195+
196+
patchesJson6902:
197+
- target: # NOTE: This patch needs to be repeatd for EACH CustomResourceDefinition you have under crd/bases.
198+
group: apiextensions.k8s.io
199+
version: v1
200+
kind: CustomResourceDefinition
201+
name: {{CRD_NAME_HERE}}
202+
path: patch_crd_webhook_namespace.yaml
203+
```
204+
- **patch_crd_webhook_namespace.yaml**: This patch sets the conversion webhook namespace to `capi-webhook-system`.
205+
```yaml
206+
- op: replace
207+
path: "/spec/conversion/webhook/clientConfig/service/namespace"
208+
value: capi-webhook-system
209+
```
210+
211+
- In `config/default`
212+
- **Create**
213+
- **namespace.yaml**
214+
```yaml
215+
apiVersion: v1
216+
kind: Namespace
217+
metadata:
218+
name: system
219+
```
220+
- **Move**
221+
- `manager_image_patch.yaml` to `config/manager`
222+
- `manager_label_patch.yaml` to `config/manager`
223+
- `manager_pull_policy.yaml` to `config/manager`
224+
- `manager_auth_proxy_patch.yaml` to `config/manager`
225+
- `manager_webhook_patch.yaml` to `config/webhook`
226+
- `webhookcainjection_patch.yaml` to `config/webhook`
227+
- `manager_label_patch.yaml` to trash.
228+
- **Patch**
229+
- **kustomization.yaml**
230+
- Add under `resources`:
231+
```yaml
232+
resources:
233+
- namespace.yaml
234+
```
235+
- Replace `bases` with:
236+
```yaml
237+
bases:
238+
- ../rbac
239+
- ../manager
240+
```
241+
- Add under `patchesStrategicMerge`:
242+
```yaml
243+
patchesStrategicMerge:
244+
- manager_role_aggregation_patch.yaml
245+
```
246+
- Remove `../crd` from `bases` (now in `config/kustomization.yaml`).
247+
- Remove `namePrefix` (now in `config/kustomization.yaml`).
248+
- Remove `commonLabels` (now in `config/kustomization.yaml`).
249+
- Remove from `patchesStrategicMerge`:
250+
- manager_image_patch.yaml
251+
- manager_pull_policy.yaml
252+
- manager_auth_proxy_patch.yaml
253+
- manager_webhook_patch.yaml
254+
- webhookcainjection_patch.yaml
255+
- manager_label_patch.yaml
256+
- Remove from `vars`:
257+
- CERTIFICATE_NAMESPACE
258+
- CERTIFICATE_NAME
259+
- SERVICE_NAMESPACE
260+
- SERVICE_NAME
261+
262+
- In `config/manager`
263+
- **Patch**
264+
- **manager.yaml**: Remove the `Namespace` object.
265+
- **kustomization.yaml**:
266+
- Add under `patchesStrategicMerge`:
267+
```yaml
268+
patchesStrategicMerge:
269+
- manager_image_patch.yaml
270+
- manager_pull_policy.yaml
271+
- manager_auth_proxy_patch.yaml
272+
```
273+
274+
- In `config/webhook`
275+
- **Patch**
276+
- **kustomizeconfig.yaml**
277+
- Add the following to `varReference`
278+
```yaml
279+
- kind: Deployment
280+
path: spec/template/spec/volumes/secret/secretName
281+
```
282+
- **kustomization.yaml**
283+
- Add `namespace: capi-webhook-system` at the top of the file.
284+
- Under `resources`, add `../certmanager` and `../manager`.
285+
- Add at the bottom of the file:
286+
```yaml
287+
patchesStrategicMerge:
288+
- manager_webhook_patch.yaml
289+
- webhookcainjection_patch.yaml # Disable this value if you don't have any defaulting or validation webhook. If you don't know, you can check if the manifests.yaml file in the same directory has any contents.
290+
291+
vars:
292+
- name: CERTIFICATE_NAMESPACE # namespace of the certificate CR
293+
objref:
294+
kind: Certificate
295+
group: cert-manager.io
296+
version: v1alpha2
297+
name: serving-cert # this name should match the one in certificate.yaml
298+
fieldref:
299+
fieldpath: metadata.namespace
300+
- name: CERTIFICATE_NAME
301+
objref:
302+
kind: Certificate
303+
group: cert-manager.io
304+
version: v1alpha2
305+
name: serving-cert # this name should match the one in certificate.yaml
306+
- name: SERVICE_NAMESPACE # namespace of the service
307+
objref:
308+
kind: Service
309+
version: v1
310+
name: webhook-service
311+
fieldref:
312+
fieldpath: metadata.namespace
313+
- name: SERVICE_NAME
314+
objref:
315+
kind: Service
316+
version: v1
317+
name: webhook-service
318+
```
319+
- **manager_webhook_patch.yaml**
320+
- Under `containers` find `manager` and add after `name`
321+
```yaml
322+
- "--metrics-addr=127.0.0.1:8080"
323+
- "--webhook-port=9443"
324+
```
325+
- Under `volumes` find `cert` and replace `secretName`'s value with `$(SERVICE_NAME)-cert`.
326+
- **service.yaml**
327+
- Remove the `selector` map, if any. The `control-plane` label is not needed anymore, a unique label is applied using `commonLabels` under `config/kustomization.yaml`.
328+
329+
In `main.go`
330+
- Default the `webhook-port` flag to `0`
331+
```go
332+
flag.IntVar(&webhookPort, "webhook-port", 0,
333+
"Webhook Server port, disabled by default. When enabled, the manager will only work as webhook server, no reconcilers are installed.")
334+
```
335+
- The controller MUST register reconcilers if and only if `webhookPort == 0`.
336+
- The controller MUST register webhooks if and only if `webhookPort != 0`.
337+
338+
After all the changes above are performed, `kustomize build` MUST target `config/`, rather than `config/default`. Using your favorite editor, search for `config/default` in your repository and change the paths accordingly.
339+
340+
In addition, often the `Makefile` contains a sed-replacement for `manager_image_patch.yaml`, this file has been moved from `config/default` to `config/manager`. Using your favorite editor, search for `manager_image_patch` in your repository and change the paths accordingly.

0 commit comments

Comments
 (0)