@@ -136,8 +136,10 @@ To learn more, see the following resources:
136
136
This section describes recommendations for improving performance and reducing
137
137
latency. In summary, these are as follows:
138
138
139
- * Consolidate webhooks and limit the number of API calls per webhook
140
- * Use audit logs to check for webhooks that repeatedly do the same action
139
+ * Consolidate webhooks and limit the number of API calls per webhook.
140
+ * Use audit logs to check for webhooks that repeatedly do the same action.
141
+ * Use load balancing for webhook availability.
142
+ * Set a small timeout value for each webhook.
141
143
142
144
### Improve latency in mutating webhooks {#improve-latency-mutating-webhooks}
143
145
@@ -182,6 +184,22 @@ To detect these loops, try the following:
182
184
same patch being applied to the same object, or for an object having
183
185
a field updated and reverted multiple times.
184
186
187
+ ### Set a small timeout value {#small-timeout}
188
+
189
+ Admission webhooks should evaluate as quickly as possible (typically in
190
+ milliseconds), since they add to API request latency. Use a small timeout for
191
+ webhooks.
192
+
193
+ For details, see
194
+ [ Timeouts] ( /docs/reference/access-authn-authz/extensible-admission-controllers/#timeouts ) .
195
+
196
+ ### Use a load balancer to ensure webhook availability {#load-balancer-webhook}
197
+
198
+ Admission webhooks should leverage some form of load-balancing to provide high
199
+ availability and performance benefits. If a webhook is running within the
200
+ cluster, you can run multiple webhook backends behind a Service to use the
201
+ Service load balancing.
202
+
185
203
## Request filtering {#request-filtering}
186
204
187
205
This section provides recommendations for filtering which requests trigger
@@ -283,27 +301,36 @@ Consider the following when modifying arrays:
283
301
284
302
### Avoid side effects {#avoid-side-effects}
285
303
286
- It is recommended that admission webhooks should avoid side effects if possible, which means the webhooks operate only on the
287
- content of the ` AdmissionReview ` sent to them, and do not make out-of-band changes. The ` .webhooks[].sideEffects ` field should
288
- be set to ` None ` if a webhook doesn't have any side effect.
304
+ Ensure that your webhooks operate only on the content of the ` AdmissionReview `
305
+ that's sent to them, and do not make out-of-band changes. These additional
306
+ changes, called _ side effects_ , might cause conflicts during admission if they
307
+ aren't reconciled properly. The ` .webhooks[].sideEffects ` field should
308
+ be set to ` None ` if a webhook doesn't have any side effect.
289
309
290
- If side effects are required during the admission evaluation, they must be suppressed when processing an
291
- ` AdmissionReview ` object with ` dryRun ` set to ` true ` , and the ` .webhooks[].sideEffects ` field should be
292
- set to ` NoneOnDryRun ` . See [ Side effects] ( #side-effects ) for more detail.
310
+ If side effects are required during the admission evaluation, they must be
311
+ suppressed when processing an ` AdmissionReview ` object with ` dryRun ` set to
312
+ ` true ` , and the ` .webhooks[].sideEffects ` field should be set to ` NoneOnDryRun ` .
313
+
314
+ For details, see
315
+ [ Side effects] ( /docs/reference/access-authn-authz/extensible-admission-controllers/#side-effects ) .
293
316
294
317
### Avoid self-mutations {#avoid-self-mutation}
295
318
296
- A webhook running inside the cluster might cause deadlocks for its own deployment if it is configured
297
- to intercept resources required to start its own pods.
319
+ A webhook running inside the cluster might cause deadlocks for its own
320
+ deployment if it is configured to intercept resources required to start its own
321
+ Pods.
322
+
323
+ For example, a mutating admission webhook is configured to admit ` CREATE ` Pod
324
+ requests only if a certain label is set in the Pod (such as ` "env": "prod" ` ).
325
+ The webhook server runs in a Deployment that doesn't set the ` "env" ` label.
298
326
299
- For example, a mutating admission webhook is configured to admit ` CREATE ` pod requests only if a certain label is set in the
300
- pod (e.g. ` "env": "prod" ` ). The webhook server runs in a deployment which doesn't set the ` "env" ` label.
301
- When a node that runs the webhook server pods
302
- becomes unhealthy, the webhook deployment will try to reschedule the pods to another node. However the requests will
303
- get rejected by the existing webhook server since the ` "env" ` label is unset, and the migration cannot happen.
327
+ When a node that runs the webhook server Pods becomes unhealthy, the webhook
328
+ Deployment tries to reschedule the Pods to another node. However, the existing
329
+ webhook server rejects the requests since the ` "env" ` label is unset. As a
330
+ result, the migration cannot happen.
304
331
305
- It is recommended to exclude the namespace where your webhook is running with a
306
- [ namespaceSelector] ( #matching-requests-namespaceselector ) .
332
+ Exclude the namespace where your webhook is running with a
333
+ [ namespaceSelector] ( /docs/reference/access-authn-authz/extensible-admission-controllers/ #matching-requests-namespaceselector) .
307
334
308
335
### Fail open and validate the final state {#fail-open-validate-final-state}
309
336
@@ -402,39 +429,42 @@ challenging. The following recommendations might help:
402
429
multiple times by the same webhook.
403
430
* Ensure that the scope of each mutating webhook is specific and limited.
404
431
405
- #### Example of idempotent mutating admission webhooks :
432
+ The following examples show idempotent mutation logic :
406
433
407
- 1 . For a ` CREATE ` pod request, set the field ` .spec.securityContext.runAsNonRoot ` of the
408
- pod to true, to enforce security best practices .
434
+ 1 . For a ` CREATE ` Pod request, set the field
435
+ ` .spec.securityContext.runAsNonRoot ` of the Pod to true .
409
436
410
- 2 . For a ` CREATE ` pod request, if the field ` .spec.containers[].resources.limits `
411
- of a container is not set, set default resource limits.
437
+ 1 . For a ` CREATE ` Pod request, if the field
438
+ ` .spec.containers[].resources.limits ` of a container is not set, set default
439
+ resource limits.
412
440
413
- 3 . For a ` CREATE ` pod request, inject a sidecar container with name ` foo-sidecar ` if no container
414
- with the name ` foo-sidecar ` already exists.
441
+ 1 . For a ` CREATE ` Pod request, inject a sidecar container with name
442
+ ` foo-sidecar ` if no container with the name ` foo-sidecar ` already exists.
415
443
416
- In the cases above, the webhook can be safely reinvoked, or admit an object that already has the fields set.
444
+ In these cases, the webhook can be safely reinvoked, or admit an object that
445
+ already has the fields set.
417
446
418
- #### Example of non-idempotent mutating admission webhooks :
447
+ The following examples show non-idempotent mutation logic :
419
448
420
- 1 . For a ` CREATE ` pod request, inject a sidecar container with name ` foo-sidecar `
421
- suffixed with the current timestamp (e.g. ` foo-sidecar-19700101-000000 ` ).
449
+ 1 . For a ` CREATE ` Pod request, inject a sidecar container with name
450
+ ` foo-sidecar ` suffixed with the current timestamp (such as
451
+ ` foo-sidecar-19700101-000000 ` ).
422
452
423
- 2 . For a ` CREATE ` /` UPDATE ` pod request, reject if the pod has label ` "env" ` set,
424
- otherwise add an ` "env": "prod" ` label to the pod.
453
+ Reinvoking the webhook can result in the same sidecar being injected multiple
454
+ times to a Pod, each time with a different container name. Similarly, the
455
+ webhook can inject duplicated containers if the sidecar already exists in
456
+ a user-provided pod.
425
457
426
- 3 . For a ` CREATE ` pod request, blindly append a sidecar container named
427
- ` foo-sidecar ` without looking to see if there is already a ` foo-sidecar `
428
- container in the pod.
458
+ 1 . For a ` CREATE ` /` UPDATE ` Pod request, reject if the Pod has label ` "env" ` set,
459
+ otherwise add an ` "env": "prod" ` label to the Pod.
429
460
430
- In the first case above, reinvoking the webhook can result in the same sidecar being injected multiple times to a pod, each time
431
- with a different container name. Similarly the webhook can inject duplicated containers if the sidecar already exists in
432
- a user-provided pod.
461
+ Reinvoking the webhook will result in the webhook failing on its own output.
433
462
434
- In the second case above, reinvoking the webhook will result in the webhook failing on its own output.
463
+ 1 . For a ` CREATE ` Pod request, append a sidecar container named ` foo-sidecar `
464
+ without checking whether a ` foo-sidecar ` container exists.
435
465
436
- In the third case above, reinvoking the webhook will result in duplicated containers in the pod spec , which makes
437
- the request invalid and rejected by the API server.
466
+ Reinvoking the webhook will result in duplicated containers in the Pod , which
467
+ makes the request invalid and rejected by the API server.
438
468
439
469
## Mutation testing and validation {#mutation-testing-validation}
440
470
@@ -541,15 +571,6 @@ Consider situations like the preceding example when writing your webhooks.
541
571
Exclude operations that are a result of Kubernetes responding to unavoidable
542
572
incidents.
543
573
544
- It is recommended that admission webhooks should evaluate as quickly as possible (typically in
545
- milliseconds), since they add to API request latency.
546
- It is encouraged to use a small timeout for webhooks. See [ Timeouts] ( #timeouts ) for more detail.
547
-
548
- It is recommended that admission webhooks should leverage some format of load-balancing, to
549
- provide high availability and performance benefits. If a webhook is running within the cluster,
550
- you can run multiple webhook backends behind a service to leverage the load-balancing that service
551
- supports.
552
-
553
574
## Examples of good implementations {#example-good-implementations}
554
575
555
576
{{% thirdparty-content %}}
0 commit comments