You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Your handler must implement the [admission.Handler](https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/webhook/admission#Handler) interface. This function is responsible for both mutating and validating the incoming resource.
Notice that we use kubebuilder markers to generate webhook manifests.
62
+
This marker is responsible for generating a mutating webhook manifest.
63
+
64
+
The meaning of each marker can be found [here](./markers/webhook.md).
65
+
66
+
To have controller-gen automatically generate the webhook configuration for you, you need to add the appropriate markers in your code. These markers should follow a specific format, especially when defining the webhook path.
40
67
41
-
**Note**: in order to have controller-gen generate the webhook configuration for
setupLog.Error(err, "unable to create webhook", "webhook", "corev1.Pod")
283
+
os.Exit(1)
284
+
}
285
+
}
286
+
```
287
+
68
288
## Deploy
69
289
70
290
Deploying it is just like deploying a webhook server for CRD. You need to
@@ -73,5 +293,36 @@ Deploying it is just like deploying a webhook server for CRD. You need to
73
293
74
294
You can follow the [tutorial](/cronjob-tutorial/running.md).
75
295
296
+
## What are `Handle` and Custom Interfaces?
297
+
298
+
In the context of Kubernetes admission webhooks, the `Handle` function and the custom interfaces (`CustomValidator` and `CustomDefaulter`) are two different approaches to implementing webhook logic. Each serves specific purposes, and the choice between them depends on the needs of your webhook.
299
+
300
+
## Purpose of the `Handle` Function
301
+
302
+
The `Handle` function is a core part of the admission webhook process. It is responsible for directly processing the incoming admission request and returning an `admission.Response`. This function is particularly useful when you need to handle both validation and mutation within the same function.
303
+
304
+
### Mutation
305
+
306
+
If your webhook needs to modify the resource (e.g., add or change annotations, labels, or other fields), the `Handle` function is where you would implement this logic. Mutation involves altering the resource before it is persisted in Kubernetes.
307
+
308
+
### Response Construction
309
+
310
+
The `Handle` function is also responsible for constructing the `admission.Response`, which determines whether the request should be allowed or denied, or if the resource should be patched (mutated). The `Handle` function gives you full control over how the response is built and what changes are applied to the resource.
311
+
312
+
## Purpose of Custom Interfaces (`CustomValidator` and `CustomDefaulter`)
313
+
314
+
The `CustomValidator` and `CustomDefaulter` interfaces provide a more modular approach to implementing webhook logic. They allow you to separate validation and defaulting (mutation) into distinct methods, making the code easier to maintain and reason about.
315
+
316
+
## When to Use Each Approach
317
+
318
+
-**Use `Handle` when**:
319
+
- You need to both mutate and validate the resource in a single function.
320
+
- You want direct control over how the admission response is constructed and returned.
321
+
- Your webhook logic is simple and doesn’t require a clear separation of concerns.
322
+
323
+
-**Use `CustomValidator` and `CustomDefaulter` when**:
324
+
- You want to separate validation and defaulting logic for better modularity.
325
+
- Your webhook logic is complex, and separating concerns makes the code easier to manage.
326
+
- You don’t need to perform mutation and validation in the same function.
0 commit comments