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
Notice that we use kubebuilder markers to generate webhook manifests.
16
+
This marker is responsible for generating a mutating webhook manifest.
17
+
18
+
The meaning of each marker can be found [here](/reference/markers/webhook.md).
19
+
20
+
You will find those markers in the both following examples.
21
+
</aside>
22
+
23
+
## Implementing Your Handler Using `Handle`
24
+
25
+
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.
setupLog.Error(err, "unable to create webhook", "webhook", "corev1.Pod")
255
+
os.Exit(1)
256
+
}
257
+
}
258
+
```
259
+
68
260
## Deploy
69
261
70
262
Deploying it is just like deploying a webhook server for CRD. You need to
@@ -73,5 +265,48 @@ Deploying it is just like deploying a webhook server for CRD. You need to
73
265
74
266
You can follow the [tutorial](/cronjob-tutorial/running.md).
75
267
268
+
## What are `Handle` and Custom Interfaces?
269
+
270
+
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.
271
+
272
+
## Purpose of the `Handle` Function
273
+
274
+
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.
275
+
276
+
### Mutation
277
+
278
+
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.
279
+
280
+
### Response Construction
281
+
282
+
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.
283
+
284
+
## Purpose of Custom Interfaces (`CustomValidator` and `CustomDefaulter`)
285
+
286
+
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.
287
+
288
+
### Validation (`CustomValidator`)
289
+
290
+
The `CustomValidator` interface is used specifically for validating resources during create, update, or delete operations. It ensures that the resource meets certain criteria before the operation is allowed to proceed. The interface provides three methods:
291
+
292
+
-**`ValidateCreate`**: Called during resource creation.
293
+
-**`ValidateUpdate`**: Called during resource updates.
294
+
-**`ValidateDelete`**: Called when a resource is deleted.
295
+
296
+
### Defaulting (`CustomDefaulter`)
297
+
298
+
The `CustomDefaulter` interface is used for setting default values on a resource before it is created. This allows you to ensure that all necessary fields have valid values, even if the user did not specify them.
299
+
300
+
## When to Use Each Approach
301
+
302
+
-**Use `Handle` when**:
303
+
- You need to both mutate and validate the resource in a single function.
304
+
- You want direct control over how the admission response is constructed and returned.
305
+
- Your webhook logic is simple and doesn’t require a clear separation of concerns.
306
+
307
+
-**Use `CustomValidator` and `CustomDefaulter` when**:
308
+
- You want to separate validation and defaulting logic for better modularity.
309
+
- Your webhook logic is complex, and separating concerns makes the code easier to manage.
310
+
- You don’t need to perform mutation and validation in the same function.
0 commit comments